Source: https://aiprimetech.io/zh/docs/api-reference/errors/ [首页](/zh/) / [文档](/zh/docs/) / 错误 # 错误 网关返回的所有状态码、实际成因及解决方法。 复制页面 [ 以 Markdown 查看](/zh/docs/api-reference/errors.md) [ 向 ChatGPT 提问](https://chatgpt.com/?q=Read%20https%3A//aiprimetech.io/docs/api-reference/errors.md%20and%20answer%20questions%20about%20it.%20Start%20with%20a%20short%20summary.) [ 在 Claude 中提问](https://claude.ai/new?q=Read%20https%3A//aiprimetech.io/docs/api-reference/errors.md%20and%20answer%20questions%20about%20it.%20Start%20with%20a%20short%20summary.) 错误返回 JSON,其中包含 type 以及便于人类阅读的 message。状态码遵循 Anthropic 和 OpenAI 的约定。 ``` Copy{"type": "error", "error": {"type": "authentication_error", "message": "invalid api key"}} ``` ## 状态码 | 代码 | 含义 | 最常见的实际原因 | 解决方法 | 400 | 请求无效 | 缺少 max_tokens (Messages 请求中),或格式错误的 messages 数组 | 对照以下内容检查请求体 [Messages](/zh/docs/api-reference/messages/) | 401 | 未通过身份验证 | 密钥放在了不符合该格式要求的请求头中 | x-api-key 用于 Messages,Bearer 用于 Chat Completions | 402 | 需要付款 | 余额耗尽或套餐已过期 | [充值](/zh/topup/) 或 [购买套餐](/zh/plans/) | 403 | 禁止访问 | 密钥已被撤销,或账户未启用该模型 | 创建新密钥;检查 /v1/models | 404 | 未找到 | 基础 URL 错误——通常是重复或缺少 /v1 | 参见 [基础 URL](/zh/docs/getting-started/base-urls/) | 429 | 请求受到速率限制 | 并发请求过多 | 采用指数退避;参见 [速率限制](/zh/docs/api-reference/rate-limits/) | 500 | 网关错误 | 意外的内部故障 | 重试一次;如果问题持续存在,请联系支持并提供请求 ID | 529 | 上游过载 | 提供商容量问题,与您的账户无关 | 采用退避策略重试——通常会在几秒内恢复 ## 错误类型 | 类型 | 含义 | invalid_request_error | 请求体格式错误或缺少必填字段 | authentication_error | 密钥缺失、格式错误或已被撤销 | permission_error | 密钥有效,但无权使用该模型 | not_found_error | 未知路径或未知模型 ID | rate_limit_error | 受到限流 | api_error | 内部故障 | overloaded_error | 上游容量耗尽 ## 正确重试 重试 429, 500, 502, 503 和 529。切勿盲目重试 400, 401, 402 或 403 ——相同的请求会以相同方式失败,而且对于已生成部分内容的响应,您可能需要支付两次费用。 ``` Copyimport time, httpx def call(body, key, tries=4): for i in range(tries): r = httpx.post(class="s">"https://aiprimetech.io/v1/messages", json=body, timeout=120, headers={class="s">"x-api-key": key, class="s">"anthropic-version": class="s">"2023-06-01"}) if r.status_code in (429, 500, 502, 503, 529): time.sleep(2 ** i) class=class="s">"c"># 1s, 2s, 4s, 8s continue r.raise_for_status() return r.json() raise RuntimeError(class="s">"upstream unavailable after retries") ``` 在生产环境中,为等待时间添加随机抖动。大量工作进程同步重试,会将一次短暂故障变成针对同一上游的惊群请求。 [速率限制并发与吞吐量](/zh/docs/api-reference/rate-limits/)[故障排查按症状查找的索引](/zh/docs/guides/troubleshooting/) [上一页流式传输](/zh/docs/api-reference/streaming/)[下一页速率限制](/zh/docs/api-reference/rate-limits/)