Rate limits and quotas in APIs
easy 22 minA limit is part of an API's contract.
Video lesson · Chapter 1 of 6
Spend a bucket of tokens
429: what it says and what to do, in 75 seconds
1:15Reel coming soon
Theory
Read or listen first. About 2 min.
What it is
A rate limit says how many calls one caller may make in a period, for example 100 a minute. A quota is the same idea over a long time, such as 10,000 a month. A budget counts money instead of calls.
This topic is about using limits well: what to promise, how to say no, and how a client should react. How a limiter counts, and how many servers share one count, is in rate limiter.
Why it exists
Every API has a capacity, and many share it. One broken loop in one customer's script can use all of it. A limit keeps service fair, protects what is behind it, and keeps the bill predictable.
Refusing is not enough. A client that is refused without explanation tries again at once, which makes things worse. Clear limits and headers turn angry retries into calm waiting.
A short history
January 1997
HTTP/1.1 (RFC 2068) includes the Retry-After header.
April 2012
RFC 6585 adds status 429 Too Many Requests, which may carry Retry-After.
March 2015
Amazon's Marc Brooker publishes measurements of exponential backoff with jitter: random waiting keeps clients from returning together.
November 2026
Standard RateLimit headers are still being written: the IETF draft reaches version 12 after several changes of format.
Words to know
- RPM
- Requests per minute.
- TPM
- Tokens per minute: units of text, used by language model APIs, where one request can be small or huge.
- Burst
- How many calls may come at once before the steady rate applies.
- Quota
- An allowance over a long period, such as a month.
- Tier
- A level of service with its own limits, such as free or pro.
- 429
- Status code: too many requests from this caller.
- Retry-After
- Header: wait this many seconds, or until this date.
- Pacing
- A client spreading its calls so that it never hits the limit.
- Jitter
- A random part in a waiting time, so clients do not all return together.
How it works
Step through the idea, or press Play.
Publish. Limits per tier, in the docs and in response headers
Understand it
Start here
The water fountain story
Think of the water fountain at school. The water pressure is limited, and everyone is thirsty.
The important part is the sign: it says how long, so nobody has to push.
Four reasons to limit
Fairness
One caller cannot use what belongs to all.
Protection
A broken loop or a guessing attack is stopped early.
Cost
Expensive calls cannot run without end.
Product
Plans differ in what they allow.
Choose
What to count, and for whom
What to count
| Unit | Good for | Weakness |
|---|---|---|
| Requests | Calls that cost about the same | A heavy report counts like a tiny read |
| Points or tokens | Calls of very different size | Cost must be estimated before the call |
| Calls in flight | Slow calls, uploads, exports | Needs a release when the call ends, or a timeout |
| Bytes | Files and streaming | Known only at the end |
| Money | Paid services behind your API | Prices change |
For whom
| Key | Use | Watch out |
|---|---|---|
| API key or user | Normal choice after authentication | One person may hold several keys: add a limit for the account |
| Account or organisation | Business plans | One member can use up everyone's share |
| IP address | Before login | A whole school can share one address; use your proxy's trusted address, not a header the client sends |
| Account name at login | Guessing passwords | Do not lock out the owner for ever |
| Endpoint | Costly or risky routes | Add to the others, not instead of them |
Which limits does this endpoint need?
Tiers
Different limits for different plans
An example plan table
| Plan | Requests a minute | Tokens a minute | Budget a day |
|---|---|---|---|
| Free | 3 | 10,000 | 1 USD |
| Pro | 60 | 100,000 | 50 USD |
| Enterprise | 500 | 1,000,000 | 500 USD |
Can we keep the promise?
One request against three limits
Architecture
Limits in layers
Each layer stops what it understands
| Layer | Limit | Stops |
|---|---|---|
| CDN or edge | Per address | Floods and simple bots |
| API gateway | Per key, per plan | Broken scripts, unfair use |
| Service | Calls in flight, points | Slow and heavy work |
| Budget | Money a day or month | Surprise bills |
The counters themselves must be shared and atomic, and somebody must decide what happens when the store fails. Both are in rate limiter.
Contract
How to say no
HTTP/1.1 429 Too Many RequestsContent-Type: application/problem+jsonRetry-After: 20X-RateLimit-Limit: 60X-RateLimit-Remaining: 0X-RateLimit-Reset: 1735689620{"type": "https://api.example.com/problems/rate-limit", "title": "Too many requests", "status": 429, "detail": "60 requests a minute on the pro plan. Try again in 20 seconds.", "limit": "requests"}The headers
| Header | Means | Status |
|---|---|---|
Retry-After | Wait this many seconds, or until a date | Standard (RFC 9110) |
X-RateLimit-Limit, -Remaining, -Reset | Allowance, what is left, when it renews | Convention. Reset is a timestamp in some APIs and seconds in others |
RateLimit-Policy: "basic";q=100;w=60 | The rule: quota 100 per 60 seconds | IETF draft 12 |
RateLimit: "basic";a=60;w=58 | Now: 60 left, for the next 58 seconds | IETF draft 12 |
Which status?
| Case | Status |
|---|---|
| This caller sent too many | 429 |
| The whole service is overloaded | 503, also with Retry-After |
| Quota for the month is used up | 429 with a clear reason; some APIs use 403 |
| The request itself is too large | 413, or 400 for too many items |
Softer
Serve less before refusing
A phone plan slows down when the data is used up instead of cutting you off. APIs can do the same: a cheaper model, an older cached answer, fewer results.
Example levels by use of the allowance
| Level | Free from | Pro from | Enterprise from | What is served |
|---|---|---|---|---|
| Full | 0% | 0% | 0% | Best quality |
| Reduced | 70% | 85% | 95% | Smaller model, cached answers allowed |
| Minimal | 90% | 95% | 99% | Cached answers only |
| Blocked | 100% | 100% | 100% | 429 |
What happens to a call
Money
Budgets and alerts
Cost of one call
- 1
Record
Per account: input, output, cost, time. Use decimal numbers, not floating point, for money.
- 2
Warn
Tell the owner at 50%, 75%, 90% and 100% of the budget.
- 3
Stop
A hard limit refuses new calls. Reserve the cost before the call, so that parallel calls cannot all pass the check.
- 4
Review
Look at who is near the limit, who never is, and what the next month will cost.
Client
How a good client behaves
Pace yourself
A refusal, handled well
Do
- Read the headers and slow down early
- Wait at least Retry-After, plus jitter
- Share one limiter between all your workers
- Give up after a few tries and report it
- Batch, cache and use webhooks instead of polling
- Ask for a higher limit before a launch
Do not
- Retry at once in a loop
- Start more workers when it gets slow
- Spread calls over many keys to get around a limit
- Ignore 429 because most calls work
- Retry a payment without an idempotency key
Waiting times without a header, backoff and retry budgets belong to retries.
Feel
A bucket, seen from outside
Many APIs use a token bucket: it holds up to a burst of tokens and refills at a steady rate. From the outside you notice two numbers: how much at once, and how much per second.
How long to wait
What each style feels like
| Style | Client sees | Good when |
|---|---|---|
| Token bucket | Short bursts pass, then a steady rate | Interactive apps |
| Sliding window | Smooth, no jump at the minute | Strict fairness |
| Fixed window | Everything renews at once, up to double across the edge | Simple quotas by day or month |
Operations
Choosing and changing limits
- 1
Measure first
Look at calls per caller for some weeks. A limit a little above the busiest honest caller hurts nobody.
- 2
Run in shadow
Count and log who would be refused, without refusing.
- 3
Announce
Tell the callers near the limit, and give a date.
- 4
Enforce slowly
Start high, lower in steps, and keep a switch to raise a limit for one account.
- 5
Watch
Share of 429 answers, callers above 80%, waiting time, and whether the limiter itself is healthy.
Summary
The whole topic in one minute.
Choose the unit that reflects cost, count it per caller, and publish the numbers. Combine a short limit against bursts, a long quota, and a budget in money where calls differ in price.
When a caller goes over, answer 429 with a waiting time, or give a smaller service. A good client reads the headers, spreads its calls, waits with jitter and gives up after a few tries.
- Count what costs you: calls, tokens, calls in flight, money. Use more than one.
- Key by account after login and by address before it. Check all limits, then take from all.
- Say no with 429, a reason and Retry-After. Show what is left on every answer.
- Serve less before serving nothing, and say so.
- A good client paces, waits with jitter, shares one limiter, and stops after a few tries.