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")