# OpenAIModelRouterMiddleware

> **Class** in `langchain_openai`

📖 [View in docs](https://reference.langchain.com/python/langchain-openai/middleware/model_router/OpenAIModelRouterMiddleware)

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.

!!! warning

    This middleware is experimental. Its API may change without notice.

## Signature

```python
OpenAIModelRouterMiddleware(
    self,
    *,
    choices: Mapping[str, ModelChoice],
    instructions: str,
    model: str | OpenAIDecisions,
)
```

## Description

??? example "Route agent calls by task"

```python
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])
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `choices` | `Mapping[str, ModelChoice]` | Yes | Named model choices, each containing a LangChain model or model string and the criterion for selecting it. |
| `instructions` | `str` | Yes | Routing question sent to the Decisions API. |
| `model` | `str \| OpenAIDecisions` | Yes | Decisions model name, or a configured `OpenAIDecisions` instance. |

## Extends

- `AgentMiddleware[_ModelRouterState]`

## Constructors

```python
__init__(
    self,
    *,
    choices: Mapping[str, ModelChoice],
    instructions: str,
    model: str | OpenAIDecisions,
) -> None
```

| Name | Type |
|------|------|
| `choices` | `Mapping[str, ModelChoice]` |
| `instructions` | `str` |
| `model` | `str \| OpenAIDecisions` |


## Properties

- `state_schema`
- `trace_policy`
- `question`
- `models`
- `decisions`

## Methods

- [`before_agent()`](https://reference.langchain.com/python/langchain-openai/middleware/model_router/OpenAIModelRouterMiddleware/before_agent)
- [`abefore_agent()`](https://reference.langchain.com/python/langchain-openai/middleware/model_router/OpenAIModelRouterMiddleware/abefore_agent)
- [`wrap_model_call()`](https://reference.langchain.com/python/langchain-openai/middleware/model_router/OpenAIModelRouterMiddleware/wrap_model_call)
- [`awrap_model_call()`](https://reference.langchain.com/python/langchain-openai/middleware/model_router/OpenAIModelRouterMiddleware/awrap_model_call)

---

[View source on GitHub](https://github.com/langchain-ai/langchain/blob/16a08bda1247010c23fcfa887e421259a5fced5e/libs/partners/openai/langchain_openai/middleware/model_router.py#L80)