KV cache - the model's short-term memory
The KV cache is RAM the model uses to remember every token it has already seen in the current chat.
Short answer
The KV cache is RAM the model uses to remember every token it has already seen in the current chat. The bigger the context, the more RAM the KV cache eats - and it fills up with every message.
Detailed explanation
Every time a language model generates a token, it needs to “look back” at every previous token in the conversation. To avoid recomputing that history from scratch on every step, the model stores intermediate calculations for each seen token - the keys (K) and values (V) - in a chunk of RAM called the KV cache.
The size of the KV cache scales linearly with the number of tokens in the chat (context length), the number of transformer layers, and the number and size of the key/value heads per layer. The formula: KV cache bytes = 2 × n_layers × n_head_kv × head_dim × n_tokens × 2 (the first ×2 is K plus V, the last ×2 is bytes per value at fp16). For Llama 3.1 8B (32 layers, 8 KV heads, head size 128) at 8K context this is 1 GB. For a dense 32B model like Qwen 2.5 32B (64 layers, 8 KV heads) at 32K context it is about 8 GB.
If the model uses GQA (Grouped Query Attention), n_head_kv is smaller than the number of query heads, so the cache is typically 4-8× smaller than it would otherwise be: groups of query heads share the same K and V. Abliteration reads head_count_kv from the GGUF header so the estimate matches reality instead of assuming the worst case.
The KV cache is separate from the model weights themselves. Loading the model file takes RAM (say, 5 GB for a 7B Q4). Chatting with it adds KV cache RAM on top (say, 1 GB more at 8K context). This is why “the model fits” and “the chat runs comfortably” are two different questions.
Why it matters
If you set the context too high for your free RAM, Abliteration can’t allocate the KV cache and either the model fails to load or Windows starts swapping to disk - which slows generation by 10-100×. Understanding KV cache is the difference between “why is my model suddenly at 0.5 tok/s?” and knowing to lower the context.
In Abliteration specifically
The Model info tooltip (the (i) icon next to a model) shows working memory in tokens (e.g. Working memory: 4,096 tokens, with Model's full capacity: … on the next line). The KV cache MB estimate specifically appears in the Edit dialog’s context slider label - as you drag the slider, the label updates live, showing {ctx} / {max} followed by uses ~{kv} MB extra RAM. Dragging the slider higher increases KV RAM linearly.
Abliteration’s KV formula is GQA-correct - it reads head_count_kv from the GGUF header and uses that instead of assuming head_count_kv == head_count (which would inflate estimates 4-8× for modern models). This makes the estimate match reality closely on GQA models like Llama 3, Qwen 2.5, and Mistral. The formula does not shrink for MoE - KV cache doesn’t benefit from expert routing. It does not yet special-case sliding-window attention (Gemma 2/3) or DeepSeek’s compressed latent KV (MLA), so estimates for those models are less accurate.
Related
- GQA - Grouped Query Attention - the reason a 7B model’s KV cache isn’t as huge as it looks
- Context length - the knob that controls KV cache size
- RAM requirements - total RAM = weights + KV cache + overhead