Error Code Reference

Complete guide to AI API gateway error codes. Find causes, meanings, and solutions for common errors.

Error Code Reference

Code Name Description Solution
400 Bad Request Invalid request format or missing parameters Check request body, JSON syntax, required fields
401 Unauthorized Invalid or missing API key Verify API key is correct and included in header
403 Forbidden API key lacks required permissions Check key permissions and scope settings
429 Rate Limited Too many requests in time window Implement exponential backoff, reduce request rate
500 Server Error Internal gateway or upstream error Retry with backoff, contact support if persists
502 Bad Gateway Upstream service unavailable Check backend service status, retry later
503 Service Unavailable Gateway is overloaded or maintaining Implement circuit breaker, retry after delay
504 Gateway Timeout Upstream took too long to respond Increase timeout, optimize request size

Error Handling Flow

1. Detect

Identify error type

2. Classify

Retryable or not

3. Handle

Apply strategy

4. Report

Log and alert

5. Recover

Resume operation

Best Practices

1 Always Implement Retry with Backoff

Use exponential backoff for transient errors. Start with 1s delay, double each retry, max 3-5 attempts. retry: { max: 3, backoff: 'exponential' }

2 Distinguish Retryable Errors

Only retry 429, 500, 502, 503, 504. Never retry 400, 401, 403 as they indicate client issues requiring code changes.

3 Implement Circuit Breaker

After N consecutive failures, stop making requests temporarily. This prevents cascade failures and allows services to recover.

4 Log and Monitor Errors

Track error rates, types, and patterns. Set up alerts for unusual spikes that may indicate systemic issues.

Frequently Asked Questions

What's the difference between 502 and 503?
502 means the upstream server responded with an error. 503 means the gateway itself is overloaded or down for maintenance.
Should I retry on 401 errors?
No. 401 indicates invalid credentials. Retrying won't help - you need to fix the API key or authentication configuration.
How should I handle rate limit errors?
Implement exponential backoff with jitter. Respect Retry-After header if present. Consider caching responses to reduce requests.

Related Resources

Exception Handling

Error handling

Retry Strategies

Backoff patterns

Prompt Caching

Reduce errors

Home

Back to hub