Skip to content

Inference retries

Last updated: 2026-11-27T01:08:49-03:00 (UTC-3)

A transient 429 or 503 does not have to become an error for the client. For chat, Messages, Responses, embeddings, moderations and rerank, the gateway can repeat the call a few times on the same target, with a growing, randomized wait, respecting the provider’s Retry-After. Flag: inference_retries.

features:
inference_retries: true
models:
- id: assistant
capabilities: [chat, messages, responses]
retry:
max_attempts: 2 # per target, counting the first send
initial_backoff: 100ms
max_backoff: 2s
targets:
- {provider: openai, model: gpt-4.1-mini}
- {provider: gemini, model: gemini-2.5-flash}
Field Values
max_attempts 2 or 3 sends per target in total, including the first one. Required
initial_backoff 10 ms to 1 s. Required
max_backoff between initial_backoff and 10 s. Required

The policy applies to aliases with at least one target and one of the capabilities chat, messages, responses, embeddings, moderations or rerank (an alias without capabilities serves chat). In the console: Model catalog → Edit routing → Bounded inference retries, with versioning, audit and revert to YAML. Without retry, nothing changes. The API is PUT /api/v1/admin/models/{id} with the same retry object.

On an alias with several targets, each target gets its own max_attempts before the alias falls back to the next target, in the alias order. With two targets and max_attempts: 2, a request sends at most four times: two to the first target, two to the second.

Why this order: a 429 or 503 is usually a short, local condition of one destination (a rate window, a busy replica). Retrying it first keeps traffic on the target the operator put first, usually the cheaper or preferred one, and only a target that stays unavailable passes the request on. A failure that retrying cannot fix moves on at once:

  • A 500 and other 5xx outside the list below are not retried; the alias falls back to the next target, as it does without a policy.
  • A refused admission (no destination slot) or an open circuit breaker does not wait for another attempt; the alias falls back, or answers 503 on the last target.
  • A target that cannot express the request in its protocol is skipped without any send (see fallback across translation refusals).
  • Retries only on: transport failure, 429, 502, 503 and 504. A 500 is not retried.
  • Wait: exponential with full jitter. The ceiling doubles on each attempt up to max_backoff, and the actual wait is a random value between zero and that ceiling.
  • Retry-After: when the provider sends it, it becomes the minimum wait (seconds or an HTTP date). If it is invalid or larger than max_backoff, the gateway does not retry that target and moves on as if the attempts were spent: the next target, or the original response, Retry-After included, on the last one.
  • Total deadline: the configured operation deadline covers the whole call, including waits and response consumption. models[].request_timeout overrides network.request_timeout; the compiled network default is two minutes. A wait that would end after the deadline is not taken; the gateway keeps the answer it has. Canceling the request interrupts the wait (499).
  • Attempt timeout: with attempt_timeouts on, the alias’s attempt_timeout bounds how long each attempt may wait for the provider to start answering. An attempt it ends is not repeated on the same target: the next target is tried, or the client gets 504.
  • Capacity and circuit breaker: the target’s slot is released during the wait and requested again on each attempt; the circuit breaker can block an attempt.
  • Tracing: each send has its own span and an upstream_attempt log, with the same request ID. Under a policy the log record also carries attempt (1, 2, 3) and max_attempts.

A send is repeated only while nothing has reached the client.

  • JSON answers. The decision is made on the provider’s status line, before any header or byte is sent to the client.
  • Streams. With a policy, the gateway holds a streamed answer until its first byte before it starts the client’s stream, while another send (a retry or a next target) remains. A provider whose stream closes or fails before that first byte is retried like a transport failure. Once the first byte has been relayed the stream belongs to that send: a later break is reported to the client as an interrupted stream and is never repeated. The client’s response headers wait for that first byte; without a policy streams are relayed at once, as before.
  • Translated streams. For a target the gateway translates (Anthropic, Gemini, Bedrock, Cohere, OCI, or an OpenAI target with api: responses, or api: auto when it chose Responses), a provider stream that breaks before its first event becomes an in-band error event of the translated stream, which is the first byte: it is relayed and not retried. A failure before the provider’s response headers is still retried.
  • Stream clocks. A stream timeout that fires (first_byte, idle, max_duration) is not retried on the same target: the provider accepted the request and was still working on it, so a second send would pay for the same generation twice. It still falls back to the next target, as without a policy.
  • Answers already served. A body the gateway could not read or translate after the provider answered, and a buffered JSON answer to a stream request (provider_did_not_stream), are not retried: the provider did the work.

gemini_retries (off by default; requires inference_retries) lets an alias’s retry policy also repeat native Gemini POST /v1beta/models/{model}:generateContent and :streamGenerateContent (T168), with the same attempts, backoff and Retry-After floor: a retryable status (429, 502, 503, 504) or a transport failure is repeated on the same target, before the next target, and only before any byte reaches the client. A request with cachedContent or a provider-hosted tool is sent once per target. The flag also gates the translated generateContent ingress (a generate_content alias served by another provider). The console offers the policy on a generate_content alias only with the flag on.

Each case is something a second send could duplicate or break.

Request Retried Why
Chat Completions, with or without function tools Yes The provider keeps no conversation state; the client resends the history and runs its own tools
Chat with a hosted tool (a tools[].type other than function or custom, or web_search_options) No The tool runs at the provider; its effect or charge could happen twice
Messages Yes Same as chat
Messages with container, mcp_servers or a server tool (any tools[].type other than custom) No Code execution, remote MCP calls and server tools act at the provider
Responses, stateless Yes Same as chat
Responses with store: true Yes The client only ever receives the ID of the send that succeeded. A failed send may still leave a stored response at the provider (for example after a transport failure), but nothing references it
Responses with previous_response_id or conversation No The state lives at one provider; the continuation stays pinned there
Responses with background, a stored prompt, an item_reference input, a reasoning item without encrypted_content, a file_id content part or a hosted tool (code interpreter containers, file or web search, MCP, image generation, computer use) No Each continues or creates provider state, reads a stored object, or acts from the provider
Embeddings, moderations, rerank with stream, tools, tool_choice, functions, function_call, previous_response_id, conversation or background No Unknown extensions that could select streams, tools or state never inherit permission to replay
Native Gemini :generateContent and :streamGenerateContent, with gemini_retries Yes Stateless like chat, with or without client function declarations (tools[].functionDeclarations); before the first byte only
Gemini with cachedContent (or cached_content) or a hosted tool (googleSearch, codeExecution, urlContext, …) No The cached context lives at one provider; hosted tools act from it
Gemini without gemini_retries, decisions, MCP tools, audio, image, video No Not covered by this policy

The same rules decide Responses fallback: a request that is not retried is not sent to another target either.

  • Billing. Repeating a POST can lead to a new charge at the provider, for example when a transport failure happened after the provider started the work. Turn it on only where that is acceptable.
  • Estimated cost. The reported usage comes from the last response; when there was more than one send, valuation marks the cost as unknown. The receipt represents the client’s request, not each send.
  • Budgets and TPM. A quota reservation covers one send to one target. When a budget or TPM policy covers the alias, a request to an alias with a retry policy is refused with 422 before any provider call, as one to an alias with more than one target is.
  • At most max_attempts × targets sends per request, all inside the request deadline.
  • The decision is per process; retries are not coordinated across replicas.
  • Turning the flag off stops retries on new requests; requests already admitted keep the configuration they were admitted with.
  • The single-target rule is gone: a retry policy is valid on an alias with any number of targets, and on chat, messages and responses aliases. Existing embeddings, moderations and rerank policies behave as before on a single target, with two refinements: a transport-failure retry now also needs its wait to fit the deadline, and an answer the gateway received but could not read (an adapter’s normalization error) is no longer sent again.
  • An alias with chat that already had no retry is unchanged. Adding retry to a chat alias delays the client’s stream headers until the provider’s first byte.

The parameter registry shares the existing retry bounds with Go validation and console controls. All three members are required when a retry policy is configured; omitting the policy adds no retries. The form’s initial values (2 total attempts, 100ms initial backoff and 2s maximum) are creation presets, not backend defaults. Maximum backoff must also be at least the configured initial backoff.

m7gtwcontact@m7gtw.com