AI API 429: rate limits, Retry-After, and backoff done right
HTTP 429 from the OpenAI API is not an outage; it is the rate limiter doing its job. The fix is not "retry harder" but reading what the response tells you and pacing accordingly. This guide follows OpenAI's rate limits documentation.
What the limiter measures
Rate limits use metrics like RPM (requests per minute), RPD (requests per day), TPM (tokens per minute) and TPD (tokens per day), and you can hit any of them first. OpenAI's own example: 20 requests of only 100 tokens each still exhausts a 20 RPM limit, even if you are far under your token cap.
Read the headers and the error
Responses carry headers that describe your budget: x-ratelimit-limit-requests and x-ratelimit-limit-tokens state the maximums, and remaining-headers tell you what is left. Most useful of all: Retry-After, the minimum number of seconds to wait before retrying a temporary rate-limit error, when present.
The documented handling for 429 and 503
- 429 (rate_limit_error, slow_down): your request rate increased too quickly. Follow
Retry-Afterwhen present, reduce your request rate, then increase it gradually. - 503 (service_unavailable_error): the model is temporarily overloaded. Follow
Retry-Afterwhen present, then retry; if the error continues, increase the delay between attempts. - No Retry-After? Increase the delay between retries and add a small random delay (this is the classic exponential-backoff-with-jitter pattern).
A retry loop that follows the docs
import time, random, requests
def call_api(payload):
delay = 1.0
for attempt in range(5):
r = requests.post(API_URL, json=payload, timeout=30)
if r.status_code == 200:
return r.json()
if r.status_code in (429, 503):
retry_after = r.headers.get("Retry-After")
wait = float(retry_after) if retry_after else delay + random.random()
time.sleep(wait)
delay *= 2 # back off for the next attempt
continue
r.raise_for_status()
raise RuntimeError("still rate limited after 5 attempts")
Important distinction
The docs are explicit: Retry-After applies to temporary rate limits and model overload. Quota, billing or other errors that require your action will not be fixed by retrying; read the error before looping.
If you keep hitting the ceiling
OpenAI's rule of thumb: once traffic reaches 1 million input tokens per minute, increase it by no more than 50% every 15 minutes. Persistent needs are a capacity conversation (usage tiers), not a retry problem.
Related: what is an AI agent? (Arabic) and calling APIs with requests (Arabic).
