Preparing your design workspace

Loading the problem and your latest saved attempt…

Design Rate Limits for an LLM Inference API

Practice designing per-organization request and token limits for a model API that runs on thousands of servers in three regions, where a response's token count is not known until the model finishes writing it.

Problem statement

Design the rate limits of an LLM inference API.

For this exercise, assume the API runs on many servers in several regions. Developers call the API with an API key and get a model response, either all at once or streamed piece by piece. Each key belongs to an organization (org). For each model, every org has two limits: requests per minute and tokens per minute.

The limits come from the org's usage tier, operators can override them for one org, and one org's limits are shared by all of its API keys in every region.

A limit of N per minute refills continuously at N/60 every second, and unused allowance builds up to at most N, so an idle org can burst up to one minute's worth at once.

Also assume the tokens limit counts both prompt tokens and output tokens. The API server can count a request's prompt tokens when it arrives, but output tokens are known only as the model writes them. A caller may set a maximum output length; without one, the model may write up to 16,000 tokens.

A request that would go over either limit is rejected with HTTP 429 before it reaches a model server, and the response says which limit was hit and when to try again. Requests are never queued for later.

For this exercise, these are out of scope: billing and monthly spending caps, scheduling work inside the model servers, sign-up and API key management, and limits in consumer chat apps.

What to build

  1. Before a request reaches a model server, check it against the org's requests-per-minute and tokens-per-minute limits for that model, and either let it through or reject it with a 429 that says which limit was hit and when to try again.
  2. Count prompt and output tokens so that, once a response ends, the org's tokens limit reflects the tokens it really used, whether the response finished, was stopped by the caller or failed partway.
  3. Every response, including a 429, tells the caller its two limits, how much of each remains and when each will be full again.
  4. When an org's tier changes or an operator sets or removes an override, the new limits apply on every server in every region within one minute.

At scale

  • For this exercise, assume about 1 million orgs, about 3,000 API servers spread over 3 regions, and peaks of 200,000 requests a second in total. Most orgs have small limits, such as 500 requests and 200,000 tokens a minute. The largest org can send 10,000 requests a second, from all 3 regions at once, against limits of 600,000 requests and 1.5 billion tokens a minute.
  • For this exercise, assume an average request has about 2,000 prompt tokens and 500 output tokens. An output can reach 16,000 tokens, and a streamed response can last up to 10 minutes.
  • For this exercise, assume the limit check may add at most 5 ms to a request at the 99th percentile, while a round trip between two regions takes 50 to 150 ms.
  • For this exercise, assume that, measured over any 5 minutes, an org may get at most 10% more than its limits allow, and an org using less than 90% of its limits should not get a 429. If the limit-counting service fails, inference must keep running with looser limits for a few minutes rather than stop.