Before you start: words this module uses#
Module 10 taught what a transformer computes. This module is about running one to generate text. These terms appear in every lesson; each is explained properly later, but here is what they mean:
| Term | Plain meaning | Taught in |
|---|---|---|
| Token | A piece of text (about ¾ of an English word) represented by an integer ID | 12.b |
| Prompt | The tokens the user sends in (system prompt, conversation, documents, tool results) | 12.b |
| Prefill | Processing all prompt tokens in one go, before the first output token | 12.c |
| Decode | Producing output tokens one at a time, each pass waiting for the previous one | 12.c, 12.d |
| KV cache | Saved intermediate results (keys and values) for every token seen so far, so they are never recomputed | 12.e |
| Batch | A group of requests the GPU processes together in one pass | 2.d, 12.i |
| Context length | How many tokens (prompt + output) one request holds at once | 12.h |
| Scheduler | The part of an engine that decides which requests run in each step | 12.j |
1. What is it?#
This lesson is the map of Module 12. It shows the life of one request inside an inference engine, the five facts that explain almost everything about serving language models, and which lesson attacks which fact. Read it now, then read it again after 12.q — it will mean more the second time.
2. Why does it exist?#
LLM serving has about a dozen interlocking ideas. Learned one by one, they become a list of words. The value is in the connections: why the KV cache forces paged memory (12.l), why paged memory makes prefix sharing cheap (12.m), why prefix sharing changes how requests should be routed (21.h). This lesson gives you the skeleton those connections hang on.
3. Simple analogy#
A restaurant kitchen serving a tasting menu. Prefill is reading the whole order and doing all the prep at once. Decode is sending out one course at a time; each course can only start when the previous one has left. The KV cache is the prep station: everything already chopped, so nothing is chopped twice. The scheduler is the head chef deciding which tables’ courses go on the stove in each round so the stove is never idle.
4. Tiny example: one request, end to end#
A user sends “Explain gravity in one sentence.” and gets back 20 tokens.
A REQUEST ARRIVES
│
▼
┌───────────────────────┐
│ TOKENIZE (12.b) │ text → integers: ~8 tokens
└───────────┬───────────┘
▼
┌───────────────────────┐
│ SCHEDULER (12.i–12.k)│ is there KV memory? who else runs?
└───────────┬───────────┘
▼
┌───────────────────────┐
│ PREFIX CACHE (12.m) │ seen this system prompt before? reuse it
└───────────┬───────────┘
▼
┌───────────────────────┐
│ PREFILL (12.c) │ all ~8 prompt tokens in one pass
│ compute-bound │ → first output token + KV cache filled
└───────────┬───────────┘
▼
┌───────────────────────────────┐
│ DECODE LOOP (12.c, 12.d) │ ← 19 more times
│ read all weights │ memory-bound
│ read the KV cache (12.e) │ grows by one token per step
│ compute one token │
│ append its K,V (12.l) │ into fixed-size memory blocks
│ pick the token (12.o) │
│ stream it out (12.p) │
└───────────────┬───────────────┘
▼
┌───────────────────────┐
│ FREE THE KV MEMORY │
└───────────────────────┘On an 8B model on one modern GPU, prefill of 8 tokens takes a few milliseconds and each decode step about 10 ms, so the user sees the first word almost at once and the rest within a quarter of a second. Change the prompt to 100,000 tokens (a long document, or an agent’s history) and prefill takes seconds while the KV cache grows to gigabytes. Change the output to a 10,000-token reasoning trace and decode dominates everything. Those three cases are what this module teaches you to reason about.
5. The five facts that explain everything#
Fact 1 — Prefill and decode are different workloads. Prefill processes many tokens per request in parallel, so the GPU’s arithmetic units are busy: it is compute-bound. Decode processes one token per request per step, so the GPU spends most of its time reading memory: it is memory-bound (2.e). Every scheduling decision in an engine is about managing this asymmetry (12.c).
Fact 2 — Decode reads all the active weights for every step.
70B dense model at 16 bits → 140 GB read per decode step, whatever the batch size
H100 memory bandwidth 3.35 TB/s → at least 42 ms per stepThat 42 ms is paid once per step, and one step can serve a whole batch. This is why batching (share each weight read across many requests) and quantization (fewer bytes per weight) are the two biggest levers. Mixture-of-experts models read only their active experts per token, but a large batch touches most experts, so at high load they too read most of their weights each step (14.g).
Fact 3 — The KV cache makes decode affordable, then becomes the limit. Without it, step N would recompute attention inputs for all N−1 earlier tokens. With it, those are stored and only read. But storage grows with every token of every active request, and it is usually what caps how many users fit on a GPU (12.e, 12.f).
Fact 4 — Requests differ wildly in cost and their length is unknown. One request produces 5 tokens; a reasoning request produces 20,000. You cannot know in advance. This breaks fixed-size batches and simple load balancing, and forces scheduling at every step (12.j).
Fact 5 — Capacity is a memory budget, and you can compute it.
KV budget = GPU memory − weights − runtime overhead
concurrency = KV budget ÷ (KV bytes per token × tokens per request)Every capacity question reduces to this. 12.q works through many examples.
6. How the techniques map to the facts#
| Technique | Attacks | Lesson |
|---|---|---|
| Batching | Fact 2 (share weight reads) | 2.d, 12.i |
| Continuous batching | Fact 4 (unknown lengths) | 12.j |
| Chunked prefill | Fact 1 (long prefills stall decode) | 12.k |
| PagedAttention | Fact 3 (wasted KV memory) | 12.l |
| Prefix caching | Fact 1 (skip repeated prefill) | 12.m |
| Speculative decoding | Fact 2 (several tokens per weight read) | 12.n, 13.n |
| GQA, MLA, sparse and hybrid attention | Fact 3 (smaller KV per token) | 12.g |
| Weight and KV quantization (FP8, FP4) | Facts 2 and 3 (fewer bytes) | 13.b–13.h |
| Tensor and expert parallelism | Fact 2 (more total bandwidth) | 14.c, 14.e |
| Prefill/decode disaggregation | Fact 1 (separate the workloads entirely) | 14.j |
| KV offloading to CPU, SSD, remote storage | Fact 3 (more KV than HBM holds) | 14.k |
Every technique in this material attacks one of these five facts. When you meet a new one, ask which fact it attacks. If you cannot answer, you have not understood it yet.
7. The formulas you should know from memory#
Given a model’s config file and a GPU spec sheet:
Weights (GB) = parameters × bytes per parameter ÷ 1e9
FLOPs per token ≈ 2 × active parameters (+ attention: ~4 × layers × context × hidden size)
KV bytes per token = 2 × layers × KV heads × head dim × bytes (standard attention; 12.f, 12.g
give the MLA and hybrid forms)
Decode step floor = bytes read per step ÷ memory bandwidth
Max concurrency = (GPU memory − weights − overhead) ÷ (KV bytes per token × context)Five formulas. With them you can answer most capacity questions before renting a single GPU.
8. Production implications#
- Measure prefill and decode separately. Time to first token is prefill (plus queueing); inter-token latency is decode. A slow service is almost always slow in one of them.
- Know your token mix. In 2026 traffic is dominated by long inputs (agents re-sending history, retrieved documents) and long outputs (reasoning). The ratio of prompt tokens to output tokens decides which facts bite hardest.
- Every default in a modern engine exists because of one of the five facts. Continuous batching, paged KV, prefix caching and chunked prefill are on by default in vLLM and SGLang; Module 15 compares the engines.
9. Common mistakes#
- Counting requests instead of tokens. Capacity, cost and latency all scale with tokens.
- Treating prompt and output tokens as equal. A prompt token is processed in parallel and is cheap; an output token needs its own pass and is expensive (12.c).
- Forgetting reasoning tokens. A reasoning model may generate 10–50x more hidden tokens than it shows. They cost decode time and KV memory like any other output token.
10. Hands-on exercise#
A. Draw it. Reproduce the diagram in section 4 from memory, then compare. What did you miss?
B. Predict, then check. Write down your guesses now and check them at the end of the module:
- For an 8B model at a 128k-token context, is the KV cache of one request bigger or smaller than the weights?
- What fraction of GPU slots does static batching waste with realistic output lengths?
- How much does continuous batching improve throughput?
C. Set up a lab. Serve a small model locally with vLLM or SGLang; you will use it throughout this module (16.b covers vLLM in detail).
pip install vllm
vllm serve Qwen/Qwen3-0.6B --max-model-len 8192
# then: curl http://localhost:8000/v1/modelsNo NVIDIA GPU? llama-server from llama.cpp runs the same model on a laptop (15.e).
11. Interview questions#
- Walk through what happens to a request inside an inference engine, naming each phase.
- Why is prefill compute-bound and decode memory-bound?
- Which of the five facts does PagedAttention attack? Speculative decoding? Disaggregation?
- Write the formula for maximum concurrency and explain each term.
12. Further reading#
- [ESTABLISHED] Kwon et al., “Efficient Memory Management for LLM Serving with PagedAttention” (SOSP 2023) — introduction now, the rest after 12.l.
- [ESTABLISHED] Pope et al., “Efficiently Scaling Transformer Inference” (2022).
- [ESTABLISHED] Yu et al., “Orca: A Distributed Serving System for Transformer-Based Generative Models” (OSDI 2022) — the origin of iteration-level scheduling.
- [REFERENCE] vLLM documentation and SGLang documentation.