How LLM inference works
A model writes an answer in two phases. Prefill reads the whole prompt at once and is limited by math. Decode writes one token per step and is limited by reading memory. The KV cache keeps the work of earlier tokens, and its size often decides how many users fit on a GPU. Measure speed as users feel it, with time to first token and time per token, at p95.
Author: bezzad
Contents
The problem: fast in the demo, slow at launch
Our shop runs its own support assistant on an open-weight 8B model, on one GPU with 80 GB of memory. In the demo, one person asked questions, and the answers came fast.
On launch day:
- The first words came late. Some people waited several seconds before anything appeared.
- The answers were slower. Words came at maybe half the speed of the demo.
- Some people waited a long time in a queue, while the GPU showed free memory a few minutes earlier.
The model did not change. The load changed. To understand why, we need to know what the GPU does when a model writes an answer.
Tokens: the unit of everything
A model does not read words. It reads tokens, small pieces of text. A common English word is often one token; long or rare words, numbers and many non-English words take more tokens.
Everything in serving is measured in tokens:
- Cost. Prices and GPU work are counted per token.
- Speed. Answers come one token at a time.
- Memory. Each token in a running chat uses GPU memory, as we will see.
Two phases: prefill and decode
When a request arrives, the model works in two phases.
- Prefill. The model reads all prompt tokens at once: the shop rules, the chat history and the new question. The GPU does a lot of math in parallel here. A longer prompt means a longer prefill.
- Decode. The model writes the answer one token per step. Each new token depends on all tokens before it, so the steps cannot run in parallel for one request.
- The answer ends when the model writes a special stop token, or when it reaches the output limit.
These two phases behave very differently, and almost every serving trick targets one of them.
Why decode is slow: reading memory
In each decode step, the GPU must read all the weights of the model from its memory, to produce one token for each running request.
- The weights are big. 8 billion parameters × 2 bytes = about 16 GB.
- Memory speed has a limit. A data center GPU can read roughly 2,000 to 8,000 GB per second, depending on the model of GPU.
- So there is a speed limit for one user. With a GPU that reads about 3,000 GB per second, reading 16 GB takes about 5 ms. That means at most about 190 tokens per second for a single request, and in practice less.
- The math is small. For one request, the GPU spends most of the step waiting for memory, not computing. We say decode is memory-bound.
This is the key idea of serving: reading the weights once can serve many requests. If 32 requests are in the same step, the GPU reads the 16 GB once and writes 32 tokens. That is called batching, and it has its own lesson.
Prefill is different. It processes thousands of tokens with the same weights, so the GPU is busy with math. We say prefill is compute-bound.
The KV cache: the memory of a chat
In attention, each token looks at all earlier tokens through their keys and values. Without a cache, the model would recompute keys and values for the whole chat at every step. So engines keep them in GPU memory: the KV cache.
The cache grows with every token, and it is kept for every running request. Its size per token comes from the model’s config:
def kv_cache_bytes(tokens, layers, kv_heads, head_dim, bytes_per_num=2):
# 2 = one key and one value, for each layer and KV head
return tokens * 2 * layers * kv_heads * head_dim * bytes_per_num
per_token = kv_cache_bytes(1, layers=32, kv_heads=8, head_dim=128)
print(per_token) # 131072 bytes, about 128 KB
print(per_token * 32_768 / 1e9) # about 4.3 GB for one full 32k chat
What this means for our GPU:
- Weights: about 16 GB.
- Runtime and temporary work: a few GB.
- The rest is for the KV cache: about 56 GB, or about 400,000 tokens for all chats together.
- Long chats eat it fast. Only about 13 chats of 32,000 tokens fit. Chats of 2,000 tokens: about 200.
- When it is full, new requests wait, or the engine pauses some running ones and redoes their work later. Users see waiting, not an error.
Model design changes this a lot. Our model uses grouped-query attention: 8 KV heads shared by more query heads. With one KV head per query head, the cache could be several times bigger.
The numbers users feel
A single “latency” number is not enough. Use these:
| Number | What it means | Users feel it as |
|---|---|---|
| Time to first token (TTFT) | From sending the request to the first token. Mostly queue time plus prefill. | “Is it working?” |
| Time per output token (TPOT), also inter-token latency | The time between two tokens while streaming. One decode step. | Reading speed of the answer. |
| End-to-end time | TTFT plus all tokens. | Total wait for a full answer. |
| Throughput | Tokens per second for the whole GPU. | Nothing directly. It is a cost number. |
| Goodput | Throughput that counts only requests that met the targets. | Cost of good service. |
Always look at p95 and p99, not only the average. The slowest 5 percent of users are the ones who complain.
A good first target for chat: TTFT under 1 second and about 20 tokens per second or more (TPOT under 50 ms), which is faster than most people read. Your product may need different numbers.
What makes each number worse
- Long prompts make prefill longer, so TTFT goes up.
- A busy queue makes TTFT go up, even if prefill is fast.
- Bigger batches make each decode step a bit slower, so TPOT goes up, while throughput goes up.
- Long answers make end-to-end time longer. Set an output limit.
- A full KV cache makes requests wait or restart.
Back to our launch day: many users at once meant a queue (late first words), bigger batches (slower words), and a full cache (long waits). Each of the next lessons fixes one part.
Common mistakes
| Mistake | Result | Right way |
|---|---|---|
| Counting only the weights | Plans that fit 100 chats on paper and 13 in reality. | Count weights + KV cache for real chat lengths + runtime. |
| One latency number | Slow first words or slow streaming go unseen. | TTFT and TPOT, at p95 and p99. |
| Testing with one user | Demo speed, not launch speed. | Load test with many parallel users. |
| Counting words, not tokens | Wrong cost and memory estimates, worst for non-English text. | Count with the model’s tokenizer. |
| Unlimited output | Long, slow and costly answers. | An output token limit on every request. |
Summary in six lines
- Everything in serving is counted in tokens.
- Prefill reads the whole prompt at once and is limited by math.
- Decode writes one token per step and is limited by reading the weights from memory.
- The KV cache keeps each chat’s work in GPU memory and grows with every token.
- The KV cache, not the weights, often decides how many users fit on a GPU.
- Measure TTFT and TPOT at p95, not only throughput or averages.