The memory trick that makes language models fast
Watch a language model stream a 300-word reply. It looks effortless. Yet without one quiet piece of GPU memory called the KV cache, every single new word would force the model to re-read its entire past from scratch.
Why generating one word at a time gets painfully slow
Imagine writing an essay where, before typing word #50, you erase your memory and re-read words 1 through 49. Before word #51, you re-read words 1 through 50. By paragraph three, you are spending almost all your energy re-reading instead of writing.
That is exactly how a naive Transformer behaves during autoregressive decoding. Step through the race below to watch how quickly redundant work explodes.
Across just 8 tokens, naive decoding runs token projections () instead of (). Stretch that to a 4,000-token coding reply and the gap becomes 8 million projections versus 4,000.
Why Keys and Values stay, while Queries vanish
Inside every attention layer, each incoming token gets multiplied into three small vectors: a Query (), a Key (), and a Value (). Think of a quiet research library.
Query (Your question)
What the newest token is searching for right now. Once it gathers its answer, nobody will ever ask this exact question again.
Key (Book spine label)
Advertises what an older token is about. Every future token needs to compare its Query against this label.
Value (Pages inside)
The actual meaning pulled out when a Query matches a Key. Future tokens keep reading it, so it stays on the shelf.
Queries are disposable. Keys and Values are permanent.
Notice the elegance: the model never modifies an older token’s Key or Value once computed. It only reads them and appends one fresh pair to the end of the shelf.
Why changing one early word ruins the whole cache
In our Prompt Caching guide , we saw a strict rule: edit a single word near the top of your prompt, and the API charges you fresh price for everything after it. Now you can see why.
Because language models read left to right, each token ‘s Key () and Value () depend only on positions , and on nothing after it. That creates a lower-triangular dependency grid called the causal mask.
Click any row token on the left to edit it
Watch how an edit leaves every token above it untouched in VRAM, while forcing every token at and below it to recompute its and tensors.
Practical rule of thumb: Put stable instructions and documents at the top of your prompt, and put timestamps, user queries, or live tool outputs at the bottom. That keeps the blue valid region as large as possible.
When the KV cache outgrows the model itself
KV caching turns compute from to , but it trades compute for GPU memory. Every layer of the network needs its own and shelf, for every token, for every concurrent user.
Drag the context length and concurrent users below to see how quickly a single 80 GB NVIDIA H100 fills up.
This memory pressure is why modern inference engines rely on three inventions together: Grouped-Query Attention (GQA) to shrink each token’s footprint, FP8 KV quantization to halve precision bytes, and PagedAttention (pioneered by vLLM) to pack KV blocks into non-contiguous memory pages like an operating system, cutting memory waste from over 60% to under 4%.
From single-request memory to cross-turn savings
Everything above happens inside a single response. When an API provider saves that exact blue KV cache shelf between requests so your next turn skips the prefill step altogether, you get Prompt Caching.
Prompts & Prompt Caching
See how keeping your prefix intact turns KV cache reuse into up to 95% lower API bills across 40-turn agent sessions.