44A rate limiter for an LLM API

mediumpython
45:00

44 A rate limiter for an LLM API

OpenAI candidates report a system design question about rate limiting (1 report). A guide lists a distributed rate limiter for the design round and an implementation for coding (2 sources). An ML engineer candidate had to find the bug in a Django app's rate limiter (1 report). This is practice in that shape, not a leaked question.

Set a 45-minute timer. Read one level at a time, and run only that level's tests until they pass: pytest problems/44_rate_limiter -k level1. Earlier levels must keep passing.

Times are seconds, as floats, and they never go backwards.

Level 1. A token bucket. TokenBucket(capacity, rate) starts full. allow(now, cost=1) first refills rate tokens for each second since the last call, never past capacity. Then, if the bucket holds at least cost, it spends cost and returns True. Otherwise it spends nothing and returns False. retry_after(now, cost=1) returns how many seconds from now a request of cost would pass: 0 if it passes now, the time to refill the tokens it is short by ((cost - tokens) / rate) if not, and math.inf if cost is more than capacity. A cost equal to capacity passes once the bucket is full. Asking spends nothing.

Level 2. Limits on tokens. LLMLimiter(rpm, itpm, otpm) keeps three token buckets: requests, input tokens and output tokens. Each holds one minute's limit and refills at the limit divided by 60 each second (so 2 requests a minute bring back one request every 30 seconds). start(now, input_tokens, max_tokens, cached_tokens=0) lets a request start only if all three have room for it: 1 request, input_tokens - cached_tokens input tokens, and max_tokens output tokens, reserved because the answer's length isn't known yet. If any one is short, take nothing from any of them and return None. Otherwise return a request id: 1 for the first request that starts, then 2, and so on. finish(now, request_id, output_tokens) gives back the output tokens the request reserved but didn't write (refill to now first), never filling the bucket past its capacity. A request that wrote more than it reserved gives back nothing and takes nothing more. Each id finishes once: a second finish for the same id must not give anything back again (raising KeyError is fine).

Level 3. Many customers. KeyedLimiter(capacity, rate, global_capacity, global_rate) gives each key its own bucket, full on the key's first request, and one global bucket that every key shares. allow(now, key, cost=1) passes only when both the key's bucket and the global bucket hold cost, and takes from both or from neither. keys(now) returns how many key buckets it still stores. Drop a key whose bucket has refilled to capacity, since a full bucket says nothing a new one wouldn't.

Things to get right: refill before you check, stop at capacity, spend nothing on a denied request, and take from every bucket or from none. Follow-ups from the lesson: sharing one limit across many servers, what to do when the shared store is down, Retry-After in whole seconds, and counting output as it's written instead of reserving it.

solution.py
Run the tests to see which pass.