Skip to main content
Use Cases
  • Surviving transient provider errors without surfacing failures to end users.
  • Automatic failover to a backup provider when the primary is degraded or rate-limited.
  • Absorbing short rate-limit bursts without manual intervention or custom retry logic.
  • Meeting availability SLAs on production features without adding retry code to every service.

Retries

Retry failed requests automatically with exponential backoff. Configure which HTTP error codes trigger retries and how many attempts to make.

Fallbacks

Route to a different model when the primary fails. Define a fallback chain across providers for high availability.

Retries

Automatically retry failed requests with exponential backoff.

Quick Start

The OpenAI TypeScript SDK does not declare retry or fallbacks in its request types, and it has no extra_body option. Build the request as a variable, as the samples do, so TypeScript accepts the extra fields, then pass it to create. The SDK serializes the whole body, so the gateway receives them.

Configuration

When retry is omitted, the gateway retries twice on the default codes. A retry object without count sends 0, so it makes exactly one call per model. An agent’s model.retry.count accepts 0 to 5. A retry object without count sends 0; omitting model.retry uses the gateway default of 2. See Agent model configuration. retry, fallbacks and timeout are top-level fields on responses, chat completions, embeddings, rerank, and decisions and classify. On /v3/anthropic/v1/messages, retry and fallbacks do not configure the gateway: retries stay at the default of two on the default codes, and fallbacks come from Routing Rules. timeout behaves as on the other endpoints.

Streaming

The gateway retries and falls back only while the stream has produced no output. After output arrives (text, reasoning, or a tool call), a provider failure ends the stream: the gateway does not retry it and does not try a fallback model. Responses. When every attempt fails, the stream ends with a response.failed event whose response.error.code and response.error.message describe the failure; no response.completed event follows. Before output arrives, the gateway may retry or fall back before sending that event. After output has started, the event reports the failure that ended the stream. Chat Completions. If every attempt fails before the gateway writes its first chunk, the client receives an HTTP error status and a JSON error body, such as 408 on a timeout. After the first chunk, the stream ends with a data chunk holding an error object, followed by a [DONE] chunk:
The OpenAI SDK raises that error object as an APIError while iterating the stream. Protocol events do not count as output: response.created, response.in_progress, and chat chunks without content can be followed by a retry. A stream that stalls after output has started ends the same way when the stream idle timeout fires; see Timeout with streaming.

Error Codes

Retry Strategies

Backoff Algorithm

Exponential backoff with full jitter

Each backoff delay is drawn at random between zero and the attempt’s ceiling, so retries from concurrent requests do not synchronize.
  • Attempt 1: up to 1s.
  • Attempt 2: up to 2s.
  • Attempt 3: up to 4s.
  • Attempt 4: up to 8s.
  • Attempt 5: up to 16s.
Maximum total delay: ~31 seconds for 5 retries A provider Retry-After replaces the backoff, and the 31 second ceiling does not cap it: the wait is capped at 60 seconds instead. When that wait is longer than the time left before the request deadline, the gateway stops retrying and, if a fallback model is configured, tries it next; with no fallback, the provider error is returned. See Provider rate limits.

Code examples

Best Practices

Production recommendations

Follow this advice for a solid production setup:
  • Use count: 2-3 for balance of reliability and speed.
  • Always include 429 (rate limits) in on_codes.
  • Monitor retry rates to detect systemic issues.
  • Implement a circuit breaker for persistent failures.

Error handling

Troubleshooting

High retry rates
  • Check whether rate limits are being hit frequently.
  • Verify API keys have sufficient quotas.
  • Monitor provider status pages for outages.
Slow response times
  • Reduce retry count for latency-sensitive apps.
  • Use shorter timeout values with retries.
  • Consider fallbacks for faster alternatives.
Still getting errors
  • Check if error codes are in on_codes list.
  • Verify retry count isn’t exhausted.
  • Review provider-specific error documentation.

Monitoring

Track these retry metrics:

Limitations

  • Increased latency: Retries add backoff delay (up to 31s for 5 retries), and a provider Retry-After adds up to 60 seconds per retry.
  • Cost implications: Failed requests may still incur charges.
  • Rate limit consumption: Retries run inside the gateway on one incoming request, so a retry counts once against platform rate limits and budgets, while every attempt counts against the provider’s own rate limits.
  • Limited retries: Maximum 5 retries per model after the initial attempt (up to 6 calls) to prevent excessive delays.
  • Non-retryable errors: Only the codes listed in on_codes are retried, plus 408, which the gateway adds to every retry configuration because each model call carries a timeout. Most 4xx client errors, such as 400 and 403, are not retried; 429 is retried when it is listed in on_codes.

Advanced Usage

Environment-specific configs:
With other features:
Custom retry logic (client-side):

Fallbacks

Automatically switch to a different model when the primary fails.

Quick Start

Configuration

Set fallback_on_codes at the top level of the request. A custom list replaces the default codes. For example, include 400 alongside the defaults to fall back on bad-request errors:
JSON
Setting "fallback_on_codes": [400] alone limits fallback to 400; rate limits and server errors no longer trigger it. Omit the field or use an empty array to restore the defaults.

Trigger Conditions

By default, fallbacks activate on the errors below. Override the set with fallback_on_codes on a request or in a Fallback routing rule’s models_config. Supply up to 20 unique integers from 400 through 599. A matching routing rule replaces request-level fallback models and codes. For example, "fallback_on_codes": [429, 503] limits fallback to rate limits and service unavailability. Cancelled requests never fall back. Models missing a required capability are skipped regardless of the configured codes.

Best Practices

Use a maximum of 3 fallback models. Order them by preference or cost, and choose models with similar capabilities.

Code examples

Limitations

  • Response consistency: Different models may return varying output styles.
  • Parameter support: Not all providers support identical parameters.
  • Cost implications: Failed requests may still incur charges from the primary provider.
  • Latency impact: Sequential attempts add processing time.
  • Provider dependencies: Requires API keys for all fallback providers.