API reference

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 response · json
{
  "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

SourceMeaning

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

StatusMeaningClient actionRetry
400Invalid requestCheck the request shape, model parameters, and content limits.No
401Authentication failedCheck the error source. Only account errors require checking your gateway API key; upstream errors concern the provider credentials.Check source
402Balance or billing limitCheck the error source. An upstream balance error concerns the provider account, not your gateway wallet.After resolution
403Access deniedFor account errors, check your key restrictions. For upstream errors, the provider must resolve its account permissions or quota.Check source
408 / 504Request or provider timeoutCheck the error source and retry cautiously. A streaming request may have produced partial output.Yes
413 / 422Request limit or validation failureReview the error category. Reduce input size or correct unsupported parameters; provider quota limits require provider recovery.Check category
404Route or resource not foundVerify the endpoint and resource identifier.No
429Rate or quota limitBack off for rate limits. Provider billing quota errors need a funded route or a quota reset, not a gateway top-up.Check category
500Gateway or upstream errorCheck the error source, retry with backoff, and retain the request ID for support.Yes
502Upstream failureRetry because another eligible channel may become available.Yes
503No route availableBack 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.

JavaScript · ts
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

  1. Confirm the key can list the requested model.
  2. Validate the request against the selected public API format.
  3. Check the error source before changing your gateway balance, API key, or request.
  4. Check account quota, key quota, expiration, and IP restrictions.
  5. Capture the timestamp, model ID, status, and gateway request ID.