What Is KV Cache?
KV cache (key-value cache) is a store of previously computed key and value vectors used during large language model inference. It lets models respond faster by reusing these vectors rather than recalculating them for all previous tokens each time they generate a new token.
How Does KV Cache Work?
At its core, KV cache is a deliberate trade-off: It saves significant compute by storing key and value tensors in memory for a small cost in capacity that pays for itself many times over in speed.
When an AI model generates a response, it does not produce the whole reply at once. It produces one token at a time, and before generating each new token, the attention mechanism needs to look back at every token that came before. In transformer models, "attention" is the process that determines which previous words matter most to the current word being generated.
When processing tokens, attention creates three mathematical representations of every token: a key (K), a query (Q), and a value (V). You can think of the query as a request for information, each key as a label used to determine how well a token matches that request, and each value as the information that token can contribute.
The attention computation then takes into account K, Q, and V for the new token, as well as the key and value for every prior token (queries for prior tokens are not used). This is how the model understands context. Without caching, AI infrastructure would need to recompute the K and V matrices for all prior tokens for every new token, which would grow computationally expensive very quickly.
KV cache solves this by acting like a notepad—storing K and V matrices as the model processes the input so they don’t have to be recomputed. This makes the model significantly faster, especially in long conversations or when processing long documents. As the model processes the input prompt (the “prefill” phase), it computes K and V tensors for each token at each transformer layer. These tensors represent what the model "knows" about each token in context. Rather than throwing them away, they get stored in GPU memory—that's the KV cache. During the "decode" phase (generating new tokens), the model only needs to compute K and V for the new token and retrieve the rest from cache, reducing the work per step.
The practical impact is significant: The more context a model carries (a long conversation, a lengthy document, a detailed system prompt), the more time the KV cache saves. If the cache gets too full and entries are removed, the model has to redo that work from scratch. Keeping the cache well managed is therefore central to delivering fast, cost-effective inference.
Quick Links
Applications and Use Cases of KV Cache
KV cache is essential to nearly every deployment of large language models in the current AI ecosystem, especially as mixture-of-expert (MoE) models have become the standard and models use reasoning to solve longer, more complex prompts.
Agentic AI further highlights the importance of KV cache. AI agents that reason across many steps, call tools, and loop back need to carry context across the full reasoning chain. In transformer-based models, that long-term memory is realized as inference context—the KV cache, which preserves context so the model does not recompute history for every new token. As agent workflows grow more complex and multi-turn, KV cache persistence across agent steps becomes essential infrastructure.
Anywhere a model repeatedly attends to previously seen content, KV cache converts that from redundant compute into a fast memory lookup. The more repetition or shared context in a workload, the bigger the benefit. KV cache applies to virtually every generative AI workload—any application where a model produces more than a single output token. The gains are largest wherever shared or repeated context appears. The following scenarios illustrate where those advantages are most pronounced.
Retrieval-Augmented Generation: When a system retrieves documents and puts them into a long prompt before asking a question, the document content is identical across many similar queries. KV cache lets the system compute the document representations once and reuse them. When users ask questions about a large document that repeats across requests but with different questions at the end, KV cache reuse delivers roughly a 2x speedup in time to first token.
Long-Context Document Analysis: As models expand context windows—such as when they analyze legal documents or business contracts—the context window can reach millions of tokens. KV cache becomes a bottleneck since it grows linearly with prompt length and must reside in GPU memory during generation. KV cache offloading to host CPU memory or SSD is often what makes million-token contexts practically deployable.
Large-Batch and High-Concurrency Inference: Serving more users simultaneously means holding more active context in GPU memory at once, which puts significant pressure on both memory capacity and bandwidth during the decode phase. NVIDIA NVFP4 KV cache reduces memory footprint by up to 50% compared to FP8, effectively doubling the batch sizes and user concurrency a given GPU can support, while reducing the memory bandwidth consumed by the constant read/writes of the KV cache during decoding.
What Are the Benefits of KV Cache?
Challenges and Solutions
KV cache comes with trade-offs that engineering teams need to manage. Here are the most common challenges and how they are addressed.
FAQs
KV cache delivers real speed improvements by converting redundant recomputation into a fast memory lookup. The gains grow with how much context is shared or repeated across requests: Workloads with shared system prompts, repeated document content, or long conversation histories see the greatest benefit in both latency and cost per response.
For any organization running AI at scale, response speed and infrastructure cost are major operational factors. KV cache directly reduces how much computation is needed per response, which simultaneously lowers cost and improves the user experience. Organizations that manage KV cache well can serve more users on the same hardware, which is a meaningful advantage as AI workloads grow.
When the GPU memory used for the KV cache fills up, the system must either remove older stored context (requiring the model to redo that work if needed again), limit how long a conversation the model can handle, or add more hardware. NVIDIA Dynamo addresses this by automatically offloading less-active stored context to lower-cost memory and retrieving it when needed, without interrupting the AI's response.
Retrieval-augmented generation (RAG) is a technique for finding relevant information, such as internal documents or database records, and feeding it to an AI model as context. KV cache is an efficiency technique that stores the AI's work on whatever context it has already received, so it doesn’t repeat that work. The two complement each other: RAG decides what information the model reads, and KV cache makes reading it faster and cheaper.
KV cache works with the transformer architecture used by the vast majority of today's large language models, including models used for text, code, and multimodal tasks. Some newer model architectures handle long sequences differently and don’t rely on a traditional KV cache, though they address the same underlying challenge of processing context efficiently.