Home / Learn / AI Infrastructure / What Is a KV Cache?

On this pageHow a language model generates textThe problem the cache solvesThe two phases of inferenceHow large the cache getsWhy it has become a bottleneckReuse across requestsManaging the cacheWhat it means for hardware and systemsCommon questionsRelated guides

What Is a KV Cache?

A KV cache stores intermediate results that a language model has already computed for earlier tokens, so it does not have to compute them again for each new token. KV stands for key-value, the names of the two sets of numbers that are saved. The cache makes text generation dramatically faster. It also uses a large amount of memory, which grows with the length of the conversation and has become one of the main constraints on serving language models.

How a language model generates text

A language model produces output one token at a time. A token is a word or a piece of a word. To produce each one, the model considers all the text so far: the user’s prompt and everything it has already written.

It does this using a mechanism called attention. For each token in the context, the model computes a few sets of numbers, among them a key and a value. When generating a new token, the model compares it against the keys of all earlier tokens to decide which are relevant, and combines their values accordingly.

The problem the cache solves

Without a cache, generating each new token would require recomputing the keys and values for every earlier token. The context grows by one token each step, so the work would grow with it. Producing a long response would mean repeating the same calculations over and over.

But the keys and values of earlier tokens do not change. Once computed, they stay the same for the rest of the generation.

The KV cache stores them. For each new token, the model computes that token’s own key and value, adds them to the cache and reuses everything already there. The work per token becomes roughly constant instead of growing with the context.

The two phases of inference

The cache gives language model inference two distinct phases.

Prefill. The model processes the whole input prompt at once and fills the cache. This is compute-intensive and can be done in parallel across the prompt’s tokens. Its duration determines how long the user waits for the first token.

Decode. The model generates output tokens one at a time, reading the cache at each step and appending to it. Each step involves relatively little arithmetic and a lot of memory reading.

The two phases stress hardware differently, which has led to system designs that handle them separately.

How large the cache gets

The cache holds a key and a value for every token in the context, at every layer of the model. Its size therefore depends on:

  • Context length. The number of tokens.
  • Model depth. The number of layers.
  • Model width. The size of each key and value.
  • Number format. The precision used to store them.

For a large model and a long context, the cache for a single conversation can reach many gigabytes. That is for one user. A server handling many simultaneous conversations needs a cache for each.

Models with very long context windows make this more acute. The cache for one long session can approach or exceed the size of the model’s own weights.

Why it has become a bottleneck

Accelerator memory is fast, limited in capacity and expensive. It has to hold the model’s weights and the caches of all active conversations.

  • It caps concurrency. The number of users a single accelerator can serve is often limited by cache memory before its arithmetic capacity is exhausted.
  • It makes decode memory-bound. Each generated token requires reading the cache, so the speed of memory limits the speed of generation.
  • It is costly to rebuild. If a cache is discarded and the conversation continues later, the prefill work must be done again.

Reuse across requests

Caches are valuable beyond a single response.

  • Multi-turn conversations. When a user sends a follow-up message, the earlier part of the conversation is unchanged. Keeping its cache avoids reprocessing it.
  • Shared prefixes. Many requests begin with the same text, such as a standard system instruction or a common document. Its cache can be computed once and shared. This is often called prompt caching.
  • Agents. AI agents make many model calls in sequence, each building on a long, mostly unchanged context. Cache reuse greatly reduces their cost.

Reuse saves compute and shortens response times. It also means caches must be kept somewhere between requests, which increases the memory demand.

Managing the cache

Several techniques address the memory pressure.

  • Compression and quantization. Storing the cache in a smaller number format.
  • Efficient memory allocation. Managing cache memory in blocks, much as an operating system manages memory in pages, so space is not wasted and common prefixes can be shared between requests.
  • Eviction. Discarding caches that are unlikely to be needed.
  • Attention variants. Model designs that share parts of the computation and so store less.
  • Offloading. Moving caches that are not in active use from accelerator memory to cheaper tiers such as system memory or fast storage, and bringing them back when the conversation resumes.

What it means for hardware and systems

The KV cache has shifted where the limits lie in inference systems.

  • Memory capacity and bandwidth matter as much as arithmetic speed.
  • Fast paths between memory tiers are needed to move caches in and out without long delays.
  • Routing matters. Sending a follow-up request to the machine that already holds its cache avoids recomputation.
  • Storage and networking become part of inference performance, when caches are held or shared outside the accelerator.

For these reasons, the cache is now a central consideration in designing both inference accelerators and the infrastructure around them.

Common questions

What does KV stand for in KV cache?

Key and value. They are two sets of numbers that the attention mechanism computes for each token and that the cache stores for reuse.

Does the KV cache change the model’s output?

No. It stores exact intermediate results. The output is the same as if everything had been recomputed. It only saves work.

Why does a longer conversation use more memory?

The cache holds an entry for every token in the context. As the conversation grows, so does the cache.

Related guides