错误码详解
401 Unauthorized
原因:API Key 无效或已过期。
json
{"error": {"message": "Invalid API key", "code": 401}}排查:确认 key 拼写正确、未过期。
403 Forbidden
原因:余额不足或权限不足。
429 Rate Limit Exceeded
原因:超出 RPM 或 TPM 限制。
python
import time
for attempt in range(3):
try:
return client.chat.completions.create(...)
except RateLimitError:
time.sleep(2 ** attempt) # 指数退避400 Bad Request
常见场景:
model模型名不支持messages缺少rolestream: true与response_format不兼容
500 / 502 / 503
上游服务错误,等待后重试或切换 channel。
速查表
| 错误信息 | HTTP | 原因 | 处理 |
|---|---|---|---|
Invalid API key | 401 | key 无效 | 检查拼写 |
Insufficient credits | 403 | 余额不足 | 充值 |
Rate limit exceeded | 429 | 超限 | 等待/切换 channel |
context_length_exceeded | 400 | 输入超长 | 缩短或换 32k 模型 |
model_not_found | 400 | 模型不存在 | 检查模型名 |