Skip to content
Browse the docs

Guides · JavaScript · 6 min

Handle errors and rate limits

Every status and code the API returns, what each one means and which are worth retrying.

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

json
{
  "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

StatuscodeWhat to do
400invalid_requestRead message; a field is missing, malformed or not supported by the model.
400content_policyThe provider refused the request. Do not retry it unchanged.
401invalid_api_keyThe key is missing, mistyped, revoked or expired.
402insufficient_creditsAdd credit, or lower max_tokens so the hold fits.
402spend_limit_exceededThe key reached its own limit. Raise it or use another key.
403insufficient_scopeThe key does not have the scope the endpoint needs.
404model_not_foundThe id is wrong. Take ids from GET /v1/models.
404model_unavailableThe model exists but has no live route for the API right now.
429rate_limit_exceededWait for retry-after seconds, then retry.
502provider_unavailableThe 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
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

javascript
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, 502 and dropped connections.
  • Do not retry 400, 401, 402, 403 or 404; 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.