A KV cache saves previously computed attention keys and values so the model can use them again. This avoids repeating work when a language model generates another token. The cache stores numerical intermediate results for a sequence.1
If Q, K and V are still unfamiliar, start with the attention lesson. The useful idea is simple: a new query compares with keys and uses the corresponding values to build a mixture.
KV cache: keep the work you already did
Follow one prompt through prefill and one decode step. Watch what is saved, what is new, and what attention still has to read.
AI-assisted learning visualization · simplified worked example
Our example prompt has three tokens. The model has not processed them yet, so this request’s KV cache is empty.
Follow one generation cycle
- Read the prompt — prefill. The model processes the input tokens and builds their keys and values at each attention layer. The final prompt position provides the prediction used to choose the first output token.
- Process the new token — decode. That chosen token becomes the next input. Each layer computes its new query, key and value.
- Reuse and append. The new query reads the earlier cached keys together with the current key. Attention mixes their values, and the current K/V are retained for the following step.
- Repeat. The model predicts another token, which can be fed back in. A conventional full-attention cache grows as more positions are processed.1
Why is reuse possible?
In a causal decoder, an earlier position cannot read future tokens. With the prefix and model unchanged, adding a later token therefore does not require recomputing those earlier keys and values. Each layer has its own cached tensors; keys from one layer are not a substitute for keys from another.1
In the diagram, follow the older entries as a new entry arrives. The important distinction is between reusing stored vectors and calculating the new token's attention. The second job still happens.
The memory tradeoff
More stored tokens mean more K/V entries. More simultaneous sequences, layers, KV heads or bytes per entry also increase the raw storage required. Actual runtimes may reserve extra capacity or organize the cache into blocks.2
For a small counting example, four tokens stored as two K numbers and two V numbers each require 16 numbers. A fifth token adds four more. Real models repeat this across heads and layers, so the same growth can become a substantial memory cost.
Caching saves recomputation, but full attention still reads a growing set of keys and values during decode. It does not make the work per generated token independent of context length.1
Avoid this misunderstanding
The KV cache is not a collection of finished answers or newly learned model weights. Editing an earlier part of the prompt generally requires rebuilding the affected states. This lesson illustrates ordinary causal decoder inference; sliding windows, compressed caches and other architectures can use different layouts.1
Next: learn how NVFP4 and INT4 store numbers. For more depth, open the KV-cache glossary or return to the learning path.
Sources
Footnotes
-
NVIDIA TensorRT, KV cache tensor layouts and memory ownership. ↩