Skip to content
IJK

The KV Cache: What It Stores and Why It Scales With Context

By IJK ·

What a Hugging Face post says the KV Cache stores, how autoregressive generation works and why the cache scales linearly with the context window.

Autoregressive generation

In autoregressive models, text generation happens token by token, with each prediction relying on all preceding tokens for context. [1]

What the KV Cache stores

KV Cache optimizes this process by storing the intermediate results—keys (K) and values (V)—from the attention layers, so the model can reuse them for future tokens instead of recalculating them. [2]

optimized bybutToken-by-token generationeach prediction relies on all preceding tokensKV Cachestores keys and values so the model can reuse the...Linear scaling with context windowthe major drawback
What does the KV Cache store and cost?

How it scales

As powerful as the KV Cache is, it comes with a major drawback-it scales linearly with the size of the context window. [3]

The values stored in the KV Cache come from all the attention blocks used by the model. [4]

Sources

  1. The Hugging Face source says: In autoregressive models, text generation happens token by token, with each prediction relying on all preceding tokens for context. Mastering Long Contexts in LLMs with KVPress
  2. The Hugging Face source says: KV Cache optimizes this process by storing the intermediate results—keys (K) and values (V)—from the attention layers, so the model can reuse them for future tokens instead of recalculating them. Mastering Long Contexts in LLMs with KVPress
  3. The Hugging Face source says: As powerful as the KV Cache is, it comes with a major drawback-it scales linearly with the size of the context window. Mastering Long Contexts in LLMs with KVPress
  4. The Hugging Face source says: The values stored in the KV Cache come from all the attention blocks used by the model. Mastering Long Contexts in LLMs with KVPress

Editorial (AI-assisted; automatically checked, not human-reviewed). Each statement is linked to its source; check the source before relying on a detail.

  • llm
  • kv cache
  • inference
  • hugging face