What is a key-value cache?

Definition

In a transformer language model, previously processed tokens contribute key and value representations used by attention. Keeping those representations avoids recomputing the entire preceding sequence for every new token.

The cache can consume substantial memory, especially with long inputs or many simultaneous requests. Reuse also depends on compatible model settings and matching context, so an old cache cannot be applied blindly to unrelated input.

ELI5

A key-value cache keeps useful working information from what a model has already processed. The model can reuse it as it generates the next part of an answer.

For example, while writing a long response, a model can consult retained information about earlier text instead of processing all of that text from scratch at every step. Keeping that working information also takes memory.

Is this the same as saving a final answer?

No. It stores intermediate attention information rather than simply returning a previously completed response.

Why can long conversations use more memory?

More processed tokens can require more cached attention information, depending on the model architecture and runtime.

  1. Károly Zsolnai-Fehér in a photographic portrait composition on black beside the blue-and-white headline “SHARED AI MEMORY”.
  2. Harshul Jain and Tanmay Sah beside a glowing GPU memory map showing LLM tokens, KV cache blocks and throughput metrics.
  3. Three flat rectangular memory blocks joined by a broad blue connection on black above the blue-and-white headline “SHARED MEMORY LESS WORK”.
  4. 0xSero in a blue top beside the blue and white headline Local Models Real Work on black.
  5. 0xSero gesturing in a blue top beside the blue-and-white headline Inside Pi Agent on black.
    Pi Coding Agent - Deep Dive
    0xSero29m 59s
  6. A flat attention-blue local-agent chip on a black background beside the headline ‘27B LOCAL AGENT’.
  7. Vivek Muppalla beside the headline Healthcare Voice Agent Architecture.
Definition card for key-value cache: What is a key-value cache?

A key-value cache stores intermediate attention information so an AI language model can reuse earlier computation during generation.