The order
- The model, attempt one.
- On a passing failure: wait 1.5 seconds, emit
retry, attempt two. - Still failing: emit
fallbackand move to the next model infallbackModels, which gets the same two attempts. - Out of models: the last error is thrown, an
errorevent is emitted, andrun()rejects.
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);
},
});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 retryA 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
| 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
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
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
- 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: a smaller model may refuse a history the first one accepted.
A fallback is per turn, not per run
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.