Docs

Errors

Errors use OpenAI's envelope, so SDK error classes map cleanly. The rule that matters: a failed request is never a billed request.

Error shape

{
  "error": {
    "message": "Insufficient credits. This request may cost up to 0.0301 USD. Top up at the dashboard.",
    "type": "insufficient_quota",
    "code": "insufficient_quota"
  }
}

type groups the family, code (when present) names the exact condition, and message is safe to log and show to humans.

Status codes

StatusCodeMeaningCharged
400invalid_request_errorBad JSON, missing model or messages, invalid max_tokens.No
401invalid_api_keyMissing, malformed, or revoked key.No
402insufficient_quotaBalance cannot cover the request's worst case. Top up.No
404model_not_foundThe model id is not in the catalog.No
413file_too_largeBatch input file over the size limit.No
429rate_limit_errorRequests per minute exceeded. Honor Retry-After.No
429monthly_key_limit_exceededThis key's monthly spend cap is exhausted.No
4xx(engine)The engine rejected the request, e.g. prompt over the context window. Forwarded verbatim.No
502server_errorThe upstream GPU failed, timed out, or failed TLS verification.No
503server_errorNo live backend is serving this model right now.No
A 503 means the model has no live GPU behind it at this moment. The Batch API accepts work during exactly that state and runs it when capacity returns.

Handling in SDKs

import openai
from openai import OpenAI

client = OpenAI(base_url="https://tokenkiln.com/api/v1", api_key="tk_...")

try:
    response = client.chat.completions.create(
        model="Qwen/Qwen2.5-7B-Instruct",
        messages=[{"role": "user", "content": "Hello!"}],
    )
except openai.RateLimitError as e:
    retry_after = e.response.headers.get("retry-after")
    print(f"429, retry in {retry_after}s")
except openai.APIStatusError as e:
    # 402 insufficient_quota, 404 model_not_found, 5xx upstream...
    print(e.status_code, e.response.json()["error"]["code"])

Retry guidance

  • 429: wait the number of seconds in the Retry-After header, then retry.
  • 502 and 503: retry with exponential backoff. Since failed requests are never billed, retrying an errored call cannot double-charge you.
  • 402: not retryable; top up or enable auto top-up on the Credits page.
  • When contacting support, include the response's x-request-id header.