Errors and retries
Handle errors by status, preserve request identifiers, and retry only failures that can succeed without changing the request.
Error shape
Inference errors keep their original HTTP status, type, and code. The message explains what failed and what to do next; details provides the same guidance as structured fields.
{
"error": {
"message": "Upstream provider balance is insufficient. The upstream provider's account has insufficient credits or billing quota. This is separate from your gateway balance. Try another provider route or contact support; adding gateway credit will not resolve this provider error. (request id: example-request-id)",
"type": "upstream_error",
"param": "",
"code": "insufficient_quota",
"details": {
"source": "upstream",
"category": "provider_balance",
"title": "Upstream provider balance is insufficient",
"description": "The upstream provider's account has insufficient credits or billing quota. This is separate from your gateway balance.",
"action": "Try another provider route or contact support; adding gateway credit will not resolve this provider error.",
"retryable": false
}
}
}Anthropic Messages errors retain the standard type, error, and request_id envelope. Their message includes the source-aware explanation, recovery guidance, error code, and request ID. Streaming failures use the same guidance in the terminal error event.
Error source
| Source | Meaning |
|---|---|
Upstream provider upstream | The provider serving the model. Its credentials, credits, limits, and availability are separate from your gateway account. |
Your gateway account account | Your gateway authentication, permissions, wallet balance, subscription, or API key budget. |
Gateway gateway | Routing, model pricing, configuration, capacity, or an internal gateway failure. |
Your request request | Request validation, payload size, unsupported operations, resource identifiers, or content policy. |
Source unavailable unknown | The error does not contain enough information to assign a source. Keep the request ID for support. |
HTTP status guide
| Status | Meaning | Client action | Retry |
|---|---|---|---|
| 400 | Invalid request | Check the request shape, model parameters, and content limits. | No |
| 401 | Authentication failed | Check the error source. Only account errors require checking your gateway API key; upstream errors concern the provider credentials. | Check source |
| 402 | Balance or billing limit | Check the error source. An upstream balance error concerns the provider account, not your gateway wallet. | After resolution |
| 403 | Access denied | For account errors, check your key restrictions. For upstream errors, the provider must resolve its account permissions or quota. | Check source |
| 408 / 504 | Request or provider timeout | Check the error source and retry cautiously. A streaming request may have produced partial output. | Yes |
| 413 / 422 | Request limit or validation failure | Review the error category. Reduce input size or correct unsupported parameters; provider quota limits require provider recovery. | Check category |
| 404 | Route or resource not found | Verify the endpoint and resource identifier. | No |
| 429 | Rate or quota limit | Back off for rate limits. Provider billing quota errors need a funded route or a quota reset, not a gateway top-up. | Check category |
| 500 | Gateway or upstream error | Check the error source, retry with backoff, and retain the request ID for support. | Yes |
| 502 | Upstream failure | Retry because another eligible channel may become available. | Yes |
| 503 | No route available | Back off or select another model. | Yes |
Retry policy
Use details.retryable when available, then fall back to the HTTP status for older responses. A retryable error is not a guarantee of success. Use bounded exponential backoff and respect Retry-After.
const retryable = new Set([429, 500, 502, 503, 504]);
for (let attempt = 0; attempt < 4; attempt++) {
const response = await fetch(url, options);
if (response.ok) return response;
const body = await response.clone().json().catch(() => null);
const guidance = body?.error?.details;
const canRetry = guidance?.retryable ?? retryable.has(response.status);
if (!canRetry || attempt === 3) return response;
const retryAfter = response.headers.get("Retry-After");
const retryAfterMs = retryAfter
? (/^\d+$/.test(retryAfter)
? Number(retryAfter) * 1000
: Date.parse(retryAfter) - Date.now())
: 0;
const delay = Math.min(8000, 500 * 2 ** attempt);
const wait = Math.max(delay, Number.isFinite(retryAfterMs) ? retryAfterMs : 0);
await new Promise((resolve) => setTimeout(resolve, wait + Math.random() * 250));
}Troubleshooting checklist
- Confirm the key can list the requested model.
- Validate the request against the selected public API format.
- Check the error source before changing your gateway balance, API key, or request.
- Check account quota, key quota, expiration, and IP restrictions.
- Capture the timestamp, model ID, status, and gateway request ID.