Docs

Error Handling

Handle AnyPath gateway errors safely.

On this page
  1. Error envelope
  2. HTTP status classes
  3. Public error codes
  4. Retry policy
  5. Model availability

Error envelope

OpenAI-compatible gateway endpoints return this JSON envelope for public gateway errors:

{
  "error": {
    "message": "Human-readable error description",
    "type": "error_code",
    "code": "error_code"
  }
}

At runtime type is identical to code. Client logic should branch on HTTP status and error.code, not on prose in message.

Anthropic-compatible and Gemini-compatible endpoints may preserve native protocol error shapes when the failure comes from protocol handling or an upstream provider.

HTTP status classes

StatusMeaningRetry
400Invalid request body, parameters, or unsupported fieldsNo; fix the request
401Missing authentication, or an invalid or expired keyNo; send a valid key
403Permission issue, suspended account, or spending limitNo; fix access or balance
404Model or resource not available to this keyNo; refresh model discovery
413Request body too largeNo; reduce input size
415Unsupported content typeNo; use supported content type
429Your rate limit, or the suppliers’ capacity for the model, is exhausted; the code says whichYes; respect Retry-After when present
502Upstream provider errorUsually yes with backoff
503Model unavailable, provider circuit open, or no route currently availableUsually yes with backoff
504Gateway or provider timeoutUsually yes with backoff

Public error codes

The full registry is maintained in Error Codes. Common codes include:

CodeStatusMeaning
auth_required401API key is missing
auth_invalid401API key is invalid
auth_expired401API key has expired
spending_limit_exceeded403Key spending limit or balance gate blocked the request
model_not_found404Requested model is not available to the current key
rate_limit_exceeded429Your own RPM/TPM limit was exceeded
supplier_rate_limited429You are within your limits; the suppliers of the model are at theirs
unsupported_request_field400Selected route does not support one or more request fields
provider_error502Upstream provider returned an error
model_unavailable503Model or route is temporarily unavailable
provider_timeout504Upstream provider timed out

Retry policy

Use bounded retries only for temporary failures:

import random
import time

def retry_delay_seconds(attempt: int, retryAfterHeader: str | None = None) -> float:
    if retryAfterHeader:
        return float(retryAfterHeader)
    return min(30, (2 ** attempt) + random.random())
  • Retry 429, 502, 503, and 504 with exponential backoff and jitter.
  • Respect the Retry-After response header when it is present.
  • Do not retry authentication, authorization, spending-limit, validation, or request-size errors without changing the request or configuration.
  • Log status, error.code, request ID, model, and route metadata. Do not log prompts, files, API keys, or raw provider credentials.

Model availability

Call GET /v1/models with the same API key before storing model configuration. If a later request returns model_not_found, refresh the model list for that key and route policy instead of guessing fallback model names.