錯誤回應格式
所有錯誤都以一致的 JSON 格式回傳,並可選擇包含 Agent-First 提示:message, type, code, param) 永遠會存在。提示欄位 (did_you_mean, suggestions, hint, retryable, retry_after, balance_usd, estimated_cost_usd) 為 AI agent 自我修正的選用擴充。詳情請參閱 Agent-First API guide。
OpenAI-compatible endpoints 使用 LemonData 的穩定 gateway 錯誤類型。Anthropic-compatible 與 Gemini-compatible endpoints 則使用其原生的錯誤分類與回應格式。
HTTP 狀態碼
| 狀態碼 | 說明 |
|---|---|
| 400 | Bad Request - 參數無效 |
| 401 | Unauthorized - API key 無效或遺失 |
| 402 | Payment Required - 餘額不足 |
| 403 | Forbidden - 存取被拒或模型不允許 |
| 404 | Not Found - 模型或資源不存在 |
| 413 | Payload Too Large - 輸入或檔案大小超過限制 |
| 429 | Too Many Requests - 超過速率限制 |
| 500 | Internal Server Error |
| 502 | Bad Gateway - 上游供應商錯誤 |
| 503 | Service Unavailable - 服務暫時不可用 |
| 504 | Gateway Timeout - 請求逾時 |
錯誤類型
身份驗證錯誤 (401)
| Type | Code | 說明 |
|---|---|---|
invalid_api_key | invalid_api_key | API key 遺失或無效 |
expired_api_key | expired_api_key | API key 已被撤銷 |
付款錯誤 (402)
| Type | Code | 說明 |
|---|---|---|
insufficient_balance | insufficient_balance | 帳戶餘額過低(OpenAI-compatible endpoints) |
insufficient_balance_error | insufficient_balance | 帳戶餘額過低(Anthropic-compatible endpoints) |
quota_exceeded | quota_exceeded | API key 使用額度已達上限 |
存取錯誤 (403)
| Type | Code | 說明 |
|---|---|---|
access_denied | access_denied | 存取資源被拒 |
access_denied | model_not_allowed | 此 API key 不允許使用該模型 |
驗證錯誤 (400)
| Type | 說明 |
|---|---|
invalid_request_error | 請求參數無效 |
context_length_exceeded | 輸入超過模型的上下文長度 |
model_not_found | 請求的模型在目前公開合約中不可用 |
model_not_found。
速率限制錯誤 (429)
當您超過速率限制時:Retry-After 標頭與 retry_after 欄位皆表示在重新嘗試前需等待的確切秒數。
載荷過大 (413)
當輸入或檔案大小超過限制時:- 圖像檔案過大(最大 20MB)
- 音訊檔案過大(最大 25MB)
- 輸入文字超過模型的上下文長度
上游錯誤 (502, 503)
| Type | 說明 |
|---|---|
upstream_error | 供應商回傳錯誤 |
all_channels_failed | 沒有可用的供應商 |
timeout_error | 請求逾時 |
在 Python 中處理錯誤
在 JavaScript 中處理錯誤
最佳實踐
實作指數退避
實作指數退避
當被限制速率時,在重試之間逐步增加等待時間:
設定逾時
設定逾時
始終設定合理的逾時以避免請求掛起:
記錄錯誤以利除錯
記錄錯誤以利除錯
記錄完整的錯誤回應(包含 request ID)以便支援:
處理模型特定錯誤
處理模型特定錯誤
某些模型有特定要求(例如 max tokens、圖像格式)。
在發出請求前驗證輸入。