Errors and retries
Errors come back in the dialect of the endpoint you called, so the official SDKs raise their usual exceptions. The code string inside is the same in every dialect; branch on it, not on the message.
Envelopes
Section titled “Envelopes”OpenAI endpoints (chat, responses, embeddings, images, audio, models, generation):
{ "error": { "message": "model \"gpt-9\" does not exist", "type": "invalid_request_error", "param": "model", "code": "model_not_found" }}Anthropic /v1/messages (the code is not part of Anthropic’s shape; branch on error.type and the HTTP status):
{ "type": "error", "error": { "type": "billing_error", "message": "insufficient balance" } }Gemini:
{ "error": { "code": 429, "message": "Insufficient balance. Please top up your account.", "status": "RESOURCE_EXHAUSTED" } }How the gateway’s error classes map to each dialect:
| Class | HTTP | OpenAI type |
Anthropic type |
Gemini status |
|---|---|---|---|---|
| Invalid request | 400 | invalid_request_error |
invalid_request_error |
INVALID_ARGUMENT |
| Authentication | 401 | authentication_error |
authentication_error |
UNAUTHENTICATED |
| Insufficient balance | 402 (Gemini: 429) | insufficient_quota |
billing_error |
RESOURCE_EXHAUSTED |
| Permission | 403 | permission_error |
permission_error |
PERMISSION_DENIED |
| Not found | 404 | invalid_request_error |
not_found_error |
NOT_FOUND |
| Too large | 413 | invalid_request_error |
request_too_large |
INVALID_ARGUMENT |
| Rate limited | 429 | rate_limit_error |
rate_limit_error |
RESOURCE_EXHAUSTED |
| Provider failed | 502 | server_error |
api_error |
UNAVAILABLE |
| No capacity, overloaded | 503 (Anthropic overloaded: 529) | server_error |
overloaded_error |
UNAVAILABLE |
| Provider timed out | 504 | server_error |
api_error |
DEADLINE_EXCEEDED |
| Internal | 500 | server_error |
api_error |
INTERNAL |
Request problems
Section titled “Request problems”Fix the request; do not retry unchanged. Nothing is charged.
| HTTP | code |
Meaning |
|---|---|---|
| 400 | invalid_json, invalid_request_body, invalid_request, invalid_body |
The body is not valid JSON, is not an object, or could not be read or decoded. |
| 400 | missing_required_parameter, invalid_type, invalid_value, unsupported_parameter |
A field is missing, has the wrong type or value, or is not supported on this endpoint. param names the field. |
| 400 | duplicate_key |
The JSON body repeats a key. param is the path. |
| 400 | body_read_timeout |
The body did not arrive within the read time (2 minutes by default). |
| 400 | max_tokens_too_large |
max_tokens is above the deployment’s cap (262,144 by default). |
| 400 | unsupported_capability |
The model does not support something the request uses. |
| 400 | unsupported_operation |
The model’s kind does not match the endpoint. |
| 400 | provider_not_found |
The provider named in model:provider or provider.only does not offer this model. |
| 400 | context_length_exceeded, content_filter, other provider-side 4xx |
The provider refused the request itself. The provider’s message is passed on. Nothing is charged. |
| 404 | model_not_found |
No such model or alias. GET /v1/models lists what you can call. |
| 404 | previous_response_not_found |
The previous_response_id is unknown to your organisation or has expired. |
| 404 | generation_not_found |
/v1/generation has no settled request with that id for your organisation. Retry briefly, see cost. |
| 413 | request_too_large |
The body is over the size limit. |
Access
Section titled “Access”Do not retry; see Authentication.
| HTTP | code |
|---|---|
| 401 | missing_api_key, invalid_api_key |
| 403 | org_frozen, ip_not_allowed, model_not_allowed |
Do not retry until the cause is fixed.
| HTTP | code |
Meaning |
|---|---|---|
| 402 | insufficient_balance |
The spendable balance cannot cover the worst case of this request (see billing). Top up in the console, lower max_tokens, or turn on auto recharge. |
| 402 | key_spend_limit_exceeded |
The key reached its spend limit. |
| 402 | daily_spend_cap_exceeded |
The organisation reached the daily spend cap of its account level. |
Temporary failures
Section titled “Temporary failures”Retry with backoff.
| HTTP | code |
Meaning |
|---|---|---|
| 429 | rate_limit_exceeded |
Your organisation or key is over its limit. Retry-After says when. See rate limits. |
| 429 | upstream_rate_limited |
Every provider for the model is rate limited or at its budget. Retry-After is set when a provider gave one. |
| 502 | upstream_error, upstream_invalid_response, upstream_stream_broken |
The providers failed or answered with something unusable, on every attempt. |
| 503 | no_capacity |
No provider can serve the model right now (all disabled, or circuit-broken after failures). |
| 503 (Anthropic: 529) | upstream_overloaded |
The providers are overloaded. |
| 503 | auth_unavailable, billing_unavailable |
The gateway could not check the key or hold balance right now. The request was refused rather than served unbilled. |
| 504 | upstream_timeout |
The provider did not answer in time. |
| 500 | internal_error |
A gateway fault. Retry; if it persists, report the X-Request-Id. |
What the gateway retries for you
Section titled “What the gateway retries for you”Before it sends you the first byte of a response, the gateway retries a failed provider attempt on the next provider (and, for providers with several keys, the next key), up to 3 attempts per request by default. What it retries: timeouts, connection failures, 5xx, provider-side rate limits and overloads, broken streams, and a provider rejecting the gateway’s own credential. What it does not retry: errors caused by your request content (they would fail again), and anything after the response has started.
So an error you receive has already been through those retries. Do not stack aggressive retries on top: use a few attempts with exponential backoff and jitter, and obey Retry-After.
Only the attempt whose answer reaches you is billed. X-Gateway-Attempts and retries in /v1/generation tell you how many attempts it took.
What to retry in your client
Section titled “What to retry in your client”| Situation | Action |
|---|---|
| 429 | Wait Retry-After seconds if present, otherwise back off exponentially. |
| 500, 502, 503, 504, 529 | Retry up to a few times with exponential backoff and jitter. |
| 400, 401, 402, 403, 404, 413 | Do not retry unchanged. |
| Connection reset or timeout on your side | Retry, but see below. |
| A stream that ended without its final event | The text you received is real and was billed. Decide whether to continue from it or ask again. |
There are no idempotency keys. If your client gives up on a request (a timeout, a closed connection), the gateway may still complete it and bill it; see Streaming. A retry after such a timeout is a second request with its own charge. Set client timeouts above your longest expected generation, and prefer streaming for long ones.
The official SDKs already retry connection errors, 408, 409, 429 and 5xx (twice by default) and honour Retry-After. Raise the count where it helps:
import osimport openaifrom openai import OpenAI
client = OpenAI( base_url="https://api.test.whocare.xyz/v1", api_key=os.environ["GATEWAY_API_KEY"], max_retries=0, # show the error instead of retrying)try: client.chat.completions.create(model="no-such-model", messages=[{"role": "user", "content": "hi"}])except openai.NotFoundError as e: print(e.status_code, e.code, e.param)import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.test.whocare.xyz/v1", apiKey: process.env.GATEWAY_API_KEY, maxRetries: 0, // show the error instead of retrying});try { await client.chat.completions.create({ model: "no-such-model", messages: [{ role: "user", content: "hi" }] });} catch (e) { console.log(e.status, e.code, e.param);}# The same error on the wire, with the request id header.curl -i https://api.test.whocare.xyz/v1/chat/completions \ -H "Authorization: Bearer $GATEWAY_API_KEY" \ -H "Content-Type: application/json" \ -d '{"model": "no-such-model", "messages": [{"role": "user", "content": "hi"}]}'# expect: model_not_found