AgentMiddleware[_ModelRouterState]Logic to run before the model is called.
Async logic to run before the model is called.
Logic to run after the model is called.
Async logic to run after the model is called.
Logic to run after the agent execution completes.
Async logic to run after the agent execution completes.
Intercept tool execution for retries, monitoring, or modification.
Intercept and control async tool execution via handler callback.
| Name | Type | Description |
|---|---|---|
choices* | Mapping[str, ModelChoice] | Named model choices, each containing a LangChain model or model string and the criterion for selecting it. |
instructions* | Content | Instructions the classifier should follow when selecting a route. |
classifier* | BaseDecisionModel | Decision model used to select a route. |
| Name | Type |
|---|---|
| choices | Mapping[str, ModelChoice] |
| instructions | Content |
| classifier | BaseDecisionModel |
Select an agent's model with a decision model's Choice answer.
The middleware classifies the latest human message once before an agent run,
stores the complete ChoiceAnswer in agent state, 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.
This middleware is in beta. Its API may change without notice.
from langchain.agents import create_agent
from langchain.agents.middleware import ModelChoice, ModelRouterMiddleware
router = ModelRouterMiddleware(
choices={
"fast": ModelChoice(
model="openai:gpt-5-mini",
criteria="Simple, well-scoped tasks.",
),
"powerful": ModelChoice(
model=powerful_model,
criteria="Complex tasks requiring deeper reasoning.",
),
},
instructions="Choose the least costly model suited to the task.",
classifier=classifier,
)
agent = create_agent("openai:gpt-5-mini", middleware=[router])