The KV Cache

The KV cache is the optimization that makes LLM generation fast enough to be practical — and the memory hog that makes it expensive. Almost every hard problem in LLM serving, from how many users you can batch to why long contexts cost so much, traces back to this one data structure.

The last post established that decode generates tokens one at a time, each step needing all prior tokens as context. The obvious implementation — re-process the entire sequence from scratch at every step — would be catastrophically slow. The KV cache is what avoids it, and it’s the single most important data structure in LLM inference. Understanding what it stores, why it’s necessary, and what it costs is the foundation for batching, PagedAttention, and long-context economics.

The problem: recomputing the past

Inside a transformer, generating each token involves an attention step where the new token “looks back” at every previous token. Attention works with three vectors per token — a query, a key, and a value. To generate the next token, the model computes a query for the current position and compares it against the keys of all prior tokens, then combines their values accordingly.

Here’s the crucial observation: the keys and values for tokens already in the sequence don’t change as generation continues. Token 5’s key and value are the same whether you’re generating token 6 or token 600. So recomputing them at every decode step — which naive generation would do — is pure waste, and it gets quadratically worse as the sequence grows: step N would reprocess all N-1 prior tokens, making a long generation astronomically expensive.

The solution: cache the keys and values

The KV cache stores the key and value vectors for every token already processed, so each decode step only computes the K and V for the one new token and reuses the cached K/V for all the rest:

Without KV cache: each new token recomputes K,V for ALL prior tokens  → O(n²) work
With KV cache:    each new token computes K,V for itself, reuses cache → O(n) work

Decode step for token t:
   compute Q,K,V for token t
   append K_t, V_t to the cache
   attention(Q_t, all cached K) → combine cached V → next token

This turns generation from quadratic to linear in sequence length — the difference between practical and impossible. The KV cache is why LLM chat is fast enough to use. It’s not an optional optimization; every production inference system relies on it.

The catch: the KV cache eats memory

The KV cache trades computation for memory, and the memory cost is large and, importantly, grows with every token generated. The cache must hold a key and value vector for every token, in every layer, for every attention head, for every request in flight. That product is big:

The consequences dominate serving economics:

Wasted memory: fragmentation

Beyond sheer size, how the KV cache is stored matters enormously, and the naive approach wastes a shocking amount. Because a request’s final length is unknown when it starts (you don’t know how long the answer will be), simple systems pre-allocate a contiguous block of memory for the maximum possible sequence length per request. This causes severe waste:

Studies behind modern serving engines found that naive KV cache management could waste the majority of the memory allocated to it — memory that could otherwise have held more concurrent requests. Since KV cache capacity caps batch size and batch size caps throughput, this waste directly throttles how many users a GPU can serve. That realization motivated the paged approach: manage the KV cache in small fixed-size blocks (like OS virtual-memory pages) allocated on demand, nearly eliminating the waste — the subject of the PagedAttention post.

What this means in practice

The KV cache reframes several practical realities of running LLMs:

The KV cache is the hinge of LLM serving: it makes generation fast, and its memory appetite is what the rest of the series works to tame. Next: how batching turns the GPU’s decode-time idleness into throughput.

Key takeaways

Further reading

Sources & References