Using the API

Errors

Status codes, the error object, and how to handle each kind of failure.

The API uses standard HTTP status codes. Every error response has a JSON body in the OpenAI error format, so SDKs raise their usual exception types.

Error object
{
  "error": {
    "message": "A human readable description of what went wrong.",
    "type": "invalid_request_error",
    "param": null,
    "code": "invalid_api_key"
  }
}
FieldDescription
messageA readable explanation. Safe to log, not meant to be parsed.
typeThe broad category of error, listed below.
paramThe request field that caused the error, or null.
codeA stable, machine readable code, or null. Branch on this.

Status codes

StatusTypeCodeMeaning
400invalid_request_errornullThe request body is malformed or a parameter is out of range. Check param.
401invalid_request_errorinvalid_api_keyThe key is missing, wrong or revoked.
402insufficient_quotainsufficient_quotaYour credit balance is used up. Add credits to continue.
404invalid_request_errormodel_not_foundThe model id does not exist or your key cannot use it.
429rate_limit_errornullToo many requests or tokens per minute. Back off and retry.
429insufficient_quotamonthly_limit_reachedYour organization reached its monthly spend limit.
429insufficient_quotaworkspace_limit_reachedThis workspace reached its monthly spend limit.
502api_errorupstream_errorThe model server returned an error. Retry with backoff.
503api_errorservice_unavailableThe service is temporarily unavailable. Retry with backoff.

Out of credits

When your prepaid balance reaches zero, requests fail with status 402 until you add credits or auto-reload tops you up.

402 Payment Required
{
  "error": {
    "message": "Your credit balance is too low to make this request. Add credits at https://platform.seosorted.ai/settings/billing and try again.",
    "type": "insufficient_quota",
    "param": null,
    "code": "insufficient_quota"
  }
}

Handling errors in code

import openai

try:
    response = client.chat.completions.create(model="inkwell-1", messages=messages)
except openai.AuthenticationError:
    print("Check SEOSORTED_API_KEY")
except openai.RateLimitError as e:
    print("Slow down or raise your limit:", e.code)
except openai.APIStatusError as e:
    if e.status_code == 402:
        print("Out of credits. Add credits in the console.")
    else:
        raise

Retries

Retry 429, 502 and 503 responses with exponential backoff and jitter. Do not retry 400, 401, 402 or 404: the same request will fail again. The OpenAI SDKs retry some of these for you; set max_retries (Python) or maxRetries (TypeScript) to tune it.