How to Implement a Rate Limiter (Per-User and Global)
What a rate limiter does, how token buckets work, and how to run per-user and global limits safely, explained simply for your AI interview.
In short
A rate limiter lets each request through only if a limit allows it. I'd give every user a token bucket and the whole app one more, and let a request pass only when both have a token. Refused requests get a 429 error, and clients wait before they retry.
12 min readIntermediateVery common · estimateFacts checked 26 November 2026
Say it out loud
Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.
A rate limiter lets each request through only if a limit allows it. I'd keep a token bucket for each user, so one customer can't take everything, and one for the whole app, to protect the vendor's limit and our bill. A request passes only when both have a token. A refused request gets a 429 error, and the client waits before it retries.
Picture a bank's chat assistant. Every customer message calls a language model, which costs money and has a limit set by its vendor. One customer's broken script sends 300 messages in a minute. Without a limit, that one customer uses the whole allowance and everyone else gets errors. A rate limiter counts requests and lets one through only when a limit allows it.
I'd use token buckets. Each bucket holds a set number of tokens, and each request takes one. Tokens drip back in at a steady rate, up to the bucket's size, so a customer can send a short burst and then only a steady trickle. I'd keep one bucket per user and one for the whole app. A request passes only when both have a token, and I take from each only after both say yes, so a refused request doesn't waste anyone's allowance.
When a request is refused, the client gets a 429 error. If the response has a Retry-After header, the client waits that many seconds. Either way it adds a small random delay, so blocked clients don't all retry at the same moment. Across several servers, I'd keep the counts in one shared store, so ten servers can't give one customer ten allowances.
The numbers are a starting guess that I'd tune with real traffic. I'd also count each logged-in customer as its own sender, because many people can share one internet address (an IP address).
What is a rate limiter, in plain words?
A rate limiter is a gatekeeper that counts requests. For each one, it asks: has this sender used up its allowance for now? If yes, the request is refused or delayed. If no, it goes through and the count goes up.
Think of a bank branch's queue ticket machine. It releases tickets at a steady pace, so the branch never has more people than its tellers can serve. One person pressing the button ten times should not get ten tickets ahead of everyone else.
In an AI product, the stakes are real. Each model call costs money and counts against a limit the vendor sets. One broken script can flood a chat assistant, and then the whole app hits the vendor's limit and stops working for everyone.
Key words
- Rate limit
- A cap on how many requests may pass in a set time, for one sender or for the whole app.
- Token bucket
- A bucket of tokens. Each request takes one, and tokens refill over time up to its size.
- Per-user limit
- A separate bucket for each customer, so one person can't use up the app.
- Global limit
- One bucket for the whole app, which protects the service and the vendor's limit.
How does a token bucket work?
Each bucket has a size, like 5, and a refill rate, like 1 a second. It starts full. Each request takes one token out. If the bucket is empty, the request waits or is refused. Tokens drip back in as time passes, up to the size. These are limiter credits, not the words a model reads. Anthropic's limits, for example, count requests and tokens per minute separately.
“The API uses the token bucket algorithm to do rate limiting.”
Anthropic, rate limits page
“This means that your capacity is continuously replenished up to your maximum limit, rather than being reset at fixed intervals.”
Anthropic, rate limits page
A simpler method is a counter that resets every minute. It fails at the edge of the minute. A customer can use a full allowance at 0:59 and another at 1:00. The bucket has no such reset.
Fixed window and token bucket
Fixed window
Counter resets each minute
100 at 0:59, then 100 at 1:00
Token bucket
Tokens refill over time
no sudden reset to exploit
200 pass in seconds
twice the limit at the edge
Steady flow, short bursts
a burst can only use the bucket
- 1Each minute's counter starts again at zero, so a burst at the edge passes twice.
- 2The bucket refills bit by bit, so there is no reset to exploit.
Why do you need a per-user and a global limit?
A per-user limit stops one customer from taking the whole app. A global limit protects the whole service and the vendor's limit on your account. Anthropic's API enforces limits at the organisation level, and you can set your own for each workspace. Anthropic also warns that short bursts of requests can exceed the limit and trigger rate limit errors.
The rule is simple but easy to get wrong. A request passes only when both buckets have a token. Check both before you take anything. If you take the user's token first and the global check then refuses, that customer lost a token for nothing.
- 1
Keep one bucket per user
A table that maps each user ID to that user's bucket. A new user gets a full bucket the first time they appear.
- 2
Keep one bucket for the app
It is shared by every request, so it limits the whole service.
- 3
Refill both to the current time
Add the tokens earned since the last request, up to each bucket's size.
- 4
Refuse if either is empty
Return a 429 and say which limit was hit, so the client and the logs can tell.
- 5
Take one token from each
Only when both had one. Then let the request through.
Here it is in plain Python, for a made-up bank's chat assistant. Time is passed in, so the output is the same every run.
# A bank's chat assistant: token buckets per customer and for the whole app. Time is passed in, so every run prints the same.
class Bucket:
def __init__(self, size, rate, now):
self.size, self.rate, self.tokens, self.last = size, rate, size, now
def refill(self, now):
self.tokens = min(self.size, self.tokens + (now - self.last) * self.rate)
self.last = now
USER_LIMIT = (5, 1) # up to 5 at once, then 1 back each second
APP_LIMIT = (10, 4) # the whole app: 10 at once, then 4 back each second
users, app = {}, Bucket(*APP_LIMIT, 0)
def allow(user, now):
mine = users.setdefault(user, Bucket(*USER_LIMIT, now))
for bucket in (mine, app):
bucket.refill(now)
if mine.tokens < 1:
return f"BLOCKED {user}: over their own limit"
if app.tokens < 1:
return f"BLOCKED {user}: the whole app is busy"
mine.tokens -= 1
app.tokens -= 1
return f"ALLOWED {user}"
calls = [("ana", 0)] * 5 + [("ben", 0)] * 5 + [("cy", 0), ("ana", 0), ("cy", 1)]
for user, now in calls:
print(allow(user, now))Output
ALLOWED ana ALLOWED ana ALLOWED ana ALLOWED ana ALLOWED ana ALLOWED ben ALLOWED ben ALLOWED ben ALLOWED ben ALLOWED ben BLOCKED cy: the whole app is busy BLOCKED ana: over their own limit ALLOWED cy
Read the output. Ana and Ben each use their own five tokens, and together they use up the app's ten. So Cy is refused by the whole app, and Cy's own limit is still fine. Ana's sixth call is refused by her own limit. A second later the app bucket has refilled a little, and Cy gets through.
How one request passes two checks
Customer message
one request from one customer
Two buckets
Their own bucket
5 at once, 1 back a second
Shared app bucket
10 at once, 4 back a second
Both have a token
take one from each, let it through
Either is empty
refuse with a 429
- 1The message goes to the limiter, which checks both buckets.
- 2Both have a token, so one is taken from each and the message goes through.
- 3One is empty, so the message is refused and the client is told to wait.
What does a blocked client get back?
The server answers with a 429 error. OpenAI's guide describes the same flow: a request over a temporary limit gets a 429, and the client should then wait and retry.
“When a request exceeds a temporary rate limit, the API returns a 429 error.”
OpenAI, rate limits guide
The client should then wait. Retry-After is a reply header that says how many seconds to wait. OpenAI calls that value the minimum time to wait, and says to add a small random delay on top, so clients don't all retry at the same moment.
The random part matters. If a hundred clients are blocked together and all wait exactly one second, they all retry together and get blocked again. AWS's architecture blog says the fix is to keep the backoff and add jitter, which is a little randomness in the wait.
Tip
Asked how a client should handle a 429? Say: wait for Retry-After, back off with jitter, and stop after a few tries so a broken script can't loop forever.
How would you run this across ten servers?
A table in one server's memory knows nothing about the other nine. A customer who spreads requests across all ten gets ten allowances. So I'd keep the buckets in a shared store that every server reads and writes, and make each check and update one step that can't be split in two.
Pick the sender key carefully too. Cloudflare warns that visitors who clear cookies, use private browsing or refuse cookies are not individually identified. They share one counter, which can cause false positives on busy shared networks. So for a bank's app I'd count per logged-in customer.
What does the interviewer want to hear?
A strong answer covers
- A plain definition: a rule that lets each request through only when a limit allows it.
- A token bucket: its size, its refill rate and how a short burst works.
- Two limits, per user and global, and why you need both.
- Check both buckets before taking a token from either.
- The 429 reply, the Retry-After wait and jitter on retries.
- Shared state across servers, and the sender key you pick.
Common traps
Trap"Count requests per minute and reset the count at the top of the minute."
BetterA burst at the edge passes twice. A token bucket refills gradually and avoids that.
Trap"Take the user's token first, then check the global limit."
BetterA refused request has already spent a token. Check both, then take from both.
Trap"If you get a 429, just retry straight away."
BetterWait for Retry-After, add jitter, and stop after a few tries.
Can you spot the bug in the limiter?
Spot the wrong step
Which of these four steps is wrong in a per-user and global limiter?
Quick check
A bank's chat assistant gets 300 calls in one minute from one customer's broken script. The per-user bucket is 5 at once, then 1 a second. What should happen?
Can you answer these from memory?
Flash cards · tap a card to flip it
What will they ask next?
- Q330How do you handle rate limits and 429 errors?Coming soon
- Q337Timeouts, retries and backoff?Coming soon
- Q356Design rate limiting.Coming soon
- Q473Run an LLM over 50,000 documents with concurrency limits and 429 retries.Coming soon
- Q187How do you control agent cost?13 min
Learn it in depth
Last updated on 26 November 2026
Building an AI service that many people share?
I design and build AI products and agents, including the limits, retries and cost controls behind them. Book a short call and tell me what yours needs to handle.