Skip to content

Error Handling

This guide covers common API errors and how to handle them properly in your applications.

Error Response Format

All errors follow a consistent JSON format:

json
{
  "error": {
    "message": "Invalid API key provided",
    "type": "authentication_error",
    "code": "invalid_api_key"
  }
}

HTTP Status Codes

CodeMeaningSolution
400Bad RequestCheck request format and parameters
401UnauthorizedVerify your API key
402Payment RequiredTop up your account
403ForbiddenCheck API key permissions
404Not FoundVerify the endpoint URL and model ID
429Rate LimitedWait and retry, or check Rate Limits
500Server ErrorRetry after a short delay
502Bad GatewayUpstream provider issue, retry
503Service UnavailableTemporary outage, retry shortly

Common Errors

401 — Invalid API Key

json
{"error": {"message": "Invalid API key provided", "type": "authentication_error"}}

Fix:

  • Check your API key is correct (no extra spaces)
  • Ensure the key is active on the API Keys page
  • Use the correct header format: Authorization: Bearer sk-xxx

402 — Insufficient Credits

json
{"error": {"message": "Insufficient credits", "type": "billing_error"}}

Fix:

429 — Rate Limited

json
{"error": {"message": "Rate limit exceeded", "type": "rate_limit_error"}}

Fix:

  • Implement exponential backoff
  • Reduce request frequency
  • See Rate Limits

404 — Model Not Found

json
{"error": {"message": "Model not found: invalid-model", "type": "not_found_error"}}

Fix:

  • Check the model ID spelling
  • Use the /models endpoint to list available models
  • See the Models page

Retry Strategy

Implement exponential backoff for transient errors (429, 500, 502, 503):

python
import time
from openai import OpenAI, APIError, RateLimitError

client = OpenAI(
    api_key="sk-your-toprouter-key",
    base_url="https://toprouter.cc"
)

def chat_with_retry(messages, max_retries=3):
    for attempt in range(max_retries):
        try:
            return client.chat.completions.create(
                model="google/gemini-3.5-flash",
                messages=messages
            )
        except RateLimitError:
            wait = 2 ** attempt
            print(f"Rate limited. Retrying in {wait}s...")
            time.sleep(wait)
        except APIError as e:
            if e.status_code >= 500:
                wait = 2 ** attempt
                print(f"Server error. Retrying in {wait}s...")
                time.sleep(wait)
            else:
                raise
    raise Exception("Max retries exceeded")

TIP

For production applications, use a library like tenacity (Python) or implement circuit breaker patterns for robust error handling.

Unified AI API Gateway — Access 200+ models through one endpoint.