Retries, cooldowns and fallbacks on model requests
How Kaeso 0.7 keeps model requests running when a provider drops a connection, returns 5xx or hits a rate limit — and why timeouts and 4xx refusals are never retried on the same model.
- Published
- Reading time
- 5 min
- Author
- Kaeso Team

When an application calls a model provider directly, a single dropped TCP connection, a brief 503 spike or a rate-limited API key surfaces straight to the user. Retrying blindly in the application is no better: retrying a request that timed out after 30 seconds can bill you twice while the upstream model is still generating the first reply.
Kaeso 0.7.0 separates transient glitches from lasting failures across three layers in the gateway: same-model retries, multi-key failover within a provider, and alias route fallbacks with shared Valkey cooldowns.


1. Same-model retries: only when retrying is safe01
By default, Kaeso sends a request once. In 0.7.0 you can enable automatic retries either for an entire project (under the project's Settings › Retries, from Never to 3) or on an individual model step inside an alias route (retries: 0..3).
Kaeso retries a model step only on short-lived transport and server failures before any stream output has been sent:
- Connection refused or reset before output
- Retried on the same model?
- Yes (up to
retries) - Why
- The provider never processed or finished the request
- HTTP
5xxor provider overloaded (529)- Retried on the same model?
- Yes (up to
retries) - Why
- Transient upstream server error
- Response with
Retry-After≤ 5 seconds on a retryable error- Retried on the same model?
- Yes, waiting the header's duration
- Why
- Short upstream backoff
Retry-After> 5 seconds- Retried on the same model?
- No — moves to next key or fallback
- Why
- Holding the caller open longer than 5 seconds stalls interactive apps
- Request or first-byte timeout (
504)- Retried on the same model?
- Never
- Why
- The provider may still be generating and billing the timed-out request
- Rate limit or quota (
429)- Retried on the same model?
- Never on the same key
- Why
- Retrying an exhausted rate window immediately fails again; Kaeso tries the project's next key or falls back
- Refused request (
400,413,422)- Retried on the same model?
- Never
- Why
- The same payload to the same model will be refused again
- Stream interrupted after output tokens started
- Retried on the same model?
- Never
- Why
- HTTP headers and partial tokens already reached the app; Kaeso emits an error event and logs
stream_interrupted
| Upstream failure | Retried on the same model? | Why |
|---|---|---|
| Connection refused or reset before output | Yes (up to retries) | The provider never processed or finished the request |
HTTP 5xx or provider overloaded (529) | Yes (up to retries) | Transient upstream server error |
Response with Retry-After ≤ 5 seconds on a retryable error | Yes, waiting the header's duration | Short upstream backoff |
Retry-After > 5 seconds | No — moves to next key or fallback | Holding the caller open longer than 5 seconds stalls interactive apps |
Request or first-byte timeout (504) | Never | The provider may still be generating and billing the timed-out request |
Rate limit or quota (429) | Never on the same key | Retrying an exhausted rate window immediately fails again; Kaeso tries the project's next key or falls back |
Refused request (400, 413, 422) | Never | The same payload to the same model will be refused again |
| Stream interrupted after output tokens started | Never | HTTP headers and partial tokens already reached the app; Kaeso emits an error event and logs stream_interrupted |
Before each retry, Kaeso waits between 0.25 and 2 seconds (or the provider's Retry-After when 5 seconds or less). All retries of a step share that step's timeout_seconds budget, so 3 retries cannot multiply your maximum wait time by four.
Stream failures before the first token now fall back
In 0.7.0, a streaming request counts as answered only once it carries output. If a provider opens a stream and immediately sends an error event before any output — for instance when an account hits its plan limit — Kaeso treats the step as failed and moves on to the next provider key or the step's fallback, just as it does for a non-streamed call.
2. Next provider key before next model02
When a project assigns several keys of the same provider (for example two OpenAI accounts or Azure deployments in two regions), key failover happens inside the model step before the route falls back to a different model:
- 01Each caller starts on its sticky weighted key so prompt caches stay warm.
- 02If that key is rate-limited (
429), out of credit, signed out or refused by the provider, Kaeso marks the key as waiting in Valkey and immediately tries the project's next assigned key for that provider. - 03Only when no assigned key for that provider can serve the request does the alias route move to its fallback model step.
3. Alias route fallbacks and shared Valkey cooldowns03
When a model step exhausts its retries (and any additional keys in the project's pool), the alias route checks the step's fall_back_on reasons:
unavailable(default): unreachable provider, timeout,5xx, overloaded, refused provider key or model not served.limited(default): provider429or a Kaeso limit/budget for that specific model.unsupported(default): the request uses images, PDFs, tools or a JSON schema that KPMR says the model does not support — skipped before sending a byte over the network.rejected(opt-in): the provider returned400or413(such as a prompt exceeding the first model's context window when your fallback model has a larger one).
Shared cooldowns across every gateway replica
When a step fails for a reason that lasts, testing it again on the very next request wastes latency. Kaeso places the failed step on a temporary cooldown in Valkey, shared across every gateway replica in your Docker Compose or Kubernetes deployment:
- Unreachable, timeout,
5xxor overloaded- Cooldown in Valkey
- 30 seconds
- Scope
- That model step
- Rate limit (
429)- Cooldown in Valkey
Retry-Afterduration, up to 5 minutes- Scope
- That key / model step
- Model not served to the project's key
- Cooldown in Valkey
- 5 minutes
- Scope
- That model and key
- Refused provider key or empty balance
- Cooldown in Valkey
- 5 minutes
- Scope
- Every model using that provider key
- Account plan limit reached (e.g. ChatGPT plan)
- Cooldown in Valkey
- 5 minutes, or as long as the provider states up to 1 hour
- Scope
- Every model using that account
| Failure kind | Cooldown in Valkey | Scope |
|---|---|---|
Unreachable, timeout, 5xx or overloaded | 30 seconds | That model step |
Rate limit (429) | Retry-After duration, up to 5 minutes | That key / model step |
| Model not served to the project's key | 5 minutes | That model and key |
| Refused provider key or empty balance | 5 minutes | Every model using that provider key |
| Account plan limit reached (e.g. ChatGPT plan) | 5 minutes, or as long as the provider states up to 1 hour | Every model using that account |
While a step is on cooldown — and a later fallback step in the route is ready to answer — incoming requests skip the broken step in sub-millisecond time. The first gateway replica that sees the model answer again clears the cooldown for the whole cluster.
Shorter timeouts from the app: kaeso-timeout04
An interactive UI often wants a faster fallback than a background job calling the same alias. Any request can send kaeso-timeout: 20 (in seconds) to cap how long each step waits for the first byte (or a complete non-streamed reply) before failing as unavailable and moving to the fallback. kaeso-timeout can only shorten a step's or model's configured timeout, never lengthen it.
How retries and fallbacks appear in headers, logs and metrics05
Every reply to an alias carries an RFC 9651 structured kaeso-route header showing each step reached or skipped, its status, its duration in milliseconds when it failed, and tries whenever a step sent the request more than once (via same-model retry or key failover):
kaeso-route: "route";model="anthropic/claude-sonnet-5-5";status=529;dur=842;tries=2, "route.fallback";model="openai/gpt-6-sol";status=200- Costs and budgets: Failed tries and failed steps cost
$0in Kaeso. A step that fails releases its budget reservation in Valkey before the fallback step checks its own limits (budgets and limits). - Request logs: The request log lists every tried or skipped step under Tried first, marks same-model retries as tried again, and lets you filter specifically for requests that Fell back.
- Prometheus metrics:
kaeso_upstream_retries_total{provider, outcome}(answeredvsgave_up) andkaeso_alias_fallbacks_total{provider, reason}track both layers on port 9100.
Next steps06
- Fallbacks, conditions and splits with alias routes: how conditions, weighted splits and route traces work.
- Provider key pools: spread traffic and fail over across several keys of one provider.
- Alias routes in the docs (opens in a new tab): full Platform API schema and route trace endpoint.
Product names in this article belong to their owners and are used only to name the software and services involved. Kaeso is not affiliated with or endorsed by them.
By Kaeso Team


