KV Cache: Definition and Examples
KV Cache (Key-Value Cache) is an optimization mechanism used by language models (LLMs) to store already computed attention layer keys and values, avoiding recomputation for each new generated token.
Full definition
KV Cache, or key-value cache, is a fundamental optimization technique in the operation of large language models based on the Transformer architecture. During text generation, the attention mechanism must compute key vectors (Keys) and value vectors (Values) for each token in the sequence. Without caching, these computations would be fully repeated at every new token produced, which would be extremely resource-intensive.
The principle of KV Cache is simple: once a token has been processed, its key and value vectors are stored in memory. When the model generates the next token, it only needs to compute K and V for that new token, then combines them with the already cached values. This transforms a quadratic operation into a nearly linear one, significantly speeding up generation.
In practice, KV Cache determines the amount of GPU memory required to serve a model. For a model like GPT-4 or Claude, the cache can consume several gigabytes of VRAM per request, especially with long contexts. This is why techniques like Multi-Query Attention (MQA), Grouped-Query Attention (GQA), or cache quantization have been developed to reduce this memory footprint.
For the end user, KV Cache explains why the first token of a response is slower to appear (prefill phase where the entire prompt is processed) while subsequent tokens arrive much faster (decode phase where the cache is reused). It also explains why requests with very long contexts are more expensive and slower.
Etymology
The term combines 'KV' for Key-Value, from the Transformer attention mechanism that projects embeddings into three vectors — Query, Key, and Value — and 'Cache', borrowed from computer science where it refers to a fast memory storing frequently used data to avoid recomputing it.
Concrete examples
Inference optimization — Understanding why a long conversation slows down
If you notice your AI chatbot gets slower as the conversation progresses, it's because the KV Cache grows with each message exchanged. Reducing the context length sent or summarizing previous exchanges can help.
Model deployment — Sizing GPU infrastructure
To estimate VRAM needed for KV Cache: cache_size = 2 × number_of_layers × hidden_size × sequence_length × bytes_precision × batch_size. A Llama 70B in fp16 with 4096 context tokens consumes about 2.5 GB of KV Cache per request.
Prompt engineering — Structuring prompts to optimize costs
Place system instructions and static context at the beginning of the prompt. The KV Cache from prefill is reused across successive requests by some providers (prompt caching), which reduces latency and cost if the start of the prompt remains identical.
Practical usage
In prompt engineering, understanding KV Cache allows you to optimize your prompts for speed and cost. Place static content (system instructions, few-shot examples) at the beginning of the prompt to benefit from prompt caching offered by providers like Anthropic or OpenAI. Also keep in mind that very long contexts increase memory consumption and latency, so summarize or truncate long conversations when possible.
Related concepts
FAQ
What is the difference between KV Cache and prompt caching?
Why is the first token slower than subsequent ones?
Does KV Cache impact response quality?
See also
How to use this prompt
- Copy the prompt with the button above.
- Paste it into ChatGPT, Claude or your favorite AI assistant.
- Replace the bracketed variables with your details, then refine the result.
About Prompt Guide
Prompt Guide is a free library of 2500+ ready-to-use prompts for ChatGPT, Claude and other AIs, with guides to learn prompting and tools to build and optimize your own prompts.
More definitions
LangChain: Definition and Examples
LangChain is an open source framework designed to facilitate the development of applications powered by language models (LLMs), by allowing chains of
Large Language Model: Definition and Examples
A Large Language Model (LLM) is an artificial intelligence model trained on massive volumes of text, capable of understanding and generating language
Latent Space: Definition and Examples
Latent space is a compressed mathematical representation where an AI model encodes the essential features of data as numerical vectors, capturing semantic relationships between concepts.
Layer Normalization: Definition and Examples
Layer Normalization is a normalization technique that standardizes the activations of a neural network by computing the mean and variance over the
Leaderboard: Definition and Examples
A leaderboard is a comparative ranking that evaluates and orders AI models according to their performance on standardized benchmarks, allowing users to
Least To Most Prompting: Definition and Examples
Prompt engineering technique that consists of breaking down a complex problem into progressively more difficult sub-problems, solving each one in
Get new prompts every week
Join our newsletter.