Get an API key
Agent engine

Fallback and retry

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.

2 attempts per model1.5 s betweenfallbackModels in order
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

The 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.
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 retry

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

KindValues
HTTP status408 · 429 · 500 · 502 · 503 · 504
Error codeunreachable · 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

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

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.