# Rate Limits

> The Rate Limits are restrictions on how many requests or tokens can be sent to an AI API in a period of time.

## How do I handle rate limits (HTTP 429) from an AI API?

The limits on requests and tokens per minute or day set by API providers. Exceeding them returns the '429' status code, so applications should retry with exponential backoff and queue requests.

- Topic: APIs & Integrations
- Type: concept

## Example

Wrong:

```js
async function call(req) {
  let res = await fetch(URL, req);
  while (res.status === 429) res = await fetch(URL, req);
  return res;
}
```

Right:

```js
async function call(req, attempt = 0) {
  const res = await fetch(URL, req);
  if (res.status !== 429 || attempt >= 5) return res;
  const retryAfter = Number(res.headers.get('retry-after')) * 1000;
  const delay = retryAfter || 2 ** attempt * 1000 + Math.random() * 500;
  await new Promise((r) => setTimeout(r, delay));
  return call(req, attempt + 1);
}
```

Retries respect 'retry-after', use exponential backoff with jitter and stop after a fixed number of attempts.

## Common mistakes

- Retrying instantly in a tight loop, which keeps hitting the limit and can make throttling last longer.
- Counting only requests and forgetting that token limits apply too.

## Key points

- Requests that get a '429' response are retried with exponential backoff and jitter, with a maximum number of attempts.
- The 'retry-after' header is read and respected when the API sends it.
- Concurrency is capped with a queue instead of firing all requests at once.

## Interview questions

### What is the difference between RPM and TPM limits?

RPM limits requests per minute, TPM limits tokens per minute. A few large prompts can hit the token limit while the request count stays low.

### Should I retry on HTTP 500 errors from an AI API?

Yes, retry server errors and timeouts with backoff, like 429. Do not retry 400-level errors such as invalid requests or auth failures, except 429.

### How can I get higher rate limits?

Limits usually grow with usage tier or account spend. Meanwhile, cache results, batch offline work and spread load with a queue.

## Related cards

- [4xx](https://wonderium.dev/web/4xx.md)
- [LLM API](https://wonderium.dev/ai/llm-api.md)
- [AI Gateway](https://wonderium.dev/ai/ai-gateway.md)
- [Token Cost](https://wonderium.dev/ai/token-cost.md)
- [Jitter](https://wonderium.dev/web/jitter.md)

---

- Level: [Artificial Intelligence](https://wonderium.dev/ai.md)
- HTML version: https://wonderium.dev/ai/rate-limits
- Last updated: 2026-11-25
