# Retry and fallback middleware

```python
from langchain.agents import create_agent
from langchain.agents.middleware import ModelRetryMiddleware, ModelFallbackMiddleware

agent = create_agent(
    model="gpt-5.5",
    tools=[...],
    middleware=[
        ModelRetryMiddleware(
            max_retries=3,
            backoff_factor=2.0,
            initial_delay=1.0,
        ),
        ModelFallbackMiddleware(
            "gpt-5.4-mini",
            "claude-3-5-sonnet-20241022",
        ),
    ],
)
```

## How they decide

- Retry respects each exception's `is_retryable` attribute by default: rate limits, timeouts, connection errors, and server errors retry; auth errors, not-found, bad requests, and context overflow do not.
- `on_failure` controls exhaustion: `continue` returns an AIMessage with the error details so the agent can handle it, `error` re-raises, or pass a callable for custom content.
- Fallback tries the models in order when the primary fails. Model identifiers can be strings like `openai:gpt-5.4-mini` or model instances.

## Rules

- Retry handles transient failures; fallback handles outages. Use both for anything user-facing.
- Backoff has jitter on by default; leave it on so a fleet of agents does not thundering-herd the provider.
- Fallback models should be cheaper or more available than the primary, and they must support the features the agent uses (tool calling at minimum). A fallback that cannot call tools just fails differently.