Kolkata ·  
AI engineering
foundations
deep · 6 min

KV cache

Keeping attention results for past tokens so each new token doesn't redo the work.

updated 23 Sept 2026

Here's a number that surprises people. Load Llama 2 7B onto an NVIDIA A10 GPU, which has 24 GB of memory. The model's weights take 14 GB. Now serve four users at once, each with a 4,096-token conversation. That takes about another 8 GB, and none of it is the model.

What's using it? Something the model builds while it writes, called the KV cache. To see why it exists, let's watch a model write three words.

Three words

We give a model the prompt "Time". It writes "flies". To get the next token, the simplest way is to run the whole text so far, "Time flies", back through the network. It writes "fast". For the token after that, run "Time flies fast" through again.

Every step redoes the work for "Time", though nothing about "Time" has changed. Stretch that to a 2,000-token prompt and a 500-token answer and the prompt gets reprocessed 500 times.

What exactly is being redone? Recall how attention works. Each token gets three vectors: a query (what it's looking for), a key (what it offers) and a value (what it passes on if picked). To write the next token, the model compares the newest token's query with the keys of every earlier token and blends their values.

Now look at what each step actually needs. It needs one query, for the newest token, since only the newest token is asking. And it needs the key and value of every earlier token. Those come from multiplying each token by fixed learned weights, so once a token is in the text, its key and value never change.

So old queries are never used again, and old keys and values never change. Keep the keys and values, then. That's the cache, and that's the "KV". It holds one entry per token, per layer of the network.

It fits neatly with the two stages of a call from prefill and decode. Prefill reads the whole prompt in parallel and fills the cache with keys and values for every prompt token in every layer. Then each decode step computes the new token's query, key and value, appends the key and value to the cache, and compares the query against everything cached. When the request ends, the cache is thrown away. (If you write this yourself and forget to clear it, the next prompt ends up attending to stale keys from the last one.)

How much does it save? Without a cache, the total work grows with the square of the length. With one, each key and value is computed once and the work grows in step with the length. In one from-scratch test, a small 124-million-parameter model wrote 200 tokens on a Mac Mini's CPU in about 50.5 seconds without the cache and 10.2 seconds with it. About five times faster, and the gap grows with longer text.

The cache is only for writing, by the way. It's used at inference, not in training.

Where the memory goes

Back to our 8 GB. The cache has to sit in GPU memory, next to the weights, for as long as the request runs. Its size works out as:

batch size×sequence length×2⏟keys and values×layers×hidden size×bytes per number\begin{aligned} &\text{batch size} \times \text{sequence length} \times \underbrace{2}_{\text{keys and values}} \\ &\times \text{layers} \times \text{hidden size} \times \text{bytes per number} \end{aligned}

Everything there is fixed by the model except two things: how many requests are running, and how long each one is. The cache grows in a straight line with both.

For Llama 2 7B in 16-bit, that's about 0.5 MB per token, so about 2 GB for one 4,096-token conversation. On the A10, after the 14 GB of weights, there's about 10 GB left, room for about 19,230 tokens of cache. That's four full-length conversations, or a lot more short ones, but not both. At bigger scale it gets silly: one large model with 512 requests of 2,048 tokens each needed about 3 TB of cache, three times the size of the model itself.

So for anyone running a model, the weights are the fixed cost and the cache is the one that grows, with every token of context and every extra user. When memory runs out, the server can't fit more requests into a batch, and batching is how it keeps the GPU busy during decode.

And the cache doesn't make long contexts cheap to use. Every new token still has to read every cached key and value from memory. A longer context means more to read per output token, so writing slows down as the context grows.

Making it smaller

The first fix is to store less. Attention runs many heads side by side, and normally each head has its own keys and values. Multi-query attention shares one set across all the heads. That speeds up decoding a lot, but can cost quality. Grouped-query attention sits in between: a few shared sets, each used by a group of heads. Its 2023 paper found quality close to the full version at speed close to the shared one, and you can convert an existing model with about 5% of its original training compute. Fewer sets of keys and values, smaller cache.

The second fix is to waste less. If you reserve one big block of memory per request, sized for the longest possible answer, most of it sits empty, and that limits how many requests fit. PagedAttention, the idea behind the vLLM serving engine (2023), stores the cache in small fixed-size pages, the way an operating system manages memory. Waste drops to near zero, requests can share pages, and throughput went up 2 to 4 times at the same delay, more for longer sequences and bigger models.

The third fix is to keep less. A sliding window holds only the last N tokens and drops older ones. It caps memory, but the model can't look at anything that fell out of the window.

One request, then gone

So the basic cache is built during a call and thrown away at the end. The next request starts from nothing, even if it begins with the exact same 2,000-token prompt. That's a lot of prefill work done twice.

Keeping cached work around and sharing it between requests is the next step, and serving engines like vLLM support it. When a hosted API offers it to you, it's called prompt caching.

A note on the numbers: most of them are from 2023, on models and GPUs a few generations old. The formula and the trade-off haven't changed, but you can only work the formula out for a model whose layer count and sizes you know, which in practice means open models.

Further reading