Errors

Errors

Errors use the OpenAI error envelope, with a machine-readable type.

json
{
  "error": {
    "message": "balance too low for this request: top up to continue",
    "type": "insufficient_balance"
  }
}
StatusTypeWhat it means
400invalid_requestMalformed JSON, or model omitted.
401invalid_api_keyKey missing, malformed, or revoked.
402insufficient_balanceYour balance is empty. Deposit to continue.
403model_not_allowedThis key has an allowlist that excludes the requested model.
404model_not_foundUnknown model id, or a model no longer offered.
429rate_limit_exceededThis key's per-minute limit was exceeded. Back off and retry.
502upstream_errorThe upstream model failed. Nothing was billed.
503upstream_errorCapacity temporarily unavailable. Retry shortly.

Retry guidance

Retry 429, 502 and 503 with exponential backoff. Do not retry 400, 401, 403 or 404, as they will fail identically until you change the request. Treat 402 as an alert to top up rather than a transient fault.

python
import time
from openai import OpenAI, APIStatusError

def call(**kw):
    for attempt in range(5):
        try:
            return client.chat.completions.create(**kw)
        except APIStatusError as e:
            if e.status_code in (429, 502, 503) and attempt < 4:
                time.sleep(2 ** attempt)
                continue
            raise
A failed upstream request costs you nothing. We only deduct balance once a request settles with a usage report.