What Error Responses Look Like
Every error follows the same format, so you always know what you’re dealing with:Breaking It Down
- code: The HTTP status code (because standards matter)
- message: What actually went wrong, in human speak
- type: Error category for your code to handle programmatically
- metadata: Extra details when you need to dig deeper (optional)
The Status Code Cheat Sheet
2xx: Everything’s Cool
200 OK
Your request worked perfectly. Pat yourself on the back.4xx: You Did Something Wrong (Don’t Panic)
400 Bad Request
Your request was malformed or had invalid parameters.- Forgot required parameters
- Parameter values are out of bounds
- Your JSON is wonky
- Model name doesn’t exist
401 Unauthorized
Your API key is wrong, missing, or revoked.- Double-check your API key isn’t typo’d
- Make sure you didn’t accidentally commit a revoked key
- Verify the Authorization header format is correct
402 Payment Required
You’re out of credits. Time to top up.- Add more credits to your account
- Switch to a cheaper model
- Reduce max_tokens to lower costs
403 Forbidden
Content moderation blocked your request.- Rephrase your prompt to remove problematic content
- Try a different model with different moderation rules
- Contact support if this seems like a false positive
408 Request Timeout
Your request took too long and we gave up.- Lower your max_tokens setting
- Simplify your prompt
- Try again (sometimes it just works the second time)
- Use streaming to get partial results
422 Unprocessable Entity
Your request is valid but the model can’t handle what you’re asking.429 Too Many Requests
Slow down there, speed racer. You’re hitting the rate limit.- Wait for the retry_after time (we tell you exactly how long)
- Implement exponential backoff in your code
- Upgrade to a higher tier for more requests
- Spread your requests over time
5xx: We Screwed Up (Sorry)
500 Internal Server Error
Something went wrong on our end.502 Bad Gateway
The AI model you requested is having a bad day.- Try a different model
- Wait a few minutes and try again
- Use our fallback models feature (it handles this automatically)
503 Service Unavailable
No providers are available for your requested model.Smart Error Handling Patterns
1. Exponential Backoff (The Right Way)
2. Handle Specific Errors Like a Pro
3. Smart Fallback Models
Provider-Specific Errors
Sometimes you get the raw error from the AI provider:Logging & Monitoring (Because Debugging Sucks)
Track Request IDs
Every error includes a request ID. Use it to track down issues:Monitor Error Patterns
Testing Your Error Handling
Pro Tips for Error Handling
1. Graceful Degradation
Always have a Plan B when the API is down:2. User-Friendly Error Messages
Don’t show users raw error codes:3. Smart Timeouts
Set timeouts that match your use case:4. Circuit Breaker Pattern
Stop hammering broken services:Monitor These Metrics
Set up alerts for:- Error rate spikes - Something’s going wrong
- Specific error patterns - Identify systemic issues
- Model availability - Know when your preferred models are down
- Rate limit proximity - Upgrade before you hit the wall
Remember: Good error handling is invisible to users but saves your sanity. Handle errors gracefully, log everything you need for debugging, and always have a fallback plan.