Developer docs
12. Error handling
Version 1.0 · 2026-10-20
Errors use the OpenAI format. Only 429, 502 and 503 are worth retrying; for any other status code, fix the request first.
Error format
{
"error": {
"message": "Rate limit exceeded (60/min) for this key.",
"type": "rate_limit_error",
"param": null,
"code": null
}
}| `type` | Status codes |
|---|---|
authentication_error | 401 (code is invalid_api_key) |
permission_error | 403 |
invalid_request_error | 400, 402, 404, 409, 413 |
rate_limit_error | 429 (code may be sellable_capacity_exhausted when a model is at capacity) |
server_error | 500 and above |
A few errors use {"detail": ...} instead: validation errors from the speech and image backends, 401 from /dashboard/billing/*, unknown paths (404), wrong methods (405) and query-parameter type errors (422). In addition, when the request body is not a JSON object (an array, for example), some endpoints may return a plain-text 500. Handle both formats when parsing errors. Error message text may change, so base your logic on the status code and type.
Status codes
| Status | Common causes | What to do |
|---|---|---|
| 400 | Malformed JSON, invalid field, context exceeded, audio over 600 seconds, unknown voice | Fix the request; do not retry |
| 401 | Missing or invalid key | Check the Authorization header |
| 402 | Prepaid balance exhausted | Add credit, then try again |
| 403 | Restricted key used outside its scope or expired | Check your key's scope with us |
| 404 | Model does not exist or is not available, redeem code does not exist, or unknown path | Check the model id and path |
| 409 | Redeem code already used, disabled or expired | Contact us |
| 413 | Speech recognition upload over 25 MB | Compress or split the file and upload again |
| 429 | Rate, concurrency or daily quota exceeded, or the model is at capacity | Wait for Retry-After, then retry |
| 500 | Unexpected server error | Retry once; if it persists, contact us |
| 502 | Model backend unreachable (All replicas failed) | Retry with exponential backoff; if it keeps failing, check the service status or switch models |
| 503 | Model under maintenance or being switched | Wait for Retry-After (usually 30 or 600 seconds) |
Check order
When a request has several problems at once, the platform checks them in the order below and returns the first failure. When debugging, work down the table from the top.
| Order | Check | On failure |
|---|---|---|
| 1 | Is the key valid? | 401 |
| 2 | Is the request valid JSON? | 400 |
| 3 | Is the key allowed to use this endpoint and model? | 403 |
| 4 | Does the model exist and is it available? | 404 |
| 5 | Is the model under maintenance or being switched? | 503 |
| 6 | Concurrent in-flight requests for the key | 429 |
| 7 | Prepaid balance | 402 |
| 8 | Requests per minute | 429 |
| 9 | Daily token quota | 429 |
| 10 | Overall model capacity | 429 |
| 11 | Model backend processing | 400 or 502 |
Field-validation 400s (for example, an unsupported image field) and the speech-recognition 413 may be returned earlier than this order suggests.
Retry strategy
- Retry only 429, 502 and 503, plus network-level connection errors.
- If there is a
Retry-After, wait that long. Otherwise use exponential backoff (1, 2, 4, 8 seconds) with random jitter, up to 4 attempts. - Use streaming for generations expected to take longer than 60 seconds; non-streaming requests longer than about 100 seconds may be disconnected. If a long non-streaming request times out, check your balance or usage to see whether it was charged before you resend it.
import random, time
from openai import APIStatusError
RETRYABLE = {429, 502, 503}
def with_retry(call, attempts=4):
for i in range(attempts):
try:
return call()
except APIStatusError as e:
if e.status_code not in RETRYABLE or i == attempts - 1:
raise
wait = e.response.headers.get("retry-after")
time.sleep(float(wait) if wait else 2 ** i + random.random())
resp = with_retry(lambda: client.chat.completions.create(
model="glm-5.2-fp8",
messages=[{"role": "user", "content": "Hello"}],
))With the official OpenAI SDK, you can also set max_retries when you create the client and let the SDK handle retries.