错误与重试
统一错误格式与重试退避建议。
错误码表
| HTTP | 含义 | 处理建议 |
|---|---|---|
| 401 | Key 无效/缺失 | 检查 Authorization 头 |
| 402 | 余额不足 | 充值或领取免费额度 |
| 403 | Key 无权访问该模型 | 控制台检查 Key 的模型范围 |
| 429 | 触发限流(RPM/TPM) | 指数退避重试 |
| 5xx | 上游模型故障 | 网关自动冷却故障通道并重试;仍可退避重试 |
重试示例
import time
for attempt in range(4):
try:
resp = client.chat.completions.create(model="auto", messages=msgs)
break
except Exception as e:
if attempt == 3: raise
time.sleep(2 ** attempt) # 1s 2s 4s