Skip to main content

KV Cache (Key-Value Cache)

Memory optimization in Transformer models that stores the vectors of keys and values from processed tokens in fast GPU memory. This allows for instantaneous generation of each subsequent word but rapidly increases in size with each new message in the chat.

1. Concept Overview & Systemic Problem

When interacting with a language model, you notice two things:

  1. The model outputs words at a stable rate (e.g., 40 words per second). It does not slow down towards the end of a long response.
  2. However, if the dialogue has been ongoing for an hour and the message history has become very large, the program suddenly throws an error: "Out of Memory".

Both phenomena are governed by one mechanism — KV Cache (Key-Value Cache).

From a practical standpoint, this is a short-term memory notebook for AI: to avoid re-reading the entire previous conversation from scratch for each new word, the model stores the mathematical results of already read words in a super-fast GPU memory buffer.

2. How KV Cache Saves Generation Speed

WITHOUT KV CACHE (Catastrophic Slowdown):
Generating word 1  ➔ Calculating word 1 (1 action)
Generating word 2  ➔ Recalculating words 1, 2 (2 actions)
Generating word 100➔ Recalculating all 100 words again (100 actions!)
... Speed drops to nearly zero!

─────────────────────────────────────────────────────────────

WITH KV CACHE (Consistent Lightning Speed):
Words 1..99 are already calculated and stored in the GPU's KV Cache!
Generating word 100 ➔ Calculation of ONLY ONE new token!
⚡ Speed remains equally high from the first to the last word.

3. Why Free GPU Memory is Dwindling

Many users wonder:

  • "The model weighs 5 GB, my GPU has 8 GB. Why does it crash after a large PDF file?"

Because those 3 GB of headroom are instantly consumed by KV Cache:

  • Each token in context requires storing Key and Value vectors at each layer of the transformer.
  • If you have a long document of 32,000 tokens, the size of this table in memory can exceed the weight of the model itself!

4. Production Engineering Scenarios

01. Memory Management in Long-Running Sessions

Implement strategies to limit the context window size, such as reducing from 32k to 8k tokens, to prevent memory overflow during extended interactions.

02. Optimizing for Concurrent Users

Monitor the number of simultaneous users and adjust the KV Cache size dynamically to ensure that the system remains responsive without crashing.

03. Leveraging vLLM for Scalability

Utilize vLLM and PagedAttention to manage memory fragmentation effectively, allowing for increased throughput and handling of multiple requests on a single GPU.

5. Pitfalls, Common Mistakes & Security

Avoid neglecting the impact of KV Cache on memory usage, as it can lead to unexpected crashes during high-load scenarios. Regularly audit the size of the context window and the number of tokens processed to ensure optimal performance. Additionally, implement security measures to protect sensitive data stored in memory, especially in multi-user environments.

/ Frequently Asked QuestionsSchema.org FAQPage

FAQ: KV Cache (Key-Value Cache)

Generation speed would be catastrophically low: to generate the 100th word, the model would have to recalculate attention for all previous 99 words. With each new word, the model would print slower and slower.
/ Internal links
All terms