LLM API error codes explained: 401, 402, 429, 502, 503
Published
A failed request carries more information than 'something broke'. The gateway normalizes failures into a small set of status codes with stable meanings, then maps each one into the error names your client's protocol publishes. Here is what each code means, and what to do about it.
The codes you will actually see
- 401 authentication_error — the key is wrong, revoked, or malformed. Fix the key; retrying cannot help.
- 402 billing_error — the wallet could not cover the request's admission estimate. Top up, or reduce the request's size.
- 404 not_found_error — usually a model id that does not exist. Check it against GET /v1/models.
- 429 rate_limit_error — you hit a rate or concurrency limit. Back off, and honor any Retry-After header.
- 502 / 503 overloaded_error — the upstream pool for that model is degraded; failover and cooldown are already working the problem.
- 504 timeout_error — an upstream timed out. A single retry after backoff is reasonable.
402 in detail: the admission hold
Before a request runs, the gateway estimates its cost and decrements a per-organisation counter by that estimate. There is no per-request release: settlement later reprices the request from its actual usage, and the counter is overwritten from the ledger. Two effects are worth knowing. Your visible balance dips when requests start, not when they finish. And if many requests are in flight at once, an organisation can drift slightly past zero — the next request is declined, and the running ones are cut off at their next checkpoint rather than allowed to run on.
429: which limit you hit
Limits are enforced on request rate and concurrency, per organisation and per key. If 429s appear under load, the productive fix is almost always client-side parallelism — fewer simultaneous requests — not more retries. Retrying a concurrency rejection adds requests; it does not queue them.
502 and 503: failover is already running
A model id here is served by a pool of upstreams. When one fails, it enters cooldown and traffic shifts to the rest; a 503 that reaches your client means the pool as a whole could not serve the request at that moment. Retry with exponential backoff is appropriate — immediate tight retries are not.
Same failure, three dialects
Every failure is mapped into the error vocabulary each protocol publishes: Anthropic clients see billing_error and overloaded_error, Gemini clients see FAILED_PRECONDITION and UNAVAILABLE, OpenAI clients see the familiar type names. Whatever SDK you migrated from, your existing error handling keeps working.
A retry policy that survives production
- Retry only 429 (after Retry-After) and 5xx, with exponential backoff and jitter.
- Never retry 400, 401, 402 or 404 — the same request fails the same way, and you only buy latency.
- Cap retries per request — two or three — then surface the error to the caller.
- Log the error body, not just the status: the type and message carry the why.