Select an agent's model with a decision model's Choice answer (beta).
The middleware classifies the latest human message once before an agent
run, stores the complete ChoiceAnswer in agent state as modelRoute, and
uses its selected label for every model call in the run. Keeping the
complete answer makes probabilities and confidence available in state and
traces. Classifier failures propagate and terminate the run rather than
silently selecting a different model.
modelRouterMiddleware(
config: ModelRouterMiddlewareConfig
): AgentMiddleware<ZodObject<__type, strip>, undefined, unknown, readonly ClientTool | ServerTool[], readonly []>| Name | Type | Description |
|---|---|---|
config* | ModelRouterMiddlewareConfig |
import { createAgent, modelRouterMiddleware } from "langchain";
const router = modelRouterMiddleware({
choices: {
fast: {
model: "openai:gpt-5-mini",
criteria: "Simple, well-scoped tasks.",
},
powerful: {
model: powerfulModel,
criteria: "Complex tasks requiring deeper reasoning.",
},
},
instructions: "Choose the least costly model suited to the task.",
classifier,
});
const agent = createAgent({
model: "openai:gpt-5-mini",
middleware: [router],
});