Errors
Error shapes, every status code and what to do about each.
Errors are JSON with a matching HTTP status. The envelope follows the format of the endpoint you called. The type strings are the same on both.
/v1/chat/completions
{
"error": {
"message": "Missing or invalid API key.",
"type": "invalid_api_key",
"param": null,
"code": "invalid_api_key"
}
}code always equals type. param is always null.
/v1/messages
{
"type": "error",
"error": {
"type": "invalid_api_key",
"message": "Missing or invalid API key."
}
}Status codes
| Status | Type | Meaning | What to do |
|---|---|---|---|
| 400 | invalid_request_error | The body is not valid JSON, has no model or no messages, lacks max_tokens on /v1/messages, sends images to a model without vision, uses /v1/messages with a model that is chat-completions only, or carries parameters the model host rejected. | Fix the request. Do not retry it unchanged. |
| 401 | invalid_api_key | The key is missing, malformed or unknown. | Send the full key in the Authorization or x-api-key header. |
| 402 | insufficient_balance | The balance cannot cover the input plus a minimal reply. | Top up, send a shorter prompt, or wait for requests in progress to finish and release their holds. |
| 403 | account_inactive | The key was never activated, or the account is suspended. | Activate a new key by signing in with it once. A suspended account cannot be reactivated through the API. |
| 404 | model_not_found | The model id is not in the catalog. | Use an id from GET /v1/models, spelled exactly. |
| 413 | request_too_large | The request body is larger than 12 MB. | Send less history or smaller images. |
| 429 | rate_limit_error | The key reached its requests-per-minute limit or its concurrent request limit. | Wait the number of seconds in the Retry-After header, then retry. |
| 502 | upstream_error | The model returned a reply that could not be read. Nothing was charged. | Retry. |
| 503 | service_unavailable | No model host could take the request. Nothing was charged. | Retry after the number of seconds in the Retry-After header. |
Notes
- Checks run in this order: key, account status, body size, body shape, model id, endpoint and image support, rate limit, balance, then the model itself. The first failure is the one you see.
503comes withRetry-Afterof 5 or 10 seconds. It is always safe to retry: nothing was charged.403 account_inactiveon a brand new key means it was not activated. Sign in with it once at /login, or see Authentication.- Error messages are written by us. Raw errors from a model host are never passed through, so a 400 from the model says only that the parameters were rejected.
- Errors after a stream has started arrive as a final event, not as a status. See Streaming.
- Account endpoints under
/apiuse a simpler shape:{"error":"message"}.
Retrying
Retry 429, 502 and 503, waiting for Retry-After when it is present. Do not retry 400, 401, 403, 404 or 413 without changing the request. Retry 402 only after topping up.