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

# Fallback Models: Mid-Run Failure

> Tests what happens when the primary model fails AFTER a tool call (and or within a run).

```python mid_run_fallback.py theme={null}
"""
Fallback Models — Mid-Run Failure
====================================

Tests what happens when the primary model fails AFTER a tool call (and or within a run).

Flow:
  1. gpt-4o receives the request and makes a tool call
  2. The tool mutates the model instance's id to something invalid
  3. The next API call inside Model.response()'s while-loop fails
  4. The error bubbles up to call_model_with_fallback
  5. Fallback (Claude) is tried
"""

from agno.agent import Agent
from agno.models.anthropic import Claude
from agno.models.openai import OpenAIChat


def break_model(agent: Agent) -> str:
    """Tool that corrupts the executing model mid-run."""
    # Point the model at an unreachable server and clear the cached client
    # so the next API call in the response() loop fails with a connection error.
    agent.model.base_url = "http://localhost:1/v1"  # type: ignore[union-attr]
    agent.model.client = None  # type: ignore[union-attr]
    return "Tool executed successfully."


agent = Agent(
    model=OpenAIChat(id="gpt-4o"),
    tools=[break_model],
    fallback_models=[Claude(id="claude-sonnet-4-20250514")],
)

if __name__ == "__main__":
    agent.print_response("Call the break_model tool", stream=True)
```

## Run the Example

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

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

  <Step title="Export your API keys">
    <CodeGroup>
      ```bash Mac/Linux theme={null}
      export ANTHROPIC_API_KEY="your_anthropic_api_key_here"
      export OPENAI_API_KEY="your_openai_api_key_here"
      ```

      ```bash Windows theme={null}
      $Env:ANTHROPIC_API_KEY="your_anthropic_api_key_here"
      $Env:OPENAI_API_KEY="your_openai_api_key_here"
      ```
    </CodeGroup>
  </Step>

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

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

Full source: [cookbook/02\_agents/17\_fallback\_models/03\_mid\_run\_fallback.py](https://github.com/agno-agi/agno/blob/main/cookbook/02_agents/17_fallback_models/03_mid_run_fallback.py)
