n4nAI

What happens when you hit a 429 on the OpenAI API

OpenAI API 429 error explained: triggers, rate limit mechanics, and production patterns for resilient LLM inference with code examples.

n4n Team4 min read799 words

Audio narration

Coming soon — every post will get a voice note here.

An OpenAI API 429 error explained in one line: it is the HTTP 429 Too Many Requests status returned when your client surpasses the request rate or token quota allocated to your account or organization. OpenAI sends it from the edge before any model computation occurs, signaling you to slow down rather than indicating a fault in the model service.

How OpenAI enforces rate limits

OpenAI applies limits on two axes: requests per minute (RPM) and tokens per minute (TPM). The exact thresholds depend on your usage tier, which scales with prior spend and account history. You do not negotiate these; they are assigned.

When you exceed either axis, the gateway rejects the call with a 429 and includes headers:

HTTP/1.1 429 Too Many Requests
Retry-After: 2
x-ratelimit-limit-requests: 3500
x-ratelimit-remaining-requests: 0
x-ratelimit-reset-requests: 1s

The Retry-After header is the authoritative signal. Ignore it and you will keep getting 429s.

Request vs token limits

A small request with many output tokens can trip the TPM limit while RPM stays low. For embedding calls, token counts dominate. For chat completions with large context, input tokens count too.

OpenAI calculates TPM across the whole request, not just completions. If you send 10k input tokens and max_tokens=2k, that 12k counts toward the window.

Why the OpenAI API 429 error explained matters in production

A 429 is not a rare edge case; it is the primary backpressure mechanism for shared GPU infrastructure. If you fire 100 concurrent requests from a batch job against a tier-1 RPM limit that is sized for steady traffic, you will hit it within seconds.

Unhandled, 429s cascade: your users see failures, your retry storms make the limit worse, and your incident dashboard lights up. Treating rate limits as a first-class concern separates toy integrations from production systems.

Burst vs sustained throughput

OpenAI limits are typically sliding or fixed windows per minute. A burst of 60 requests in one second might be fine if the limit is 60 RPM, but the next 59 seconds are starved. Designing for sustained throughput means pacing with a token bucket.

Concrete example: handling 429 in Python

Using the official OpenAI SDK, catch the RateLimitError and apply exponential backoff with jitter. Do not busy-loop.

import time
import random
from openai import OpenAI, RateLimitError

client = OpenAI()

def complete_with_backoff(messages, max_retries=5):
    for attempt in range(max_retries):
        try:
            return client.chat.completions.create(
                model="gpt-4o-mini",
                messages=messages
            )
        except RateLimitError as e:
            if attempt == max_retries - 1:
                raise
            # Respect Retry-After if present, else exponential
            retry_after = float(e.response.headers.get("Retry-After", 2 ** attempt))
            sleep = retry_after + random.uniform(0, 0.5)
            time.sleep(sleep)

This pattern cuts retry storms. The jitter prevents synchronized clients from hammering the reset.

Inspecting raw limits

If you call the REST endpoint directly, parse the x-ratelimit-* headers to build local admission control:

curl -i https://api.openai.com/v1/chat/completions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"hi"}]}'

Look for x-ratelimit-remaining-tokens. When it approaches zero, park your worker.

Common misconceptions about 429s

“A 429 means I’m throttled permanently”

No. It is per-window. After the reset, your quota returns. Persistent 429s indicate your design exceeds your tier, not a ban.

“I should retry immediately until it works”

That is the fastest way to get longer cooldowns. OpenAI does not penalize backoff; it expects it. Immediate retries amplify load precisely when the system asked you to stop.

“Only requests count, tokens are unlimited”

False. TPM limits are often the real constraint for long-context workloads. A single 128k context call can consume more than a thousand small calls.

“All models share one pool”

Limits are grouped. Some models share a tier, others have independent RPM/TPM. Check the dashboard; assuming uniformity causes surprise 429s when you shift models.

Comparing provider rate-limit behavior

The OpenAI API 429 error explained above is specific to OpenAI, but every hosted LLM provider implements similar throttling. Anthropic returns 429 with retry-after and uses different tier names. Google Vertex AI maps quotas to project-level metrics with separate error codes. Local inference has no 429 but you get queue saturation instead.

A multi-provider gateway changes the equation. For example, n4n.ai provides automatic fallback when a provider is rate-limited or degraded, so a 429 from one upstream becomes a silent reroute to another model rather than a client error. That only helps if your abstraction passes through provider headers and honors routing directives—which a good gateway does.

What to log

Regardless of provider, log the retry-after value, the model, and your estimated token cost. That data drives capacity planning.

{
  "event": "rate_limit",
  "provider": "openai",
  "model": "gpt-4o-mini",
  "retry_after": 1.5,
  "remaining_tokens": 0,
  "timestamp": "2024-05-12T10:22:01Z"
}

Production patterns that avoid 429s

Token bucket local limiter

Before calling the API, acquire a permit from a local token bucket sized to 80% of your RPM/TPM. This prevents edge rejection and smooths bursts.

import time

class TokenBucket:
    def __init__(self, rate, capacity):
        self.rate = rate
        self.capacity = capacity
        self.tokens = capacity
        self.last = time.time()

    def consume(self, n=1):
        now = time.time()
        self.tokens = min(self.capacity, self.tokens + (now - self.last) * self.rate)
        self.last = now
        if self.tokens >= n:
            self.tokens -= n
            return True
        return False

Queue workers

Push LLM tasks into a queue with a fixed number of consumers. The queue depth is your backpressure signal; the API limit is the throttle.

Use provider cache hints

OpenAI supports cache_control on certain models. Sending identical prefixes reduces token metering. A gateway that forwards these hints (as n4n.ai does) multiplies effective limit headroom without code changes.

Closing thoughts on the OpenAI API 429 error explained

Treat 429 as a normal, expected signal. Build local admission control, back off with jitter, and parse the headers. When you outgrow a single provider’s quota, a routing layer that fails over on 429 keeps your p99 stable. The error is not your enemy; ignoring it is.

Tagsopenai429-errorsrate-limitstroubleshooting

Written by

n4n Team

The team building n4n — a single OpenAI-compatible API in front of 240+ models, with automatic fallback, load balancing and pay-per-token metering.

More from n4n Team →

All rate limits & scaling comparison posts →