Implementing exponential backoff with jitter for OpenAI API

Nina Patel
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, APITimeoutError

def 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:

  • Always add jitter to prevent thundering herd
  • Cap the max wait time
  • Different strategies for rate limits vs timeouts
  • The official SDK has built-in retries — make sure you're not double-retrying!
  • 4.7k views22 replies63 likesSolved

    Log in to reply to this topic.