Skip to main content

Overview

The OpenAI Python SDK automatically retries certain failed requests using exponential backoff. This improves reliability by handling transient errors without manual intervention. Default retry behavior:
  • Max retries: 2 attempts (3 total requests including the initial attempt)
  • Retry conditions: Connection errors, timeouts, rate limits, and server errors
  • Backoff strategy: Exponential backoff with jitter

Automatic Retries

The SDK automatically retries these error conditions:
  • Connection errors - Network connectivity problems
  • 408 Request Timeout - Request took too long
  • 409 Conflict - Resource conflict (lock timeout)
  • 429 Rate Limit - Too many requests
  • 5xx Server Errors - Internal server errors

Configuring Retries

Client-Level Configuration

Set the default retry behavior for all requests:
Setting max_retries to None is not allowed. Use 0 to disable retries or a positive integer to set a limit.

Per-Request Configuration

Override retry settings for individual requests:

Backoff Strategy

The SDK uses exponential backoff with jitter to space out retry attempts:

Algorithm

  1. Initial delay: 0.5 seconds
  2. Exponential increase: Delay doubles with each retry
  3. Maximum delay: 8 seconds
  4. Jitter: ±25% random variation

Calculation

Example Timeline

Retry-After Header

The SDK respects the Retry-After header from rate limit responses:
The SDK also supports the non-standard retry-after-ms header for millisecond precision. Priority:
  1. retry-after-ms header (milliseconds)
  2. Retry-After header (seconds or HTTP date)
  3. Exponential backoff algorithm
If the Retry-After value is reasonable (≤60 seconds), it takes precedence over the exponential backoff calculation.

Server-Controlled Retries

The API can explicitly control retry behavior using the x-should-retry header:
  • x-should-retry: true - Always retry, even if not normally retryable
  • x-should-retry: false - Never retry, even if normally retryable

Idempotency

The SDK automatically adds idempotency headers to retried requests (except GET requests):
The idempotency key is generated once per request and reused across all retry attempts.

Retry Headers

The SDK includes metadata headers with each request:
  • x-stainless-retry-count - Number of retries taken (0 for first attempt)
  • x-stainless-read-timeout - Read timeout in seconds

Async Retries

Retries work identically with AsyncOpenAI, using async sleep:

Custom Retry Logic

For custom retry logic, catch exceptions and implement your own strategy:

Monitoring Retries

Enable logging to see retry attempts:

Best Practices

  • Don’t disable retries unless you have a specific reason - they handle transient errors automatically
  • Use reasonable max_retries - Very high values can cause long delays
  • Let the SDK handle retries - The built-in strategy is well-tested
  • Monitor rate limits - Adjust your request rate if you frequently hit 429 errors
  • Consider exponential backoff for custom retry logic

Error Scenarios

Retried Automatically

Not Retried