Topic 2 of 5 · 5 min read
Fair quota sharing across tenants
Goal: Design per-tenant limits so a single heavy tenant cannot exhaust a shared provider quota.
Give every tenant its own limit inside the shared provider quota, so one heavy customer or one runaway agent cannot starve everyone else. Your provider limit belongs to your account. Your customers do not know about it, and they will share it unless you divide it.
Check yourself · 1 of 3
What problem do per-tenant limits solve in a multi-customer LLM product?
Why it matters
A single key serves all your users. If one tenant starts a bulk import or an agent loops on tool calls, that tenant uses the tokens per minute that every other tenant needed. The provider returns 429s to everyone, and the quiet customers file the support tickets. This is the noisy-neighbour problem, and the fix lives in your gateway, not at the provider.
How to do it
- Pick the unit. Count tokens, not requests. One request can carry 200 tokens or 200,000.
- Give each tenant a token bucket. A bucket has a refill rate (the sustained limit) and a size (the burst allowed). Take tokens out before the call using an estimate of input plus the maximum output, then refund the difference after the response.
- Set limits by plan. For example free, standard and enterprise tenants get different refill rates. Keep the sum of the guaranteed rates below your provider limit, with headroom.
- Keep a shared pool. Unused guaranteed quota can be lent out, but lending should be revocable the moment its owner needs it.
- Add a global guard. A bucket for the whole account sits below the provider limit, so you queue or reject on your side and your error message is under your control.
- Limit agents separately. Cap tokens and steps per run, so one looping run cannot drain a tenant's bucket in minutes.
- Tell the caller. Return a clear error with the reset time, not a vague failure.
Before and after
Weak: one shared counter. A tenant launches a nightly re-index at 09:00 and uses 80% of the quota. Every user of the chat feature sees errors.
Better: each tenant has its own bucket sized by plan, with a small burst allowance. The re-index slows down on its own bucket, and chat for other tenants keeps working.
What goes wrong
- Counting requests. A tenant with huge prompts stays under a request limit and still uses most of the tokens.
- Charging after the call only. Many parallel calls all pass the check, then together exceed the limit. Reserve first, settle later.
- Guaranteeing more than you own. If guaranteed rates add up to more than the provider limit, the guarantees are fiction.
- No visibility. Without per-tenant usage metrics you find the heavy tenant only after the outage.
Examples
A per-tenant token bucket
class Bucket:
def __init__(self, rate_per_s, size):
self.rate, self.size = rate_per_s, size
self.tokens, self.t = size, time.monotonic()
def take(self, n):
now = time.monotonic()
self.tokens = min(self.size, self.tokens + (now - self.t) * self.rate)
self.t = now
if self.tokens < n:
return False # reject or queue this call
self.tokens -= n
return True
buckets = {t: Bucket(**PLAN[plan_of(t)]) for t in tenants}
if not buckets[tenant].take(est_input + max_output):
raise TenantLimit(retry_after=5)
After the response, add back max_output - actual_output so the bucket matches real use.
Plan limits that fit the provider quota
The provider allows 2,000,000 tokens a minute. Guaranteed tenant rates: 20 standard tenants at 40,000 a minute (800,000), 5 enterprise tenants at 100,000 (500,000), 200 free tenants at 1,000 (200,000). The total is 1,500,000, which leaves 500,000 as shared burst and retry headroom.
Key points
- A provider quota is shared by all your customers unless your own gateway divides it.
- Limit by tokens, not requests, because request size varies by orders of magnitude.
- Reserve estimated tokens before the call and refund the difference afterwards, so parallel calls cannot overshoot.
- Keep guaranteed per-tenant rates below the provider limit and lend unused quota only in a way that can be taken back.
- Cap tokens and steps per agent run so a loop cannot empty a tenant's bucket.
Common mistakes
- Rate limiting by request count while tenants send prompts of very different sizes.
- Checking the limit after the call, so parallel requests all pass and together exceed it.
- Promising per-plan guarantees whose total is more than the quota the provider gave you.
- Applying one limit to the whole account and not to each tenant or each agent run.
Check yourself · 2 of 3
Tenant A has a bucket of 100,000 tokens. A call is expected to use 2,000 input tokens and may return up to 1,000. What should the gateway take from the bucket before the call?
Check yourself · 3 of 3
Your provider quota is 1,000,000 tokens a minute. Which plan setup is sound?
Practice items, not counted towards your mastery.
Start here
- Rate limits (Claude API docs) (opens in a new tab)DocsFree
Request and token limits per model, usage tiers, 429 errors and the retry-after and remaining-quota headers.
- Rate limits (OpenAI API) (opens in a new tab)DocsFree
How requests per minute, tokens per minute and usage tiers work, and how to back off after a 429.
- Batch processing (Claude) (opens in a new tab)DocsFree
How the asynchronous batch endpoint works, its discount and its completion window.