Errors
What the API returns when something goes wrong, which errors to retry, and how to report.
An error is an HTTP status code and a JSON body:
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "messages: At least one message is required"
},
"request_id": "req_7Hq2LmN9xP4sTv1KcW8bYd3R"
}error.message says what to fix. It's written for people, so log it, but branch on error.type and the status code.
What to do
| Status | Type | What to do |
|---|---|---|
400 | invalid_request_error | Fix the request. Don't retry it as it is |
401 | authentication_error | Check the key, or create a new one |
402 | billing_error | Add credits, or raise the key's spend limit. Don't retry until then; show the message to whoever can fix it |
404 | not_found_error | Check the model name or the endpoint |
529 | overloaded_error | The model is busy, or web search is unavailable. Retry with backoff |
500 | api_error | Retry with backoff |
503 | api_error | The API is unavailable. Retry with backoff |
The full list is in the reference.
Out of credits or over a limit
A 402 has one of two messages:
- Out of credits. Your organization's balance reached zero. An owner or admin buys credits or turns on auto-reload on the Billing page.
- Spend limit reached. The key spent its monthly limit. An owner or admin raises it on the API keys page, or the key works again on the first day of next month (UTC).
See Limits and spend.
Retries
Retry 529, 500 and 503, and network errors, with exponential backoff — wait 1, 2, 4… seconds, plus a little randomness, and give up after a few tries.
The Anthropic SDKs already do this: they retry these errors twice by default. Change it with maxRetries (TypeScript) or max_retries (Python).
A stream that fails after it started ends with an error event rather than a status code. Retry the whole request.
Request IDs
Every response — success or error — has a request-id header, and error bodies repeat it as request_id. Log it. When you report a problem, send us the request ID and we can find your request.
In the SDKs it's message._request_id (TypeScript and Python).