Skip to content
,

KV Cache Explained: Why Long Context Uses So Much Memory

The KV cache stores keys and values for every past token. The formula, sizes for Llama, Qwen and DeepSeek, GQA vs MLA, and how FP8 caches and PagedAttention shrink it.

5 min read

KV Cache Explained: Why Long Context Uses So Much Memory

Short answer

The KV cache stores the key and value vectors of every token a language model has already processed, so it does not recompute them for each new token. It makes generation fast, but it grows with every token of context and every concurrent user: KV cache size = 2 × layers × KV heads × head size × tokens × bytes. For Llama 3.1 70B that is about 320 KB per token in FP16, or 43 GB for a full 128K-token context. Techniques such as grouped-query attention, FP8 caches and PagedAttention keep it manageable.

Why language models need a KV cache

A transformer generates text one token at a time. To produce each new token, its attention layers compare the new token's query with the keys of every earlier token and then mix their values. Those keys and values never change once computed, so instead of recalculating them for the whole conversation on every step, the model saves them. That saved data is the KV cache.

Without it, generating token number 10,000 would mean reprocessing the previous 9,999 tokens. With it, each step only computes one new token and reads the cache. The cost is memory: the cache lives in GPU memory next to the model weights.

How big is the KV cache?

Every layer stores one key vector and one value vector per KV head for every token:

KV cache bytes = 2 × layers × KV heads × head dimension × tokens × batch × bytes per value

For Llama 3.1 8B (32 layers, 8 KV heads, head dimension 128) in FP16 (2 bytes): 2 × 32 × 8 × 128 × 2 = 131,072 bytes, or 128 KB per token. A 32K-token conversation therefore needs 4.3 GB of cache, and serving 16 such users at once needs about 69 GB, far more than the model's 16 GB of weights.

KV cache for popular models

ModelLayers × KV heads × head sizePer token (FP16)32K tokens128K tokens
Llama 2 7B (no GQA)32 × 32 × 128512 KB17.2 GB–
Llama 3.1 8B32 × 8 × 128128 KB4.3 GB17.2 GB
Qwen2.5 7B28 × 4 × 12856 KB1.9 GB7.5 GB
Qwen3 32B64 × 8 × 128256 KB8.6 GB34.4 GB
Llama 3.1 70B80 × 8 × 128320 KB10.7 GB42.9 GB
DeepSeek-V3 (MLA)61 layers × 576 compressed valuesabout 69 KB2.3 GB9.2 GB
One user, FP16 cache, decimal GB. Llama 2 7B supported only 4K tokens; it is shown per 32K tokens for comparison. Try any combination in the LLM VRAM calculator.

MHA, GQA, MQA and MLA

The biggest lever on KV cache size is how many key and value heads a model keeps:

  • Multi-head attention (MHA): every query head has its own key and value head. Llama 2 7B uses 32 of each, so its cache is large.
  • Grouped-query attention (GQA): several query heads share one key/value head. Llama 3 uses 8 KV heads for 32 or 64 query heads, cutting the cache by 4–8×.
  • Multi-query attention (MQA): all query heads share a single key/value head, the smallest cache but with some quality cost.
  • Multi-head latent attention (MLA): used by DeepSeek-V2 and V3, it stores a compressed latent vector per token instead of full keys and values, shrinking the cache further while keeping quality.

Some models also use sliding-window attention on part of their layers (Gemma 2 alternates 4,096-token windows; gpt-oss alternates 128-token windows), so those layers only cache recent tokens.

Why the KV cache limits speed

Each generated token must read the model weights and the entire KV cache for that sequence. At short contexts the weights dominate. At long contexts the cache can be larger than the weights, and generation slows down because the GPU spends its time moving cache data through memory rather than doing math on its Tensor Cores. In servers, the KV cache also caps how many users fit on a GPU at once, which directly sets the cost per request.

How to shrink the KV cache

TechniqueWhat it doesTypical saving
FP8 or Q8 KV cacheStores keys and values in 8 bits instead of 162×
Q4 KV cache (llama.cpp)4-bit keys and values with block scalesAbout 3.5×, some quality cost
PagedAttention (vLLM)Allocates the cache in small pages instead of one big block per request, so little memory is wastedMuch higher batch sizes
Prefix cachingShares the cache for identical prompt beginnings, such as a system prompt, across requestsDepends on overlap
Shorter contextOnly reserve as many tokens as you needLinear

In llama.cpp, for example, the cache precision is set with the cache-type options for keys and values, and the context size with the context option; inference servers such as vLLM offer an FP8 KV cache setting.

Frequently asked questions

Does the KV cache affect output quality?

In FP16 it gives exactly the same results as recomputing. An FP8 cache is usually indistinguishable; 4-bit caches can slightly reduce quality, especially on long contexts.

Why does my model run out of memory with a long prompt?

Many runtimes reserve the KV cache for the full context size you set when the model loads. Lower the context length or use an 8-bit cache to free memory.

Is the KV cache the same as the context window?

The context window is the maximum number of tokens a model can attend to. The KV cache is the memory that holds those tokens' keys and values while it does.

Next, see which hardware fits your context in the LLM VRAM calculator, pick hardware in the best GPU for running LLMs locally, or learn how weights shrink in LLM quantization explained.

Sources and further reading

Think you understand it now? Prove it.

Solve real Tensor Core math in the TensorCore.si Challenge and earn a certificate with your name.

Take the challenge

Leave a Reply

Your email address will not be published. Required fields are marked *