Error handling
Last updated: 2025-01-15
•1 min readError Handling
Handle API errors gracefully in your applications.
Error Response Format
{
"error": {
"code": "error_code",
"message": "Human-readable message",
"details": {}
}
}
Common Error Codes
| Code | HTTP Status | Description |
|---|---|---|
invalid_api_key |
401 | Invalid or missing API key |
rate_limit_exceeded |
429 | Too many requests |
quota_exceeded |
402 | Monthly quota exhausted |
invalid_request |
400 | Malformed request |
model_unavailable |
503 | Model temporarily unavailable |
server_error |
500 | Internal server error |
Best Practices
try {
const response = await taskforceai.chat(message);
} catch (error) {
if (error.code === 'rate_limit_exceeded') {
// Wait and retry
await sleep(error.retryAfter);
return retry();
}
if (error.code === 'quota_exceeded') {
// Upgrade or wait for reset
notifyUser('Quota exceeded');
}
// Log and handle other errors
console.error(error);
}
Retry Strategy
Implement exponential backoff:
- Wait 1 second, retry
- Wait 2 seconds, retry
- Wait 4 seconds, retry
- Give up after 5 attempts