Implementing exponential backoff with jitter for OpenAI API
Nina PatelJan 8, 2025
Proper retry logic is essential for production OpenAI API usage. Here's my battle-tested implementation:
import time
import random
from openai import OpenAI, RateLimitError, APITimeoutErrordef call_with_retry(func, max_retries=5):
for attempt in range(max_retries):
try:
return func()
except RateLimitError:
wait = min(2 attempt + random.uniform(0, 1), 60)
print(f"Rate limited. Waiting {wait:.1f}s...")
time.sleep(wait)
except APITimeoutError:
wait = min(2 attempt, 30)
time.sleep(wait)
raise Exception("Max retries exceeded")
Key points:
4.7k views22 replies63 likesSolved
Log in to reply to this topic.