AI & Machine Learning

KV cache

Also known as: key-value cache, KV caching, attention cache

The KV cache, short for key-value cache, is memory an AI language model uses to store calculations about earlier tokens in a conversation so it doesn't have to repeat them for every new word it writes.

It makes text generation far faster. While the KV cache is essential for speed, it grows with every token in the context and can use more GPU memory than the model itself, which is why long conversations are expensive to serve.

KV cache, explained at three levels

At home

When writing a story, you don't reread the whole thing before each new word; you remember what came before. The KV cache is the AI's way of remembering so it can keep writing quickly.

At work

The KV cache makes AI responses faster, but it uses memory that affects how many users a server can handle at once.

Real-world examples

At home

Thomas ran an AI model on his laptop. Short chats were fine, but when he pasted in a long report, memory filled up because the KV cache grew with every token.

At work

Aisha is an ML engineer at a Vancouver AI startup. Switching to a serving engine with paged KV cache management let each GPU handle roughly twice as many users at once.

How it compares

featureKV cachePrompt caching
What it storesAttention data for tokens in one sequenceReusable prefix across requests
LifespanDuring a requestMinutes, across requests
Who manages itThe serving engineThe provider, exposed as a feature
RelationshipBuilding blockBuilt on saved KV cache

The KV cache speeds up one response. Prompt caching reuses it across many.

For analysts

When budgeting for self-hosted AI, I'd size GPU memory for the KV cache at your expected context length and user count, not just the model weights.

History & origin

The transformer architecture, introduced by Google researchers in 2017, computes 'keys' and 'values' for each token as part of attention. Because past tokens don't change while generating, systems began storing these results instead of recomputing them. As models and context windows grew, the cache became a major bottleneck. Noam Shazeer proposed multi-query attention in 2019, and Google researchers introduced grouped-query attention in 2023, both shrinking the cache by sharing keys and values. In 2023, Woosuk Kwon and colleagues at UC Berkeley introduced PagedAttention and the vLLM serving engine, managing the cache like computer memory pages and greatly improving throughput. DeepSeek's multi-head latent attention, used from 2024, compressed the cache further. In my view, many of the biggest serving-cost improvements came from fighting the KV cache, not from new models.