Skip to main content

Error Format

All errors follow a consistent JSON format:
{
  "error": {
    "message": "Human-readable error description",
    "type": "error_type"
  }
}

Error Types

HTTP StatusError TypeDescription
400invalid_request_errorMalformed request body or missing required fields
401authentication_errorMissing, invalid, or expired API key
403permission_error / forbidden_errorKey doesn’t have access to the requested provider or resource
429rate_limit_errorRate limit exceeded (RPM or daily cap)
502server_errorUpstream provider request failed
503server_errorAuthentication service temporarily unavailable

Common Errors and Solutions

401 — Invalid API Key

{
  "error": {
    "message": "invalid api key",
    "type": "authentication_error"
  }
}
Fix: Check that your API key is correct and hasn’t been revoked.

403 — Provider Not Allowed

{
  "error": {
    "message": "provider 'anthropic' is not allowed for this API key",
    "type": "forbidden_error"
  }
}
Fix: Your API key has an allowed_providers restriction. Use a different key or update the allowed providers via the management API.

429 — Rate Limited

{
  "error": {
    "message": "rate limit exceeded",
    "type": "rate_limit_error"
  }
}
Fix: Implement exponential backoff. Consider raising your key’s rate limit.

502 — Upstream Failed

{
  "error": {
    "message": "upstream request failed",
    "type": "server_error"
  }
}
Fix: The LLM provider returned an error or is unreachable. ARouter automatically handles key failover, but the provider itself may be experiencing issues. Retry or switch to a different provider.

Handling Errors in Code

from openai import OpenAI, APIStatusError, APIConnectionError

client = OpenAI(
    base_url="https://api.arouter.com/v1",
    api_key="lr_live_xxxx",
)

try:
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": "Hello!"}],
    )
except APIStatusError as e:
    if e.status_code == 401:
        print("Invalid API key")
    elif e.status_code == 429:
        print("Rate limited, backing off...")
    elif e.status_code == 502:
        print("Provider error, retrying...")
    else:
        print(f"API error {e.status_code}: {e.message}")
except APIConnectionError:
    print("Network error — check your connection")

Retry Strategy

For production applications, we recommend:
  1. Retry on 429 and 502 with exponential backoff
  2. Do not retry on 400, 401, 403 — these are permanent errors
  3. Set a max retry count (e.g., 3 attempts)
  4. Consider provider fallback — if one provider fails, try another via the provider/model format
import time
from openai import OpenAI, APIStatusError

def call_with_retry(client, request, max_retries=3):
    for attempt in range(max_retries):
        try:
            return client.chat.completions.create(**request)
        except APIStatusError as e:
            if e.status_code in (429, 502) and attempt < max_retries - 1:
                time.sleep(2 ** attempt)
                continue
            raise