Guides · JavaScript · 6 min
Handle errors and rate limits
Every error has an HTTP status, a JSON body in the OpenAI envelope and an x-request-id header. No error on this page results in a charge.
The envelope
{
"error": {
"message": "Rate limit exceeded for this API key.",
"type": "rate_limit_error",
"code": "rate_limit_exceeded",
"param": null
}
}
type is the family, code is the specific reason. Branch on code when you need to tell two cases in the same family apart.
Codes you will meet
| Status | code | What to do |
|---|---|---|
| 400 | invalid_request | Read message; a field is missing, malformed or not supported by the model. |
| 400 | content_policy | The provider refused the request. Do not retry it unchanged. |
| 401 | invalid_api_key | The key is missing, mistyped, revoked or expired. |
| 402 | insufficient_credits | Add credit, or lower max_tokens so the hold fits. |
| 402 | spend_limit_exceeded | The key reached its own limit. Raise it or use another key. |
| 403 | insufficient_scope | The key does not have the scope the endpoint needs. |
| 404 | model_not_found | The id is wrong. Take ids from GET /v1/models. |
| 404 | model_unavailable | The model exists but has no live route for the API right now. |
| 429 | rate_limit_exceeded | Wait for retry-after seconds, then retry. |
| 502 | provider_unavailable | The provider failed. Retry with backoff. |
Rate limits
Limits are per key and counted per minute. The default is 120 requests per minute. A rate-limited response looks like this:
HTTP/1.1 429 Too Many Requests
retry-after: 17
x-ratelimit-limit-requests: 120
x-ratelimit-remaining-requests: 0
Wait at least retry-after seconds. Sending more requests during that window does not extend it, but they will all be refused.
A retry policy that works
async function withRetry(call, attempts = 5) {
for (let i = 0; ; i++) {
try {
return await call();
} catch (err) {
const status = err?.status;
const retryable = status === 429 || status === 502 || status === undefined;
if (!retryable || i === attempts - 1) throw err;
const wait = Number(err?.headers?.["retry-after"]) || 2 ** i;
await new Promise((r) => setTimeout(r, wait * 1000 + Math.random() * 250));
}
}
}
- Retry
429,502and dropped connections. - Do not retry
400,401,402,403or404; the same request will fail the same way. - Add a little randomness to the delay so that many clients do not retry at the same instant.
- For image generation, send an idempotency key so a retry cannot charge twice.
Failover
When a model has more than one authorized route, Unbound may move a request to another route after a provider failure, before any output has been delivered. Failover never changes the model you asked for, never moves a request to a route with weaker privacy, and never retries a request that a provider refused on content grounds. If every route fails you receive 502.
Getting help
Include the x-request-id when you contact support. It identifies the request without anyone needing to see your prompt; prompts and outputs are not written to logs or usage records.