KV cache

A KV cache stores the attention keys and values a transformer has already computed for earlier tokens, so that generating each new token does not mean reprocessing the whole text.

Language models write one token at a time, and each new token depends on everything before it. Inside every attention layer of a transformer, each token is turned into a query, a key and a value. In models that only look backwards, such as GPT-style chatbots, the keys and values for earlier tokens never change once computed. The KV cache keeps them in memory. At each step the model works out the key and value for the newest token only, adds them to the cache and reuses the rest. Hugging Face's documentation notes that this turns the attention cost of each step from quadratic to linear in the length of the sequence.

The saving in computation is paid for in memory. The cache holds a key and a value for every token, in every layer, for every request being served. NVIDIA's worked example uses Llama 2 7B at 16-bit precision: one sequence of 4,096 tokens needs about 2 GB of cache, against roughly 14 GB for the model weights. Serving eight such sequences at once needs about 16 GB of cache, more than the model itself.

That is why so much inference engineering revolves around the cache. In 2023 the team behind vLLM showed that existing serving systems wasted cache memory through fragmentation and duplication. Their PagedAttention technique, which borrows the idea of paging from operating systems, gave 2 to 4 times the throughput of earlier systems at similar latency. Grouped-query attention, published the same year, lets several query heads share each key and value head, which shrinks the cache while keeping quality close to standard attention. Prefix caching keeps the cache for a shared opening, such as a long system prompt, so later requests that begin the same way skip that work.

The KV cache is also why long contexts are costly. The cache grows with every token kept in the conversation and is normally held in fast GPU memory while the request is being processed. A model advertised with a very long context window can only serve that context to as many users at once as its memory allows.

For anyone reading AI infrastructure news, the KV cache explains a lot: why memory capacity and bandwidth matter as much as raw processing power for running chatbots, why providers offer prompt caching for repeated prompt prefixes, and why techniques that shrink the cache keep appearing in model releases.

Common questions

What is stored in the KV cache?

The key and value vectors that each attention layer computed for every token processed so far. When the model generates the next token, it computes a fresh query, key and value for that token and attends over the cached keys and values instead of recomputing them.

Why does the KV cache use so much memory?

It grows with sequence length, number of layers, model width and number of simultaneous requests. NVIDIA's example puts a single 4,096-token sequence on Llama 2 7B at about 2 GB, so a batch of eight needs about 16 GB, more than the 14 GB of weights.

Is the KV cache used during training?

No. During training a model processes whole sequences in parallel, so there is nothing to reuse step by step. Hugging Face's documentation warns that caching should only be enabled for inference.

What is the difference between a KV cache and prompt caching?

The KV cache works inside a single generation, saving work from one token to the next. Prompt caching is a feature some providers offer across separate requests: when many requests start with the same text, the work done on that shared prefix is kept and reused.

Finding more

Search for "KV cache explained", "PagedAttention" or "grouped-query attention" to see how the cache works and how engineers shrink it. Reliable sources include the documentation of inference libraries such as Hugging Face Transformers and vLLM, the original papers on arXiv, and technical blogs from chip makers that show the arithmetic. Memory figures depend heavily on model size, precision and context length, so check the assumptions behind any number you see quoted.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.