OpenAI 429: rate_limit_exceeded vs insufficient_quota
An HTTP 429 Too Many Requests from OpenAI means one of two completely different things: you hit a temporary per-minute speed limit (rate_limit_exceeded), or your billing balance / monthly spend cap is exhausted (insufficient_quota). Retrying fixes the first and wastes time on the second.
Quick answer
Inspect error.code (or error.type) in the JSON response body:
rate_limit_exceeded(RPM / TPM / RPD limit): retry with exponential backoff + jitter, honoringretry-after/x-ratelimit-reset-*headers, and reducemax_completion_tokens/max_output_tokensso you do not over-reserve TPM.insufficient_quota("You exceeded your current quota"): retrying will never succeed. Check prepaid credit balance, organization monthly budget cap, or token quota limit.
Tell the Two 429 Errors Apart
Both return HTTP status 429, so checking only status_code == 429 will trap your retry loop on a billing error.
| Field / Signal | rate_limit_exceeded (Temporary) | insufficient_quota (Hard stop) |
|---|---|---|
error.code | rate_limit_exceeded | insufficient_quota |
error.type | requests or tokens | insufficient_quota |
| Message snippet | Rate limit reached for ... Limit: ..., Used: ..., Requested: ... | You exceeded your current quota, please check your plan and billing details. |
| Should retry? | Yes, after backoff / Retry-After | No, stop immediately and alert billing |
Example rate_limit_exceeded payload:
{
"error": {
"message": "Rate limit reached for gpt-6-sol in organization org-xxx on tokens per min (TPM): Limit 30000, Used 28400, Requested 4200. Please try again in 5.2s.",
"type": "tokens",
"param": null,
"code": "rate_limit_exceeded"
}
}Example insufficient_quota payload:
{
"error": {
"message": "You exceeded your current quota, please check your plan and billing details.",
"type": "insufficient_quota",
"param": null,
"code": "insufficient_quota"
}
}Fix 1: Branch on error.code and Honor Retry-After
Only retry when code == "rate_limit_exceeded". Read retry-after (or retry-after-ms) from response headers when present, and add random jitter to avoid thundering herds across workers.
import random
import time
from openai import OpenAI, RateLimitError
client = OpenAI()
def call_with_backoff(messages, model="gpt-6-sol", max_attempts=5):
for attempt in range(max_attempts):
try:
return client.chat.completions.create(
model=model,
messages=messages,
max_completion_tokens=1024,
)
except RateLimitError as exc:
code = getattr(exc, "code", None)
if code == "insufficient_quota":
raise RuntimeError("Billing/quota exhausted; do not retry.") from exc
if attempt == max_attempts - 1:
raise
retry_after = exc.response.headers.get("retry-after") if exc.response else None
delay = float(retry_after) if retry_after else min(2 ** attempt, 30)
time.sleep(delay + random.uniform(0.1, 0.5))Fix 2: Stop Over-Reserving TPM with Huge Output Caps
OpenAI enforces TPM (tokens per minute) up front using input_tokens + max_completion_tokens (or max_output_tokens on the Responses API). If you leave max_completion_tokens at 32768 for a short JSON classifier that only outputs 80 tokens, every request reserves 32K TPM and triggers 429 prematurely.
- Set
max_completion_tokens/max_output_tokensclose to the realistic upper bound for that task. - Use
x-ratelimit-remaining-requests,x-ratelimit-remaining-tokens,x-ratelimit-reset-requests, andx-ratelimit-reset-tokensresponse headers to pace batch jobs before hitting 429.
Fix 3: Resolve insufficient_quota
If error.code is insufficient_quota:
- Confirm you have prepaid API credits (a ChatGPT Plus/Pro web subscription does not include API credits).
- Check your organization hard monthly budget limit and project-level spend caps.
- Verify your process is not accidentally reading an old
OPENAI_API_KEYfrom the shell environment (see OpenAI 401 invalid_api_key). - If you are calling through a gateway, also see 402 / insufficient_quota billing errors.
Checking via an API Gateway
When routing through byesu, you can check real-time route health on the status page and verify both account balance and per-token quota caps in the console if a 429 / quota error repeats.