Skip to content

ModelRouter

Defined in: src/models/routing/router.ts:117

Routes each agent invocation among an immutable set of candidate models.

The default FallbackStrategy prefers the candidate with the fewest recorded failures and breaks ties by declaration order. maxSwitches bounds successful candidate changes per invocation.

const router = new ModelRouter([
new RoutingCandidate({ model: primary, name: 'primary' }),
new RoutingCandidate({ model: fallback, name: 'fallback' }),
])
const agent = new Agent({ model: router })
new ModelRouter(models, options?): ModelRouter;

Defined in: src/models/routing/router.ts:133

Create a model router.

ParameterTypeDescription
modelsreadonly CandidateInput[]Candidate models, nested routers, or candidate wrappers
optionsModelRouterOptionsRouting strategy and switch cap

ModelRouter

TypeError if models or the strategy are invalid

Error if candidates are empty, duplicated, named alike, stateful, or maxSwitches is not a non-negative integer

readonly name: "strands:model-router" = 'strands:model-router';

Defined in: src/models/routing/router.ts:118

A stable string identifier for the plugin. Used for logging, duplicate detection, and plugin management.

For strands-vended plugins, names should be prefixed with strands:.

Plugin.name

get candidates(): readonly RoutingCandidate[];

Defined in: src/models/routing/router.ts:152

Normalized candidates in declaration order.

readonly RoutingCandidate[]


get defaultModel(): Model;

Defined in: src/models/routing/router.ts:157

First declared candidate resolved without consulting a strategy.

Model

initAgent(agent): void;

Defined in: src/models/routing/router.ts:179

Register routing middleware and lifecycle hooks.

ParameterTypeDescription
agentLocalAgentAgent using this router as its model

void

Error if attached as an ordinary plugin rather than as the model

Plugin.initAgent