Errors

Errors use standard HTTP status codes and an OpenAI-style body:

{
  "error": {
    "message": "Invalid API key.",
    "type": "authentication_error",
    "code": "invalid_api_key"
  }
}

Status codes

Status Meaning What to do
400 The request is malformed or a parameter is invalid. Fix the request; the message says which field.
400 context_length_exceeded The prompt plus max_tokens is longer than the model’s context. Shorten the input or lower max_tokens.
401 Missing or invalid API key. Check the Authorization header and the key.
402 Not enough credits. Add credits in the dashboard.
404 Unknown model or endpoint. Check the model ID with List models.
429 Too many concurrent requests, or the model is under heavy load. Wait a moment and retry with backoff. See Rate limits.
500 Unexpected error on our side. Retry with backoff.
503 Temporarily over capacity. Retry with backoff.

Retrying

Retry 429, 500 and 503 with exponential backoff and jitter, for example 1, 2, 4 and 8 seconds. Do not retry 400, 401, 402 or 404 without changing the request. The OpenAI SDKs already retry these twice by default. Set max_retries to change that.