Chapter 18 of 211 min

18. Error Codes and Error Handling#

On failure, the API returns an OpenAI-compatible error structure (the /v1/messages endpoint returns an Anthropic-shaped error structure):

JSON
{
  "error": {
    "message": "错误描述",
    "type": "invalid_request_error",
    "code": 400
  }
}

Common status codes#

Status codeMeaningTroubleshooting
400Missing or invalid request parametersCheck required fields such as model, messages, input; do not send tools / multimodal content to /v1/messages
401Missing or invalid API KeyConfirm the Authorization: Bearer sk-... header is correct and the key is not expired / revoked
402Quota exhausted or insufficient balanceTop up in the console or adjust the key's quota
403Source IP not on the allowlistCheck the key's IP allowlist settings
404Model does not exist or is not accessible / task does not existConfirm available models with GET /v1/models; confirm the path carries a single /v1
429Rate limit triggeredLower concurrency, then retry with exponential backoff
500Internal gateway errorRetry later; contact support if it persists
502Upstream vendor returned an errorRetry later or switch models
503No available channelNo upstream is currently available for this model; retry later or contact support

Error handling tips#

  • Retry 429 and 5xx automatically (exponential backoff + a maximum retry count).
  • Do not retry client errors such as 400, 401, 403 — fix the request instead.
  • Log the X-Request-Id from the response (if present) so you can provide it to the support team when troubleshooting.