Lesson 4 of 14
The KV cache
A model writing a reply keeps notes on every word it has already processed, so it never has to process them again. The notes save almost all the arithmetic, but they pile up with every word and every user until they fill the GPU's memory.
From one card to a full GPU
The 3D view could not load. The three stages are described below.
01 · Why keep notes
Rereading is the slow way
Writing a sentence, one word at a time
Without notes words run
With notes words run
- Bold, outlined word: run through the model this step
- Bar under a word: its note card, kept in memory
A model writes one word at a time, and to choose each word it looks back at everything before it. Without notes it reruns the whole text at every step: ten words in, ten words of work. That is the top row. The bottom row keeps a note card for each word, so only the new word goes through the model.
By the end of this 16-word sentence the top row has done 8.5 times the work. For a 1,024-token question with a 1,024-token answer it is about 735 times. The saving in seconds is smaller, because with notes most of a step goes into fetching the model and the cards from memory (lesson 1), not arithmetic.
02 · What a note is
One card per word, per layer
Each card has two halves. The label says what the word is about; later words check it to decide how much this word matters to them. The content is what they take from it. A model calls the labels keys and the contents values, and all the cards together are the KV cache. A card never needs rewriting, because it depends only on its own word and the ones before it.
Models work on tokens (a token is a piece of a word, about three-quarters of one on average) and in stages called layers. Llama 3.1 8B has 32 layers and keeps 8 stacks of cards in each, so every token adds 32 × 8 = 256 cards.
Each half of a card is 128 numbers of 2 bytes. So one token costs 2 halves × 32 layers × 8 stacks × 128 numbers × 2 bytes = 131 KB.
03 · What it costs
The cards fill the shelf
That is what filled Parrot’s GPU. Its 80 GB of memory is lesson 1’s archive: a fixed length of shelving. The model’s pages take 16.1 GB of it for good, and the server keeps 10% free as a safety margin. The rest is room for cards, one stack for each reply being written.
One real model on one real GPU
Llama 3.1 8B on one NVIDIA H100. Plays by itself; move a slider to take over.
- The model
- Notes, one slice per person
- Notes that don't fit
- Safety margin
One conversation of 8,192 tokens needs 1.07 GB of cards, so at most 52 fit at once. Past that, new chats wait in the queue: the 3 a.m. alarm. On a busy server, how many people you can serve depends less on how fast the GPU calculates than on how many stacks of cards fit in its memory. Lessons 5, 7, 8 and 10 shrink the cards, pack them tighter and share identical ones.
04 · Playground
Fill a GPU yourself
Pick a model and a number of GPUs, then push the sliders until the bar turns red. Two things to try: store the cards in 8 bits instead of 16, and move the 70B model from two GPUs to four.
GPU memory budget
16-bit weights on NVIDIA H100s (80 GB each)
- Weights
- KV cache
- Held back (10%)
- Free
05 · Glossary
The vocabulary, in one place
- Token
- The unit a model reads and writes: a piece of a word, about three-quarters of one on average.
- Layer
- One stage of the model. Every token passes through all of them in order; Llama 3.1 8B has 32.
- Key
- The label half of a card. Later tokens compare themselves against it to decide how much this token matters to them.
- Value
- The content half of a card: what a later token takes from this one, in proportion to how much it matters.
- KV cache
- All the cards of one conversation, kept in GPU memory so no earlier token is ever run through the model again. 131 KB per token for Llama 3.1 8B.
- KV head
- One of the separate stacks of cards a layer keeps. Llama 3.1 8B has 8 per layer, so 256 cards per token.
- GPU memory
- The chip's own storage (80 GB on an H100). The model and every conversation's cards have to fit in it together.
- Safety margin
- The share of GPU memory a server leaves unused, 10% here, so it does not run out mid-reply.
You can now
- Explain why a model keeps a KV cache, what it saves in arithmetic, and why the time saved is smaller than that.
- Compute the KV cache size per token, per conversation and per GPU for any model config (layers x KV heads x head dim x 2 x bytes).
- Estimate how many in-flight replies fit on a GPU after the weights, and explain why the cache, not the weights or the FLOPs, sets that limit on a busy server.
Try it in the Serving Lab: Run a GPU out of KV memory, then fix it without buying another one.
Open the workbook: the formulas, the exact numbers, graded exercises and the sources behind this page.