AgentMiddleware[_ModelRouterState]| 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* | str | Routing question sent to the Decisions API. |
model* | str | OpenAIDecisions |
| Name | Type |
|---|---|
| choices | Mapping[str, ModelChoice] |
| instructions | str |
| model | str | OpenAIDecisions |
Select an agent's model with an OpenAI Decisions Choice question.
Classifies the latest human message once before each agent run, stores the
answer as a ModelRoute in agent state under model_route, and uses the selected
route for every model call in the run. Keeping the complete answer makes
probabilities and confidence available in state and traces.
The message text and base64 images are classified. Hosted image URLs, files,
and audio are replaced with placeholders such as [image omitted]. If a request
with images is rejected (HTTP 400 or 413), routing is retried once with every
image replaced.
When no route is selected, because the model refused or the state has no human
message, model_route is set to None and model calls use the agent's own
model. API errors propagate and terminate the run.
This middleware is experimental. Its API may change without notice.
from langchain.agents import create_agent
from langchain_openai.middleware import (
ModelChoice,
OpenAIModelRouterMiddleware,
)
router = OpenAIModelRouterMiddleware(
choices={
"fast": ModelChoice(
model="openai:gpt-6-luna",
criteria="Simple, well-scoped tasks.",
),
"powerful": ModelChoice(
model="openai:gpt-6-sol",
criteria="Complex tasks requiring deeper reasoning.",
),
},
instructions="Choose the least costly model suited to the task.",
model="gpt-6-luna",
)
agent = create_agent("openai:gpt-6-luna", middleware=[router])Decisions model name, or a configured OpenAIDecisions instance.