KV Cache

What is KV Cache?

A KV cache is memory in which a language model stores the key and value data computed for tokens it has already processed, so it need not repeat that work for each new token.

Updated November 2026 · 2 min read


A KV cache is the memory a language model uses to avoid repeating work while it writes. Language models generate text one token at a time, and each new token depends on all the tokens before it. To process a token, the model computes two sets of values, called keys and values, for every token in the context. Without a cache, it would recompute these for the entire context at every step. The KV cache stores them, so each new token only adds its own entry.

The cache makes generation dramatically faster, but it takes up GPU memory, and the amount grows with the length of the context and the number of requests running at once. For long contexts and busy servers, the KV cache can need more memory than the model's weights. That makes it one of the main constraints on how many users a GPU can serve and how long a context it can handle.

Why it matters for cost

  • Throughput: the more KV cache memory available, the more requests a GPU can process at once, lowering cost per token.
  • Long contexts are expensive partly because their KV caches are large. See context window.
  • Prompt caching works by storing the KV cache for repeated prompt content so it can be reused across requests. See prompt caching.

How big is a KV cache?

KV cache size per token = 2 (keys and values) × number of layers × number of key-value heads × head dimension × bytes per value. For a hypothetical model with 80 layers, 8 key-value heads, a head dimension of 128 and 16-bit values, that is 2 × 80 × 8 × 128 × 2 bytes, about 328 KB per token. A single 100,000-token conversation needs about 33 GB of cache, on top of the model weights.

How to reduce KV cache memory

  • Paged attention: storing the cache in small blocks to avoid wasted memory, introduced by the vLLM project in 2023 and now standard in serving engines.
  • Prefix sharing: letting requests with the same starting text, such as a shared system prompt, use one copy of its cache.
  • KV cache quantization: storing cache values at lower precision. See quantization.
  • Model architecture choices, such as grouped-query attention, which reduce the cache size per token.
  • Offloading parts of the cache to slower, cheaper memory.

An example

Consider a hypothetical team self-hosting a model for a document assistant. Users often upload 50-page documents, and the GPU runs out of memory after about eight concurrent conversations, causing requests to queue. Profiling shows the KV cache, not the model weights, is using most of the memory. The team switches to a serving engine with paged attention and prefix sharing, and quantizes the KV cache to 8 bits. The same GPU now handles about 25 concurrent conversations with long documents.

KV cache management is handled by model serving software and is a key part of inference optimization.

Frequently asked questions

How do you calculate KV cache size per token?

Multiply 2, for keys and values, by the number of layers, the number of key-value heads, the head dimension and the bytes per value. A model with 80 layers, 8 KV heads, a head dimension of 128 and 16-bit values needs about 328 KB per token, or about 33 GB for a 100,000-token conversation.

How does the KV cache affect the cost of serving a language model?

KV cache memory grows with context length and the number of requests running at once, and for long contexts it can exceed the model weights. The more memory it takes, the fewer requests a GPU can handle concurrently, which raises cost per token. That is a large part of why long contexts are expensive.

How can you reduce KV cache memory use?

Paged attention stores the KV cache in small blocks to avoid waste, and prefix sharing lets requests with the same opening text use one copy. Quantizing the cache to lower precision, choosing architectures such as grouped-query attention and offloading parts of the cache to cheaper memory also help a GPU serve more concurrent requests.