API key

Build

Errors and retries

The error format, every status the API returns, what you're charged, and which errors to retry.

Error format

Errors come back in the OpenAI shape:

{
  "error": {
    "message": "invalid api key",
    "type": "auth_error",
    "code": "invalid_api_key"
  }
}

Branch on the HTTP status and code. param names the field at fault, when there is one. message is written for people and may change. An error returned by the model itself is passed through with its own status and body.

Statuses

StatuscodeMeaningRetry
400malformed_json, unknown_parameter, invalid_request_bodyThe body isn't valid JSON, has a parameter outside the OpenAI API, or has an invalid value. param names the field.No: fix the request.
400max_tokens_exceeds_model_limit, token_limit_exceededmax_tokens is above what the model allows.No: lower max_tokens.
400endpoint_not_supportedThe model is served on a different endpoint.No.
401invalid_api_key, api_key_expiredThe key is missing, wrong, revoked or expired.No: fix the key.
402insufficient_fundsYour balance, or the key's credit cap, can't cover the request's maximum cost.After adding credit, or with a smaller max_tokens.
403model_not_allowedThe key isn't allowed to use this model.No.
404model_not_foundUnknown model id. It's checked before the key, so a typo answers 404 whatever the key.No: check the catalogue.
409duplicate_idempotency_keyA request with this Idempotency-Key already ran, or is still running.No.
413request_too_largeThe request is over 2 MB.No.
429rate_limited_key, rate_limited_org, rate_limited_model, upstream_rate_limitedToo many requests for the key or organisation, or the model is at capacity.Yes, after Retry-After.
502, 503The model is temporarily unavailable.Yes, with backoff.

What you're charged for

Requests that fail are never charged. A stream that ends early is charged for the output it delivered before it ended; its row in the request logs is marked as estimated.

Retrying

  • Retry 429 and 5xx. Wait for Retry-After when it's present; otherwise back off exponentially with jitter (for example 0.5 s, 1 s, 2 s, then stop).
  • Don't retry other 4xx. They need a change to the request, the key or your balance, and the OpenAI SDKs won't retry them automatically.
  • Make retries safe. A retried request that succeeds runs, and is charged, twice. Send an Idempotency-Key header to prevent that; see Chat completions.
  • Don't retry a stream you've partly received. Treat it as done.

How a request ended

Your request logs record how each request ended, in termination_reason:

ValueMeaning
successCompleted.
client_disconnectYour client closed the connection before the end.
proxy_guillotineThe request reached the five-minute limit.
upstream_stream_abortedThe model stopped the stream midway.

A refused request records its error code instead, for example insufficient_funds.

Account API errors

The Account API uses its own error format; see Account API.

Something unclear or wrong on this page? Tell us. Machine-readable copies: llms.txt.