GA
Global API
DocsGet Started

Retry Strategy

Retry Strategy

Implement exponential backoff for reliability.

Python Example

import time

import openai

from openai import RateLimitError, APIError

def call_with_retry(client, model, messages, max_retries=5):

for attempt in range(max_retries):

try:

return client.chat.completions.create(

model=model,

messages=messages

)

except RateLimitError:

wait = 2 ** attempt

time.sleep(wait)

except APIError as e:

if e.status_code >= 500:

wait = 2 ** attempt

time.sleep(wait)

else:

raise

raise Exception("Max retries exceeded")

Best Practices

  • Use exponential backoff (not fixed delay)
  • Add jitter: `wait = 2^n + random(0, 1)`
  • Log failures for monitoring
  • Circuit breaker for persistent failures
  • Get Started Free →