# Fallback and retry

Canonical URL: https://developers.iagenify.com/sdk/agent/fallback-and-retry/
Language: en

A turn that fails for a passing reason is tried once more, then handed to the next model you named. Two things are never retried: not enough credits, and a run you stopped.

Status: 2 attempts per model · 1.5 s between · fallbackModels in order

## The order {#order}

- The model, attempt one.
- On a passing failure: wait 1.5 seconds, emit `retry`, attempt two.
- Still failing: emit `fallback` and move to the next model in `fallbackModels`, which gets the same two attempts.
- Out of models: the last error is thrown, an `error` event is emitted, and `run()` rejects.

```javascript
const agent = api.agents.create({
  runtime: "local",
  machine: pc,
  model: "gemini-3.7-flash",
  fallbackModels: ["gemini-3.5-flash", "gemini-3.1-pro"],
  onEvent: (e) => {
    if (e.type === "retry")    console.warn("retrying", e.model, e.reason);
    if (e.type === "fallback") console.warn("falling back", e.from, "→", e.to);
  },
});
```

A name equal to the main model is dropped from the list, and so is anything that is not a string — a fallback that is the model that just failed is not a fallback.

## What counts as passing {#transient}

| Kind | Values |
| --- | --- |
| HTTP status | `408` · `429` · `500` · `502` · `503` · `504` |
| Error code | `unreachable` · `timeout` · `turn_failed` · `turn_incomplete` · `stream_failed` |

Anything else — a refused model name, a context that will not fit, a malformed request — fails immediately on that model and moves on to the next one rather than being tried twice. Retrying a request that is wrong is only slower.

## Two things stop everything {#never}

> **NotEnoughCredits and stop() are not retried, on any model**
>
> Retrying a credit refusal would only refuse again, and falling back to another model would spend what little is left. A stopped run must stop: trying again is the opposite of what was asked.

## Each attempt that reaches the provider is billed {#cost}

A retry is a new turn. A failure before the provider costs nothing, but a turn that started and broke mid-stream can have been charged — which is why the retry count is two and not five.

## Choosing a fallback {#choosing}

- Prefer a model of the **same family**: tool declarations, thinking settings and the shape of a tool call carry over.
- Put the cheaper one later, not first — the fallback is for availability, not for price.
- Mind the [context window](/sdk/agent/context/window): a smaller model may refuse a history the first one accepted.

## A fallback is per turn, not per run {#midrun}

The next step starts again from the main model. A provider that recovers is used again immediately, and a run does not stay demoted because one turn failed.

- [turn](/sdk/agent/events/turn): Which model actually served each step.
- [error](/sdk/agent/events/error): What reaches you when everything has been tried.
- [The loop](/sdk/agent/loop): What one step is, and what ends it.

```python
def on_event(event):
    if event["type"] == "fallback":
        print("falling back", event["from"], "→", event["to"])

agent = api.agents.create(
    runtime="local",
    machine=pc,
    model="gemini-3.7-flash",
    fallback_models=["gemini-3.5-flash", "gemini-3.1-pro"],
)
agent.run(message="…", on_event=on_event)   # the Python engine reports the fallback, not each retry
```
