Prefill vs decode, why generation is memory-bound, how big the KV cache really gets, and the batching and quantization tricks that make serving affordable
By The Coding Gopher
Published: 2026-11-26
URL: https://thecodinggopher.substack.com/p/how-llm-inference-works
LLM inference runs in two phases, a parallel prefill over the prompt and a token-by-token decode, and the decode phase is limited by memory bandwidth, not compute. This post explains both phases, the KV cache that makes decoding possible (and eats your GPU memory), and the batching and quantization techniques that serving engines like vLLM and TensorRT-LLM use to make it affordable.
1. Two phases: prefill and decode
A model generates text one token at a time: each new token depends on every token before it. But the prompt is known up front, so the model can process all of it at once.
Prefill processes the prompt in one pass. Decode then produces one token per step, appending to the KV cache.
-
Prefill runs the whole prompt (say 2,000 tokens) through the model in one parallel pass. That's large matrix multiplications, so it's compute-bound, and it determines time to first token (TTFT).
-
Decode generates one token per forward pass. Each step does little math but must read every weight of the model from GPU memory. So it's memory-bandwidth-bound, and it determines time per output token.
The arithmetic makes the point. An 8B-parameter model in FP16 is 16 GB of weights. An H100 reads memory at about 3.35 TB/s. Reading 16 GB takes about 5 ms, so a single request can't decode faster than roughly 200 tokens per second on that GPU, no matter how many FLOPs it has. The compute units are mostly idle, waiting on memory.
Almost every inference optimization is a way to read fewer bytes per token, or to share each read across more tokens.
Learn AI Engineering, Visually (Module 1 Free)
Want to understand systems like this well enough to design them yourself?
My animated course AI Engineering, Visually Explained builds every concept as a clean, step-by-step diagram, from the fundamentals to real interview designs. It grew out of my YouTube explainers, which have millions of views.
Module 1 is free. No card needed.
Want everything? All-Access Lifetime includes all 6 live courses (System Design, AI Engineering, Backend Development, Cloud Development, System Design: Case Studies and Data Structures & Algorithms), the 10 on the way (Distributed Systems, How Computers Really Work, Database Internals and more), the full System Design Visual Guide and every course after that, for one payment.
💡 Many engineers expense this through their team's learning budget.
Building a product for developers? Put it in front of thousands of engineers who read this newsletter and watch The Coding Gopher on YouTube. Newsletter and video sponsorships are open.
2. The KV cache
Attention lets each new token look at all previous tokens, via their key and value vectors at every layer. Recomputing those for the whole sequence at every step would make generation quadratic. Instead, the engine computes them once and keeps them: the KV cache.
It's big. For Llama 3 8B: 2 (K and V) × 32 layers × 8 KV heads × 128 dimensions × 2 bytes = 128 KB per token. An 8K-token conversation holds 1 GB of cache. Llama 3 70B needs about 320 KB per token.