Case study · AI · complexity 4/5
How an LLM API turns a prompt into a stream of tokens — from one GPU answering one request at a time to a fleet with continuous batching, a paged KV cache, token-based rate limits and prefix caching
This is an educational reference architecture for an LLM inference service, not the design of any AI company. Where we cite papers, open-source projects or public API documentation, it is marked as documented. We do not describe any provider's internal systems.
Ana's question reaches a server with one GPU. The GPU writes her answer token by token, about 50 tokens a second (illustrative). Ben and Chen wait in line until it is finished.
Architecture evolution
An HTTP server loads the model's weights into one GPU and runs each request from start to finish, a prefill pass over the prompt and then one decode step per output token. Requests that arrive in the meantime wait in a queue.
Components
What breaks next
Every decode step reads all 26 GB of weights to make one token, while the GPU's arithmetic units mostly idle. One request gets about 50 tokens a second, a 2,000-token answer holds the GPU for about 40 seconds, and everyone else waits (illustrative).
Served
0/s
Errors
0%
p50 latency
0 ms
p95 latency
0 ms
Avg busy
0%
Cost / hour
$0.00
illustrative
Serve 50,000 requests per second
Keep errors under 1% and p95 latency under 150 ms for 20 seconds at 50,000 req/s or more.
One server can finish 10,000 requests per second. 50,000 are arriving. Watch the queue, the latency and the failures — then fix it.
Illustrative model: rates, queues and latency follow simple queueing rules, not a production benchmark.
A large language model writes its answer one token at a time, and serving it cheaply has become a research field of its own. The ideas in this case study come from published papers, open-source inference servers and the public documentation of LLM APIs, not from inside any AI company.
Where the documented ideas come from
A model cannot write a 2,000-token answer in one go. It produces one token, appends it, and runs again, and every run reads all of the model's weights from GPU memory, so one GPU serving one request makes tens of tokens a second and needs tens of seconds for a long answer.
The two phases of one request
The simplest LLM service is one HTTP server in front of one GPU that holds the model's weights and runs each request from prompt to last token before starting the next. This is our educational design, the kind of thing a team builds for a demo.
Stage 1: one GPU, one request at a time
The bottleneck moves from waiting in line for one GPU, to GPU memory filling up with KV caches, to a fleet where cost, traffic spikes and heavy customers dominate. The throughput figures are illustrative.
The bottleneck moves at each stage
Batching lets one read of the weights serve many requests at once, which multiplies tokens per second per GPU. Doing it per decode step instead of per request, as the Orca paper proposed in 2022, stops short answers from waiting for long ones.
Static batches waste slots; continuous batches refill them
Every running request keeps a KV cache, the attention keys and values of all its earlier tokens, in GPU memory, and it grows with every token. How that memory is managed decides how many requests fit in a batch, and the vLLM paper showed that earlier servers wasted most of it.
vLLM paper: a 13B model on a 40 GB A100
PagedAttention maps requests to scattered blocks
An answer that takes 40 seconds to finish can start showing within a fraction of a second if the server sends each token as soon as it exists. Open-source servers and public LLM APIs do this with server-sent events, and users judge the service on two numbers: time to first token and tokens per second.
Same answer, two experiences
Once one GPU is not enough, the service runs many model replicas behind a router. A good router balances by real load, such as tokens in flight and free KV memory, and prefers a replica that already holds the prompt's opening tokens in its cache.
Stage 3: route by load and by prefix
A shared LLM service protects itself at three layers: per-customer rate limits counted in tokens at the gateway, admission control at each replica that admits only what its KV memory can hold, and an autoscaler that adds GPUs. The autoscaler is the slowest of the three, so the other two must hold the line while it works.
Three layers of protection, fastest first
An LLM service buys GPU-hours and sells tokens, so its unit cost is the GPU's price per hour divided by the tokens it makes in that hour. Batching, quantization, speculative decoding and prefix caching all raise tokens per GPU-hour, and public price lists show the result per million tokens.
Four levers on cost per million tokens
The techniques in this case study were published within a few years of each other: the Transformer in 2017, FlashAttention, Orca and speculative decoding in 2022, and PagedAttention in 2023. Open-source servers and public APIs then made batching, streaming and token-based limits the expected default.
From the Transformer to paged KV caches
At fleet scale the request path is gateway, router, admission queue and a GPU replica that batches every decode step over a paged KV cache, with tokens streaming back the same way. This is a typical educational design, not a diagram of any company's system.
An LLM inference service, end to end
Every knob in LLM serving trades one user-visible number against another: batch size against each user's speed, prefill budget against the time to the first token, quantization against answer quality, cache affinity against even load, and spare GPUs against cost.
Five dials for an LLM service
On a bad day, an LLM service typically sees one of six things: KV memory runs out and requests get preempted, one customer floods it with huge prompts, a GPU fails mid-stream, a spike outruns the autoscaler, one prefix gets too hot, or clients disconnect while their answers are still being written.
A bad day for an LLM service, six ways
There is no GPU simulation yet, so three general simulations stand in for the three protections: per-customer rate limits, a bounded queue in front of limited workers, and autoscaling that is slower than a spike. Their numbers are illustrative models of web servers, not of GPUs.
What each simulation stands for
Errors — normal users
0%
Errors — buggy client
0%
429s/s
0/s
p95 — normal users
0 ms
Served
0/s
Servers busy
0%
Protect the normal users
With the buggy client flooding the API, keep normal users' errors under 0.5% and their p95 under 100 ms for 20 seconds.
200 normal users each send about 5 requests per second. Five seconds in, one client with a bug in its retry loop starts sending about 10,000 requests per second — ten times all the normal traffic together. Three servers can handle 4,500. All numbers are an illustrative model.
Illustrative model: rates, queues and latency follow simple queueing rules, not a production benchmark.
Queue depth
0
Oldest message age
0 ms
Rejected/s
0/s
Processed/s
0/s
Publish p95
0 ms
Producer wait
0 ms
Bounded and lossless
At 1,500 jobs/s or more, keep the oldest job under 5 s and reject nothing for 20 seconds.
An order service publishes 1,500 invoice jobs per second, and 4 workers can finish about 1,000 per second. The queue has no limit. Watch what grows. All numbers are an illustrative model.
Illustrative model: rates, queues and latency follow simple queueing rules, not a production benchmark.
Served
0/s
Errors
0%
p50 latency
0 ms
p95 latency
0 ms
Avg busy
0%
Cost / hour
$0.00
illustrative
Three servers (10,000 req/s each) carry 12,000 req/s, with auto-scaling on. At 0:20 a match starts and traffic triples in 15 seconds. New servers take 60 seconds to boot. Numbers are illustrative.
Illustrative model: rates, queues and latency follow simple queueing rules, not a production benchmark.
The serving techniques, the KV-cache figures, the scheduling behavior of vLLM and TGI, and what public APIs expose about streaming, rate limits, caching and prices are documented in the sources. The three stages, the throughput and cost numbers, the router and admission design, and anything about how a provider runs its fleet are our educational assumptions.
Where the evidence stops