Skip to content

429 Too Many Requests: rate_limit_exceeded

Cause: You sent more requests or tokens than your rate limit allows in the current window, so the API refused this one.

Copy-paste fix

Retry in one place only, wait for Retry-After when the API sends it, otherwise back off exponentially with random jitter, and give up after a few tries.

import random, time
import openai

client = openai.OpenAI(max_retries=0)  # retry in one layer only: this one

NO_RETRY = {"insufficient_quota", "credit_balance_exhausted",
            "organization_spend_limit_exceeded", "project_spend_limit_exceeded",
            "api_key_spend_limit_exceeded", "organization_usage_limit_exceeded"}

def create_with_backoff(max_attempts=5, base=1.0, cap=30.0, **kwargs):
    for attempt in range(max_attempts):
        try:
            return client.chat.completions.create(**kwargs)
        except openai.RateLimitError as e:
            if e.code in NO_RETRY or e.type == "insufficient_quota":
                raise  # billing or quota: waiting will not fix it
            if attempt == max_attempts - 1:
                raise
            try:
                delay = float(e.response.headers.get("retry-after", ""))
            except ValueError:
                delay = random.uniform(0, min(cap, base * 2 ** attempt))  # full jitter
            time.sleep(delay)

See what each retry of this call costs, and compare models, on TokenCenter

What the error means

429 is the standard HTTP status for Too Many Requests. OpenAI's API spec defines the error code rate_limit_exceeded as “the request exceeds the available rate limit”, and its error guide shows the message as “Rate limit reached for requests”.

The same 429 status is also used for billing and spend limits (for example insufficient_quota or credit_balance_exhausted). OpenAI's guide says retrying those won't restore access, so the fix below stops on them instead of looping.

Checklist

  1. Turn off retries in the other layers (SDK, framework, gateway) so one 429 doesn't become many requests.
  2. Log the status, error code and Retry-After value on every 429.
  3. Split 429s into two groups: rate limits (wait and retry) and quota or spend limits (stop and alert).
  4. Limit concurrency with a queue instead of firing all requests at once.
  5. Keep a fallback model or provider for traffic that can't wait.
  6. Remember limits apply per organization or project, not per user, so other apps on the same key count too.

Related

Sources

All guides · All errors