OpenAI 兼容 API 与 Anthropic 兼容 API 的错误返回与处理建议。
OpenAI 兼容(/v1/*)
{
"error": {
"message": "可读说明",
"type": "invalid_request_error",
"param": "messages[0].tool_call_id",
"code": "insufficient_balance"
}
}
| 字段 | 含义 |
|---|---|
message | 主要说明 |
type | 如 invalid_request_error、rate_limit_error |
param | 校验失败时的 JSON 路径 |
code | 机器可读码(如 insufficient_balance、quota_exceeded、ip_not_allowed、upstream_rate_limited) |
部分响应可能含 error.gateway 结构化字段。
Anthropic 兼容(/anthropic/v1/*)
{
"type": "error",
"error": { "type": "authentication_error", "message": "..." },
"request_id": "req_..."
}
提工单时请记录 request_id。
HTTP 状态码
| 状态码 | 含义 | 客户端建议 |
|---|---|---|
| 400 | 请求体或内容不合法 | 修正后重试,勿盲目重放 |
| 401 | 密钥缺失/错误/过期 | 更换密钥,检查 Bearer / x-api-key |
| 403 | 策略:模型、余额、IP、上游密钥等 | 改配置或计费,通常勿重试 |
| 429 | 限流 | 指数退避 + 抖动 |
| 502/503 | 上游或网关瞬时故障 | 有限重试 |
| 504 | 超时 | 加大客户端超时,检查请求体大小 |
重试建议
- 可重试: 429、502、503、504 及短暂网络错误。
- 勿原样重试: 400、401、多数 403。
- 有副作用的 tool 调用请在业务层做幂等。
- 建议上限 3–5 次,退避 1s 起、封顶约 60s。