API Errors
OpenAI API Errors — What They Mean and How to Fix Them
OpenAI API errors are cryptic if you don't know what they mean. Here's every common error code, what's actually happening, and the exact fix.
429 — Rate limit exceeded
This means you've hit OpenAI's request rate limit for your tier. It's not a ban.
Immediate fix: Implement exponential backoff — wait 1s, retry. If it fails, wait 2s, then 4s, then 8s.
For API code:
```python
import time
def api_call_with_retry(fn, max_retries=5):
for attempt in range(max_retries):
try:
return fn()
except openai.RateLimitError:
wait = 2 ** attempt
time.sleep(wait)
raise Exception("Max retries exceeded")
```
Long-term fix: Request a rate limit increase at platform.openai.com/account/limits, reduce requests per minute, or implement a request queue.
401 — Invalid authentication
Your API key is wrong, expired, or not being sent correctly.
Check in order:
- Is your API key still valid? Check at platform.openai.com/api-keys
- Is the key set as an environment variable or being hardcoded? Never hardcode in client-side code.
- Is it being sent as `Authorization: Bearer sk-...` in the header?
- Did the key get accidentally committed to GitHub? Rotate it immediately at platform.openai.com/api-keys.
400 — Bad request
Your request is malformed. Common causes:
- `max_tokens` exceeds the model's context limit
- Invalid `model` name (use `gpt-4o`, `gpt-4o-mini`, etc. — check the current list)
- Messages array is empty or malformed
- Invalid JSON in your request body
Check the error message — it usually says exactly which parameter is wrong.
500 / 503 — Server errors
OpenAI's servers are having an issue. This is not your fault.
Check status.openai.com for a reported incident. If there's no incident, retry after 30–60 seconds. Implement the same exponential backoff as for 429s. If errors persist for more than 30 minutes with no status update, report it in the OpenAI developer community.
402 — Payment required / insufficient quota
Your account has run out of API credits.
Go to platform.openai.com/account/billing and check your usage and credit balance. Add credits or set up auto-recharge. Free tier accounts have a $5 credit that expires — this may have expired. Note: ChatGPT Plus does not include API credits. The API is billed separately.
Frequently asked questions
How do I check my OpenAI API rate limits?
Go to platform.openai.com/account/limits. Limits are per model and per tier — paid accounts have higher limits than free accounts.
My API calls work in testing but fail in production
This is almost always a rate limit issue at scale. Your test volume is low; production volume triggers limits. Implement request queuing and retry logic before going to production.
How do I request a higher rate limit?
Go to platform.openai.com/account/limits and click "Request increase." You'll need to describe your use case and expected volume. Approvals typically take 1–3 business days.