Lesson 69.2 · 2 of 5 · 4 min read · Reviewed 24 Nov 2026
Rate-limit budgeting across users and tenants
By the end, you can
- Split a shared rate-limit budget between users and tenants.
- Prevent one heavy user starving everyone else.
The provider sees one organisation. You have many users, tenants and features sharing that one limit.
Budget the limit
Suppose your limit is 400,000 input tokensInput tokens: The tokens you send in a request: system prompt, tool definitions, conversation history and attached content. per minute.
| Consumer | Share | Why |
|---|---|---|
| Live chat (all tenants) | 60% | People are waiting |
| Agent jobs | 25% | Can queue briefly |
| Batch and backfills | 0% here | Run through the batch interface instead |
| Headroom | 15% | Retries and spikes |
Within live chat, cap each tenant, for example at 10% of the pool, so no one tenant can starve the rest.
Token buckets
A tokenToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id. bucket holds up to N units and refills at a steady rate. Each request takes units equal to its estimated tokens. If the bucket is empty, the request waits or is rejected. This allows short bursts while holding the average.
bucket(tenant) capacity 40k, refill 20k per minute
request needs ~3k tokens -> take 3k if available, else queue
Estimate before you send
You know the input size before the call; count or estimate it. Output is unknown, so reserve max_tokens and give back what wasn’t used when the response arrives.
Watch the headers
Provider responses tell you how much budget remains. Feed that back into your limiter so it slows down before you hit the wall (69.5).
Key points
- Your provider rate limit is one shared budget: requests and tokens per minute.
- Give each tenant or feature its own slice, enforced in your code before calling the API.
- Token buckets are a simple way to allow short bursts while holding a steady average.
Give each tenant its own token bucket
Stop one heavy tenant from using the whole rate limit by enforcing per-tenant budgets in code.
You will have: A Bucket class and a simulation comparing one shared bucket with per-tenant buckets.
Steps
- Run the starter: allow() lets everything through.
- Implement refill, cap and spend in allow().
- Run both modes and compare how often tenant-b gets through.
- Change the split (for example 30% and 20%) and rerun.
- Note where you would estimate token cost before sending a real request.
Done when
- With one shared bucket, tenant-b gets only 12 of its 20 requests through.
- With per-tenant buckets, tenant-b gets all 20 requests through.
- tenant-a is held near its 25% slice.
Open in the Playground No API key needed.
Starter code (Python)
# Split one shared token-per-minute limit into per-tenant token buckets.
LIMIT_PER_MIN = 120_000
class Bucket:
def __init__(self, per_min):
self.rate = per_min / 60 # tokens added per second
self.capacity = per_min / 6 # allows a 10-second burst
self.tokens, self.last = self.capacity, 0.0
def allow(self, cost, now):
# TODO: refill for the time since self.last (capped at capacity), update self.last,
# then allow the call only if there are enough tokens, and spend them.
return True
# tenant-a runs a bulk job; tenant-b sends a few normal requests.
EVENTS = sorted([(round(i * 0.1, 1), "tenant-a", 2500) for i in range(600)] +
[(i * 6 + 0.05, "tenant-b", 2500) for i in range(20)])
def simulate(per_tenant):
if per_tenant: # chat keeps 50%; each tenant gets 25% of the limit
buckets = {"tenant-a": Bucket(LIMIT_PER_MIN * 0.25), "tenant-b": Bucket(LIMIT_PER_MIN * 0.25)}
else:
shared = Bucket(LIMIT_PER_MIN * 0.5)
buckets = {"tenant-a": shared, "tenant-b": shared}
allowed = {"tenant-a": 0, "tenant-b": 0}
for t, tenant, cost in EVENTS:
if buckets[tenant].allow(cost, t):
allowed[tenant] += 1
return allowed
for mode in (False, True):
a = simulate(mode)
label = "per-tenant buckets" if mode else "one shared bucket"
print(f"{label:<18}: tenant-a allowed {a['tenant-a']} of 600, tenant-b allowed {a['tenant-b']} of 20")
Show a solution
# Split one shared token-per-minute limit into per-tenant token buckets.
LIMIT_PER_MIN = 120_000
class Bucket:
def __init__(self, per_min):
self.rate = per_min / 60 # tokens added per second
self.capacity = per_min / 6 # allows a 10-second burst
self.tokens, self.last = self.capacity, 0.0
def allow(self, cost, now):
self.tokens = min(self.capacity, self.tokens + (now - self.last) * self.rate)
self.last = now
if self.tokens >= cost:
self.tokens -= cost
return True
return False
# tenant-a runs a bulk job; tenant-b sends a few normal requests.
EVENTS = sorted([(round(i * 0.1, 1), "tenant-a", 2500) for i in range(600)] +
[(i * 6 + 0.05, "tenant-b", 2500) for i in range(20)])
def simulate(per_tenant):
if per_tenant: # chat keeps 50%; each tenant gets 25% of the limit
buckets = {"tenant-a": Bucket(LIMIT_PER_MIN * 0.25), "tenant-b": Bucket(LIMIT_PER_MIN * 0.25)}
else:
shared = Bucket(LIMIT_PER_MIN * 0.5)
buckets = {"tenant-a": shared, "tenant-b": shared}
allowed = {"tenant-a": 0, "tenant-b": 0}
for t, tenant, cost in EVENTS:
if buckets[tenant].allow(cost, t):
allowed[tenant] += 1
return allowed
for mode in (False, True):
a = simulate(mode)
label = "per-tenant buckets" if mode else "one shared bucket"
print(f"{label:<18}: tenant-a allowed {a['tenant-a']} of 600, tenant-b allowed {a['tenant-b']} of 20")
Do it Build a rate budget in the Playground in the Playground. No API key needed.