1/17
Learn

A rate limiter for an LLM API, the way the design round asks

OpenAI candidates report rate limiting as a system design question (1 report), a guide lists a distributed rate limiter for the design round and an implementation for coding (2 sources), and an ML engineer candidate had to find the bug in a Django app's rate limiter (1 report). This lesson's practice problem builds the limiter, in three levels:

  1. TokenBucket(capacity, rate), with allow(now, cost) and retry_after(now, cost): a burst, then a steady rate, and when to come back.
  2. LLMLimiter(rpm, itpm, otpm), with start(...) and finish(...): requests, input tokens and output tokens, all or nothing, with the answer's tokens reserved and the unused ones given back.
  3. KeyedLimiter(capacity, rate, global_capacity, global_rate): a bucket for each customer and one for everyone, with no memory kept for customers whose buckets are full.
CallReturns
8 questions in a second, against a burst of 5True five times, then False
retry_after at 10.5 s, for that customer2.5 s, so the header says 3
start reserving max_tokens of 4,096, under 400,000 a minuteroom for 97 answers a minute
keys(now) an hour after the last question0 buckets kept

The lesson follows the outdoor shop's assistant at 120,000 questions a day: the provider's limits, the token bucket, limits on tokens, one limit across many servers, and how many requests run at once. Seven steps end with a small task in the editor.