> ## Documentation Index
> Fetch the complete documentation index at: https://agno-v2-docs-scavio-google-v2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Dynamic Model Router

> Configure an OpenRouter agent with an ordered fallback model list so a request failing on rate limits, timeouts, or overload retries on the next model.

Use dynamic model router with OpenRouter.

```python dynamic_model_router.py theme={null}
"""
This example demonstrates how to use dynamic model router with OpenRouter.

Dynamic models provide automatic failover when the primary model encounters:
- Rate limits
- Timeouts
- Unavailability
- Model overload

OpenRouter will automatically try the models defined in order until one succeeds.
"""

from agno.agent import Agent
from agno.models.openrouter import OpenRouter

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------

# Create an agent with dynamic models
# If the primary model fails, OpenRouter will automatically try the models defined in order
agent = Agent(
    model=OpenRouter(
        id="anthropic/claude-sonnet-4",  # Primary model
        models=[
            "deepseek/deepseek-r1",  # First fallback model
            "openai/gpt-4o",  # Second fallback model
        ],
    ),
    markdown=True,
)

# Run the agent - it will use the primary model if available,
# or automatically fall back to alternative models if needed
agent.print_response("Write a short poem about resilience and backup plans")

# You can also check which model was actually used in the response
# by examining the response metadata (if available from OpenRouter)

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------

if __name__ == "__main__":
    pass
```

## Run the Example

<Steps>
  <Snippet file="create-venv-step.mdx" />

  <Step title="Install dependencies">
    ```bash theme={null}
    uv pip install -U agno openai
    ```
  </Step>

  <Step title="Export your OpenRouter API key">
    <CodeGroup>
      ```bash Mac/Linux theme={null}
      export OPENROUTER_API_KEY="your_openrouter_api_key_here"
      ```

      ```bash Windows theme={null}
      $Env:OPENROUTER_API_KEY="your_openrouter_api_key_here"
      ```
    </CodeGroup>
  </Step>

  <Step title="Run the example">
    Save the code above as `dynamic_model_router.py`, then run:

    ```bash theme={null}
    python dynamic_model_router.py
    ```
  </Step>
</Steps>

Full source: [cookbook/90\_models/openrouter/chat/dynamic\_model\_router.py](https://github.com/agno-agi/agno/blob/main/cookbook/90_models/openrouter/chat/dynamic_model_router.py)
