Lesson 3: Prefill and decode · Workbook
Prefill and decode: the workbook
The full working behind lesson 3. The four latency numbers, how prompt length and answer length move them, the exchange rate between a prompt token and an output token, batching, generation stalls, and seven exercises to check yourself.
Ask a chatbot a question and you wait twice. First there is a pause before any text appears. Then the answer streams in, a word or so at a time, until it ends. These two waits feel like one experience, but they come from two different phases of work on the GPU, and each phase is limited by a different resource.
The first wait is prefill: the model reads your whole prompt in one pass. The second is decode: it generates the answer one token at a time. Lesson 1 showed that prefill is compute-bound and single-user decode is memory-bound. This lesson follows that split up to the level a user sees: the latency numbers serving systems report, how prompt length and answer length each move them, and why a long prompt and a long answer cost different things.
The life of one request
A request arrives with a prompt of tokens and will produce output tokens.
- Prefill. All prompt tokens go through the model together. This writes the prompt’s keys and values into the KV cache (the stored per-token state that later tokens attend to; lesson 4, on the KV cache, covers it) and produces the scores for the first output token.
- Decode. Each step feeds the one newest token through the model, appends its keys and values to the cache, and produces the next token. Producing tokens takes decode steps, because prefill already produced the first.
The decode steps cannot run in parallel for one request: step needs the token chosen at step . That sequential chain is why decode processes one token per weight read, which lesson 1 showed is the memory-bound case. Lesson 2 breaks down where each step’s FLOPs and bytes go; the two numbers this lesson needs from it, the matmul weight count and the KV-cache bytes per token, are given below where they are used.
Four latency numbers
Serving papers and benchmarks describe a request’s latency with a small set of numbers. The definitions here follow DistServe (Zhong et al., OSDI 2024).
- Time to first token (TTFT): from the moment the request arrives until the first output token is ready. On an idle GPU this is the prefill time. On a busy one it also includes time spent waiting in a queue.
- Time per output token (TPOT): the average time per output token after the first. It is set by the decode phase.
- Inter-token latency (ITL): each individual gap between two consecutive output tokens. Splitwise and Sarathi-Serve call it time between tokens (TBT). TPOT is the mean of a request’s inter-token latencies, so a request can have a good TPOT and still have one long gap that a user notices as a freeze.
- End-to-end latency: from arrival to the last token. DistServe writes it as TTFT plus TPOT times the number of tokens generated in the decode phase:
Two throughput numbers go with them. Per-user throughput is the output tokens per second one user sees while their answer streams, which is . System throughput is the output tokens per second the GPU produces across all the requests it is serving. Both can be counted while decoding only, or over the whole request including prefill; the table below says which. With one request they are equal. With many, they pull apart, and that gap is the subject of the batching lessons.
A worked request
Take Llama 3.1 8B in BF16 on one H100 SXM, a 2,048-token prompt and a 256-token answer. Every time below is the roofline floor from lesson 1: the larger of compute time and memory time for each group of kernels, summed. Real systems run slower, but the shape holds.
- TTFT: 30.0 ms. Prefill pushes 2,048 tokens through 6.98 billion matrix-multiply weights, plus the 0.53 billion-weight vocabulary projection for the last token only, about 29.7 TFLOP, while reading the 15.0 GB of weights once (15.3 GB including the KV-cache write). It is compute-bound.
- TPOT: 4.57 ms. Each decode step reads those 15.0 GB of weights again, plus the KV cache (128 KB per token, about 268 MB at 2,048 tokens), to do the arithmetic for one token. It is memory-bound. The first gap is 4.56 ms and the last is 4.57 ms: the cache grows by one token each step, so each step reads slightly more.
- End-to-end: 1.19 s, which is 30.0 ms + 255 × 4.566 ms. That is about 219 tokens per second for this user once the answer starts.
Prefill is 2.5% of the request’s time. The other 97.5% is decode, even though the prompt is eight times longer than the answer. Splitwise (Patel et al., ISCA 2024) found the same pattern on production traces: for most requests, the majority of end-to-end time is spent generating tokens.
Prompt length drives time to first token
Hold the answer fixed and change the prompt. TTFT depends on the prompt alone:
| Prompt tokens | TTFT floor | Prefill is | Compute in use |
|---|---|---|---|
| 128 | 4.49 ms | memory-bound | 40.4% |
| 512 | 7.29 ms | compute-bound | 100.0% |
| 2,048 | 30.0 ms | compute-bound | 100.0% |
| 8,192 | 133 ms | compute-bound | 100.0% |
| 32,768 | 747 ms | compute-bound | 100.0% |
Two regimes show up. Below a few hundred tokens, prefill is memory-bound: the time goes to reading the weights once, and a 128-token prompt costs about the same as a single decode step. This is the ridge point from lesson 1 again. On an H100 in BF16 the matmuls need about 295 tokens per weight read before arithmetic becomes the limit.
Above the ridge, prefill is compute-bound and TTFT grows with the prompt. Between 2,048 and 4,096 tokens, each extra prompt token adds about 15.7 µs, which is close to 2 FLOPs per weight divided by peak FLOP/s. Splitwise measured the same thing on real hardware: TTFT grows almost linearly with prompt size, because the prompt phase keeps the GPU’s compute busy.
The growth is not quite linear. Each prompt token attends to every token before it, so attention work grows with the square of the prompt length. Scaling the 2,048-token TTFT by 16 predicts 480 ms for 32,768 tokens; the floor is 747 ms, and the difference is attention. Lesson 9, on FlashAttention, covers how that attention is computed efficiently.
Output length drives total time
Now hold the prompt at 2,048 tokens and change the answer length:
| Output tokens | End-to-end floor | Share spent in decode |
|---|---|---|
| 1 | 30.0 ms | 0.0% |
| 64 | 317 ms | 90.5% |
| 256 | 1.19 s | 97.5% |
| 1,024 | 4.72 s | 99.4% |
| 4,096 | 19.0 s | 99.8% |
TTFT does not move. Each extra output token adds one decode step, about 4.57 ms, so end-to-end time grows in a straight line with the answer. The slope rises slowly: by the 4,096th token the cache holds over 6,000 tokens and a step takes 4.72 ms, against 4.56 ms for the first. At batch 1 the weights still dominate each step’s bytes. At larger batches and longer contexts the KV cache takes over, which is why it gets its own lesson (lesson 4).
A long prompt and a long answer cost different things
Compare two requests with the same total length:
| Long prompt | Long answer | |
|---|---|---|
| Prompt, output tokens | 4,096 + 256 | 256 + 4,096 |
| TTFT | 62.2 ms | 4.50 ms |
| End-to-end | 1.25 s | 18.7 s |
| Arithmetic | 66.0 TFLOP | 70.0 TFLOP |
| Bytes read and written | 3.98 TB | 62.7 TB |
The two do nearly the same arithmetic, because every token passes through every layer’s weights once either way (only output tokens also pay for the vocabulary projection). But the long answer moves about 16 times as many bytes and takes about 15 times as long. Prompt tokens are processed together, so one weight read serves thousands of them. Output tokens are processed one at a time, so every one of them pays for a full weight read.
There is a simple exchange rate. A decode step costs about the time to read the weights, , for weights of bytes each at bandwidth BW. A compute-bound prompt token costs about . Their ratio is
which in BF16 () is exactly the ridge point, 295 on an H100. The floor model, which also counts attention and the KV cache, gives 290: one output token costs about as much GPU time as 290 prompt tokens. Splitwise measured an exchange rate of the same order on real hardware: for BLOOM-176B, a 1,500-token prompt phase took as long as 6 output tokens.
This is why the two phases need different fixes. Decode wastes the tensor cores, so serving many users in the same step (batching) makes it far cheaper per token. Prefill already uses the tensor cores fully, so batching prompts together gains little; Splitwise recommends limiting the prompt-phase batch size for that reason. Speeding up prefill takes fewer FLOPs or faster FLOPs, such as FP8 math. Speeding up decode takes fewer bytes per token, such as smaller weights or a smaller KV cache.
Watch a request on the timeline
The figure draws a request as a Gantt chart. Time runs left to right. The solid block is prefill and the band after it is decode, with a tick each time a token is ready. Amber marks a compute-bound step and teal a memory-bound one. The bottom row shows how much of the GPU’s peak compute each step uses: achieved FLOP/s divided by peak, if the step runs at its floor.
Press Play to watch it at real speed, or Step to move one GPU step at a time. Then drag the sliders. The time axis rescales to fit the run, so compare the readout table instead: the prompt slider moves Time to first token and leaves Time per output token almost unchanged, and the output slider does the reverse.
The bottom row is the clearest picture of the split. During prefill, compute use sits at the roof: 100.0% in this floor model. Real prefill kernels reach less than that, but they get close; DistServe reports that prefilling a single 512-token sequence on a 13B model can fully engage an A100. During decode the row drops to 0.4%, a sliver you can barely see. Averaged over the whole request, the GPU uses 2.9% of its compute.
Per-user and system throughput
That idle compute is what batching recovers. Serve identical requests in lockstep: their prompts are prefilled together, and every decode step produces one token for each of them.
| Batch | TTFT | TPOT | Decode tokens/s, each user | Decode tokens/s, all users | Whole-request tokens/s, all users |
|---|---|---|---|---|---|
| 1 | 30.0 ms | 4.57 ms | 219 | 219 | 214 |
| 8 | 240 ms | 5.16 ms | 194 | 1,550 | 1,316 |
| 64 | 1.92 s | 9.93 ms | 101 | 6,444 | 3,679 |
The decode columns divide by TPOT, so they count only the decode phase; the last column divides all output tokens by the end-to-end time, prefill included.
The two phases respond in opposite ways. TPOT grows far more slowly than the batch: 64 times the requests multiply it by only 2.2, because the weights are still read once per step. What grows is the KV cache, since each of the 64 requests reads its own: about 18.3 GB per step at this length, more than the weights. Meanwhile system throughput while decoding rises about 29 times, or about 17 times counted over the whole request, since the batched prefill takes longer. Splitwise reports a similar order on real hardware, about 2× time between tokens at batch 64 for its models; the exact ratio depends on context length, because that sets how big the KV caches are. TTFT, on the other hand, grows in proportion to the batch, because prefill was already compute-bound and 64 prompts are 64 times the work.
So throughput and latency are not one dial. Batching decode buys system throughput for a small cost in TPOT. Batching prefill buys almost nothing and costs TTFT directly. Continuous batching (lesson 6) is the technique that captures the first without paying the second.
When requests share a GPU: inter-token latency
Real servers do not get identical requests that arrive together. Turn on the second request in the figure. Request B, with an 8,192-token prompt, arrives 299 ms after request A, while A is decoding. With the simple scheduler in the figure, B’s prefill runs as soon as it arrives, and A’s decoding stops until it finishes.
A’s longest gap between tokens becomes 138 ms (B’s 133 ms prefill plus one decode step), about 30 times its normal step. Yet A’s TPOT only rises from 4.57 ms to 5.34 ms, because one long gap averaged over 255 is small. Part of that rise is not the stall itself: once B is decoding, every step A shares with it also reads B’s 8,192-token KV cache, so A’s later steps take 4.88 ms instead of about 4.57 ms. The user sees the answer freeze for a moment; the average hides it. That is why serving systems track inter-token latency, and its tail, separately from TPOT.
Sarathi-Serve (Agrawal et al., OSDI 2024) calls this a generation stall. It describes the scheduler of vLLM, a widely used open-source serving engine, as running prefill-only and decode-only batches and admitting new prefills first, which produces stalls like this one; with long prompts they last seconds. Their fix, splitting a long prefill into chunks that ride along with decode steps, is the subject of lesson 12, which also covers running the two phases on separate GPUs, the approach of DistServe and Splitwise.
What the floor leaves out
The times in this lesson are lower bounds for one GPU doing only this work. A measured TTFT also includes tokenizing the prompt, waiting in the server’s queue, and the network. Kernels reach less than peak, as lesson 1’s measurement showed. Small per-step costs such as sampling and kernel launches add a roughly fixed amount to every decode step, so they show up in TPOT more than in TTFT. Lesson 14 covers how to measure all of these honestly.
Check yourself
Every answer uses the same functions as the figures and the tables: Llama 3.1 8B in BF16 on an H100 SXM unless stated. Exercise 5 uses the timeline figure above. The last two revisit earlier lessons.
Exercise 1. A 2,048-token prompt has a TTFT floor of 30.0 ms. Predict the TTFT for an 8,192-token prompt. Is it exactly four times as long? Why or why not?
Show the worked answer
The floor is 133 ms, 4.44 times the 2,048-token value of 30.0 ms. The matmul part grows linearly with the prompt, but attention work grows with the square of its length, so quadrupling the prompt slightly more than quadruples TTFT.
Exercise 2. A request has a 1,000-token prompt and a 512-token answer. What are the TTFT, TPOT and end-to-end floors? Enter the end-to-end floor.
Show the worked answer
TTFT is 14.4 ms, TPOT is 4.53 ms, and end-to-end is 14.4 ms + 511 × 4.53 ms = 2.33 s. Decode is 99.4% of it.
Exercise 3. Starting from a 2,048-token prompt and a 256-token answer, which adds more time: 1,000 more prompt tokens, or 10 more output tokens?
Show the answer
The 10 output tokens: they add 45.7 ms, against 15.5 ms for the 1,000 prompt tokens. One output token costs about as much as 290 prompt tokens, so 10 output tokens are worth a few thousand prompt tokens.
Exercise 4. Run the 2,048 + 256 request on an A100 SXM 80GB (312 TFLOP/s BF16, 2.04 TB/s) instead. By what factor does each of TTFT and TPOT change?
Show the answer
TTFT goes from 30.0 ms to 95.2 ms, 3.17× slower: prefill is compute-bound, so it scales with the peak FLOP/s ratio. TPOT goes from 4.57 ms to 7.50 ms, 1.64× slower: decode is memory-bound, so it scales with the bandwidth ratio. The same GPU upgrade helps the two phases by different amounts.
Exercise 5. Request A (2,048-token prompt, 256-token answer) is decoding when a request with a 32,768-token prompt arrives a quarter of the way through A's end-to-end time. With the prefill-first scheduler from the timeline figure above, what is A's longest gap between tokens, and how much does A's TPOT change?
Show the answer
A’s longest gap is 753 ms: the 747 ms prefill of the new prompt, plus the decode step that then produces A’s next token. A’s TPOT rises from 4.57 ms to 8.48 ms. The stall alone would raise it only to about 7.49 ms; the rest is that every later step A shares with the new request also reads its 32,768-token KV cache (about 4.29 GB), so those steps take 5.85 ms instead of about 4.57 ms. The average rises 1.9×, but the user sees a freeze of 0.75 s in the middle of the answer, which only inter-token latency reveals.
Exercise 6. From lesson 1: an H100 SXM does 989 TFLOP/s in BF16 and moves 3.35 TB/s. How many FLOPs must a kernel do per byte it moves before arithmetic, not memory, sets its speed? This is the ridge point.
Show the worked answer
About 295 FLOPs per byte: peak FLOP/s divided by bandwidth. A decode step for one user does about 1.05 FLOPs per byte, far below the ridge, so memory sets its pace. A 2,048-token prefill does about 1,943, far above it, so arithmetic does. The two waits in this lesson sit on opposite sides of this one number.
Exercise 7. From lesson 2: Llama 3.1 8B does about 2 FLOPs per weight per token in its matrix multiplies. How many GFLOP do those matrix multiplies cost for one token at short context, the vocabulary projection included?
Show the worked answer
About 15.0 GFLOP: 2 × 7.50 billion matrix-multiply weights. Prefill pays roughly this for every prompt token (the vocabulary projection only for the last one), which is why time to first token grows with the prompt once prefill is compute-bound. Decode pays it once per output token, but it is the weight read, not these FLOPs, that sets the step’s time.
Sources
- Zhong, Liu, Chen, Hu, Zhu, Liu, Jin, Zhang (2024). DistServe: Disaggregating Prefill and Decoding for Goodput-optimized Large Language Model Serving. OSDI 2024. Definitions of TTFT and TPOT, and request latency = TTFT + TPOT × tokens generated in the decoding phase (Section 1)
- Patel, Choukse, Zhang, Shah, Goiri, Maleki, Bianchini (2024). Splitwise: Efficient Generative LLM Inference Using Phase Splitting. ISCA 2024. Characterization of the prompt and token phases (Section III): TTFT grows almost linearly with prompt size; batching 64 decodes only about doubles time between tokens; a 1,500-token prompt phase of BLOOM-176B takes as long as 6 output tokens
- Agrawal, Kedia, Panwar, Mohan, Kwatra, Gulavani, Tumanov, Ramjee (2024). Taming Throughput-Latency Tradeoff in LLM Inference with Sarathi-Serve. OSDI 2024. Prefill is compute-bound, decode memory-bound; prefill-first scheduling causes generation stalls. Also arXiv 2403.02310
- NVIDIA H100 product page, specifications table. 989 TFLOP/s BF16 dense, 3.35 TB/s
- NVIDIA A100 product page, specifications table. A100 SXM 80GB: 312 TFLOP/s BF16 dense, 2,039 GB/s. Used in exercise 4
- Llama 3.1 8B model card (Meta). config.json is under Files after you accept Meta's license