A loop works at first, then fails with a rate-limit message.
Why this happens
Limits apply to requests and tokens over time. Retrying immediately makes the spike worse; a failed request can still count toward a limit, so the solution is to slow, queue, or reduce work.
Answer 1: Retry transient failures with exponential backoff and jitter
import random
import time
from openai import OpenAI, RateLimitError
client = OpenAI()
def create_with_backoff(max_retries=5):
for attempt in range(max_retries):
try:
return client.responses.create(
model="gpt-5",
input="Hi",
)
except RateLimitError:
if attempt == max_retries - 1:
raise
delay = min(30, 2 ** attempt) + random.uniform(0,
0.5)
time.sleep(delay)
response = create_with_backoff()
Use a bounded number of retries. Treat other 4xx errors as non-retryable unless the API documentation says otherwise.
