A retry loop can be small at every layer and still be large at the network boundary. For a multi-model API application, the important limit is attempts per logical user request, not attempts per SDK call or per model.
The error-handling article behind this example uses two model routes, up to three attempts on each, and explicitly sets max_retries=0 in its Python client. That last setting is easy to overlook: it keeps the SDK from adding another retry loop underneath the application policy.
Count what can actually leave the application
For the article’s loop, assuming every call fails with a retryable error:
| Retry arrangement | Maximum outbound attempts |
|---|---|
| Two routes × three application attempts, no inner retries | 6 |
| Same loop, with an inner layer allowing up to three transport attempts per call | 18 |
The second row is a hypothetical configuration, not a claim about an SDK default. More generally, the bound is 2 × 3 × s, where s includes the inner layer’s initial attempt. These are attempt counts, not billing estimates or measurements of provider-internal work. Success or a non-retryable error can stop the chain earlier.
Also, range(3) means three attempts: the initial call plus at most two retries. Calling that “three retries” makes a configuration review ambiguous before any traffic runs.
Give one layer the budget
My recommendation is to let the application policy own both a global attempt cap and a total deadline. If it owns retries, disable automatic retries beneath it where supported and check for retrying middleware too.
A useful implementation checklist:
- Create one attempt counter and one monotonic deadline at the start of the logical request. Neither resets when the model changes.
- Count the initial call, retries, and fallback calls against the same global cap. Per-route caps can still exist, but cannot expand it.
- Before a call or backoff, inspect the remaining time. Stop when the cap or deadline is exhausted; do not sleep past the deadline and then start a fallback.
- Constrain each transport timeout to the remaining budget. Verify the client’s timeout and cancellation behavior rather than assuming a per-call timeout enforces an end-to-end deadline.
- Use bounded, jittered backoff and control concurrency. An attempt cap alone does not prevent many requests from retrying together.
The article recommends a shared total latency budget, but its short Python loop does not implement a total deadline. Treat that loop as a policy illustration, not a complete deadline-enforcing wrapper.
A budget is also not permission to retry every failure. Use the API’s documented classifier. The source stops on authentication/path errors and invalid-request codes; fallback requires a compatible input/output contract. After partial streaming or a tool side effect, stop blind replay and use an explicit recovery policy instead.
Prove the stop conditions
Before rollout, inject failures and inspect the transport boundary—not just the number of times the outer function ran:
- Exhaust attempts: make every route return a retryable failure. Verify the global cap and route order, with no hidden SDK attempts.
- Exhaust time first: make a call slow or leave too little time for backoff. Verify that the next route does not start after the deadline.
- Fail permanently: inject a documented non-retryable error. Verify there is no retry or model switch.
- Reach a replay boundary: simulate a partial response or completed side effect. Verify that the normal fallback loop cannot silently restart the whole operation.
Log a logical request identifier, route, attempt number, status/error code, elapsed time, remaining budget, fallback reason, and final stop reason. Keep credentials and request bodies out of those logs. These are proposed checks, not reported test results.
If fallback usage keeps rising, escalate with sanitized request IDs and attempt traces rather than simply increasing the cap. The operational question is whether the intended recovery policy is running—or several small policies are multiplying each other.