# KV Cache

- **Canonical URL:** https://getaimath.com/dictionary/kv-cache/
- **Topics:** [LLM Inference: Prefill & Decode](https://getaimath.com/dictionary/category/llm-inference-prefill-and-decode/)
- **Last updated:** 2026-11-25

## What is KV Cache?

**In one sentence:** The KV cache is the model's short-term memory of the keys and values it already computed for earlier tokens.

Without it, the model would redo work for every earlier token each time it writes a new one. The KV cache saves that work so each new token only needs a small amount of new math. But it grows with the conversation and can use a lot of memory.

![How an AI Replies: 1. Read Your Question → 2. Type the Answer — Simple two-step cartoon: Step 1 (Prefill) shows the AI scanning your entire question in one flash (causing the brief pause before it starts talking); Step 2 (Decode) shows it typing out the reply one word at a time.](https://getaimath.com/diagrams/llm-inference-prefill-and-decode.png)
*Napkin Diagram (LLM Inference: Prefill & Decode): Every AI reply has two halves: gulping down your whole prompt at once, then speaking the answer one word at a time.*

### Quick Math / Rule of Thumb

Without a KV cache, each new token would mean re-processing the entire conversation so far. With it, the model processes only the new token and reuses the stored keys and values for everything before it. The trade-off: the cache grows with every token, using more memory.

## What is an example of KV Cache?

In a long chat, the KV cache can take several gigabytes of GPU memory.

## Why does KV Cache matter?

The KV cache makes generation fast, but it is often what limits context length and the number of users.

## Related Concepts

- [Autoregressive Generation](https://getaimath.com/dictionary/autoregressive-generation/): Autoregressive generation is creating output one step at a time, where every new token depends on all the tokens before it.
- [Key Cache](https://getaimath.com/dictionary/key-cache/): The key cache is the part of the KV cache that stores the keys for all previous tokens.
- [Token Generation](https://getaimath.com/dictionary/token-generation/): Token generation is the model producing new tokens one after another to form its answer.
- [Value Cache](https://getaimath.com/dictionary/value-cache/): The value cache is the part of the KV cache that stores the values for all previous tokens.
- [Prompt Processing](https://getaimath.com/dictionary/prompt-processing/): Prompt processing is the model reading and understanding your input before it starts writing an answer.
- [KV Cache Size](https://getaimath.com/dictionary/kv-cache-size/): KV cache size is how much memory the KV cache needs, which grows with context length, layers, heads and number of users.

---
Source: The AI Math Dictionary (https://getaimath.com/dictionary/kv-cache/)
