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 (OpenAI format)
{  "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):

error (Messages format)
{  "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.

StatustypecodeMeaning and what to do
400invalid_request_error400The body is not valid JSON, messages is missing or empty, or model is missing. Fix the request.
400invalid_request_errorcontext_length_exceededThe conversation is longer than the model's context. Shorten it.
400invalid_request_errorunsupported_inputThe model cannot read one of the attached images.
400invalid_request_errorcontent_blockedThe request cannot be processed.
400invalid_request_errorinvalid_requestThe model did not accept the request. Check its messages and fields.
401authentication_errormissing_api_keyNo key was sent. See Authentication.
401authentication_errorinvalid_api_keyThe key is wrong or was revoked.
401authentication_errorkey_expiredThe key passed its expiry. Create a new key or set a later expiry.
402insufficient_creditsinsufficient_creditsYour credit balance is zero or below. Add credits (API Credits).
402insufficient_creditskey_limit_reachedThe key has spent its cap. Raise or remove it (Management Keys).
403permission_errorsubscription_inactiveThe account's subscription is not active. The message links to your plans.
403account_suspendedaccount_suspendedThe account is suspended. Write to the address in the message.
403account_pending_deletionaccount_pending_deletionThe account is waiting to be deleted and its keys are paused. Sign in to restore it.
404invalid_request_errormodel_not_foundUnknown model name. The message lists the names that work (Models).
404invalid_request_error404No such address. Check the path and the base URL.
405invalid_request_errormethod_not_allowedWrong HTTP method: the calls are POST, the lists GET.
413invalid_request_errorrequest_too_largeThe request body is too large. Send fewer or smaller images, or a shorter history.
429rate_limit_errorrequests_per_minuteToo many requests this minute. The message gives the limit. Wait and retry.
429rate_limit_errorrate_limitedThe service is busy. Retry after the retry-after header's seconds.
429rate_limit_errorwindow_6h_limitA plan allowance for this model is used up for its 6-hour window. The message says when it resets.
429rate_limit_errorwindow_weekly_limitThe same, for the weekly window.
429rate_limit_errorfree_max_credit_exhaustedThe free plan's one-time Raven Max trial is used up.
500api_errorinternal_errorSomething failed on our side. Retry with a growing wait.
503service_unavailableraven_busyThe service is busy. Sent with retry-after: 5 and x-should-retry: true. Retry.
503service_unavailablelockdown_model_trafficModel calls are paused for maintenance. Retry later.
503service_unavailablelockdown_api_keysCalls with API keys are paused for maintenance. Retry later.
503server_errormaintenanceMaintenance. Sent with x-should-retry: false; the message says more.
503service_errorplans_missingA 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:

Statuserror.type
400, 405invalid_request_error
401authentication_error
402billing_error
403permission_error
404not_found_error
413request_too_large
429rate_limit_error
503overloaded_error
500 and other 5xxapi_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_minute message 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 429 message 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.

python
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)