Errors and limits
Every error the API sends, in both SDK formats, and what to do about each.
On this page
A refused request answers with an HTTP status and an error object, and is not billed. Branch on the status and the code; show the message to people.
The two error shapes
In the OpenAI SDK format (/v1/chat/completions, /v1/models, /v1/management/keys):
{ "error": { "message": "Rate limit exceeded (...)", "type": "rate_limit_error", "code": "requests_per_minute" }}In the Messages format (/v1/messages, /v1/messages/count_tokens, and /v1/models when the request sends anthropic-version), the shape is the Anthropic SDK's. code is the same code as in the table below, and request_id is there when the request was given an id (the same as its x-request-id header):
{ "type": "error", "error": { "type": "rate_limit_error", "message": "Rate limit exceeded (...)", "code": "requests_per_minute" }, "request_id": "<request id>"}Every code
The type column is the OpenAI format's. Where code is a number, the API sends the status itself, as a string.
| Status | type | code | Meaning and what to do |
|---|---|---|---|
| 400 | invalid_request_error | 400 | The body is not valid JSON, messages is missing or empty, or model is missing. Fix the request. |
| 400 | invalid_request_error | context_length_exceeded | The conversation is longer than the model's context. Shorten it. |
| 400 | invalid_request_error | unsupported_input | The model cannot read one of the attached images. |
| 400 | invalid_request_error | content_blocked | The request cannot be processed. |
| 400 | invalid_request_error | invalid_request | The model did not accept the request. Check its messages and fields. |
| 401 | authentication_error | missing_api_key | No key was sent. See Authentication. |
| 401 | authentication_error | invalid_api_key | The key is wrong or was revoked. |
| 401 | authentication_error | key_expired | The key passed its expiry. Create a new key or set a later expiry. |
| 402 | insufficient_credits | insufficient_credits | Your credit balance is zero or below. Add credits (API Credits). |
| 402 | insufficient_credits | key_limit_reached | The key has spent its cap. Raise or remove it (Management Keys). |
| 403 | permission_error | subscription_inactive | The account's subscription is not active. The message links to your plans. |
| 403 | account_suspended | account_suspended | The account is suspended. Write to the address in the message. |
| 403 | account_pending_deletion | account_pending_deletion | The account is waiting to be deleted and its keys are paused. Sign in to restore it. |
| 404 | invalid_request_error | model_not_found | Unknown model name. The message lists the names that work (Models). |
| 404 | invalid_request_error | 404 | No such address. Check the path and the base URL. |
| 405 | invalid_request_error | method_not_allowed | Wrong HTTP method: the calls are POST, the lists GET. |
| 413 | invalid_request_error | request_too_large | The request body is too large. Send fewer or smaller images, or a shorter history. |
| 429 | rate_limit_error | requests_per_minute | Too many requests this minute. The message gives the limit. Wait and retry. |
| 429 | rate_limit_error | rate_limited | The service is busy. Retry after the retry-after header's seconds. |
| 429 | rate_limit_error | window_6h_limit | A plan allowance for this model is used up for its 6-hour window. The message says when it resets. |
| 429 | rate_limit_error | window_weekly_limit | The same, for the weekly window. |
| 429 | rate_limit_error | free_max_credit_exhausted | The free plan's one-time Raven Max trial is used up. |
| 500 | api_error | internal_error | Something failed on our side. Retry with a growing wait. |
| 503 | service_unavailable | raven_busy | The service is busy. Sent with retry-after: 5 and x-should-retry: true. Retry. |
| 503 | service_unavailable | lockdown_model_traffic | Model calls are paused for maintenance. Retry later. |
| 503 | service_unavailable | lockdown_api_keys | Calls with API keys are paused for maintenance. Retry later. |
| 503 | server_error | maintenance | Maintenance. Sent with x-should-retry: false; the message says more. |
| 503 | service_error | plans_missing | A short outage on our side. Retry shortly. |
Management keys have codes of their own; see Management Keys.
Types in the Messages format
In the Messages format, error.type follows the status, as the Anthropic SDK expects, and error.code carries the code from the table above:
| Status | error.type |
|---|---|
| 400, 405 | invalid_request_error |
| 401 | authentication_error |
| 402 | billing_error |
| 403 | permission_error |
| 404 | not_found_error |
| 413 | request_too_large |
| 429 | rate_limit_error |
| 503 | overloaded_error |
| 500 and other 5xx | api_error |
A Messages request whose body the API cannot read (not JSON, a missing field, an unknown block or tool type) gets 400 with the code invalid_request and a message that names the field.
Errors inside a stream
Once a stream has started, its status is 200, so a later failure arrives in the stream. In the OpenAI format it is a data: chunk with an error object (code stream_parse_error), then data: [DONE]. In the Messages format it is an error event of type api_error, with no message_stop. See Streaming.
Limits
- Per minute. Requests are counted per account, per minute. The limit follows the account's plan, and the
requests_per_minutemessage states it. - Per key. A key can have a spending cap and an expiry (Authentication).
- Credits. Calls with an API key need a balance above zero (API Credits).
- Plan allowances. Plans have 6-hour and weekly allowances per model; Plans and Usage explains them. When one is used up, the
429message says when it resets.
Retry with backoff
Retry requests_per_minute, rate_limited, raven_busy and internal_error, waiting a little longer each time and honouring retry-after when it is sent. Do not retry an answer with x-should-retry: false, a 4xx other than those, or a plan allowance that is used up: they need a change, not another attempt.
import os, timefrom openai import OpenAI, APIStatusErrorclient = OpenAI( base_url="https://api.dipoleml.com/v1", api_key=os.environ["RAVEN_API_KEY"], max_retries=0, # this loop does the retrying)RETRY = {"requests_per_minute", "rate_limited", "raven_busy", "internal_error"}def complete(messages, attempts=4): for i in range(attempts): try: return client.chat.completions.create(model="Raven Flash", messages=messages) except APIStatusError as e: code = (e.response.json().get("error") or {}).get("code") if code not in RETRY or i == attempts - 1: raise time.sleep(float(e.response.headers.get("retry-after") or 2 ** i))resp = complete([{"role": "user", "content": "Ping"}])print(resp.choices[0].message.content)