Context length and VRAM: how the KV cache eats memory
Updated December 2, 2026. Tables on this page are calculated with the same formulas as the Will it run? tool.
The context is everything the model can see at once: your prompt, any files you paste, and the conversation so far. To avoid recomputing it for every new token, the runtime keeps a KV cache (keys and values for each token at each layer) in memory. It grows with every token of context.
For Llama 3.1 8B Instruct the weights at Q4_K_M take 4.5 GB. The KV cache adds 1.0 GB at 8K tokens and 16.0 GB at its full 128K. At long context the cache can be bigger than the model.
KV cache size for popular models
FP16 cache, which is the default in llama.cpp, LM Studio and Ollama. Contexts are capped at each model's maximum.
| Model | 4K | 32K | 128K |
|---|---|---|---|
| Llama 3.1 8B Instruct | 0.5 GB | 4.0 GB | 16.0 GB |
| Qwen3 8B | 0.6 GB | 4.5 GB | 5.6 GBmax 40K |
| Qwen3 32B | 1.0 GB | 8.0 GB | 10.0 GBmax 40K |
| Gemma 3 27B | 0.7 GB | 2.9 GB | 10.4 GB |
| gpt-oss-20b | 0.1 GB | 0.8 GB | 3.0 GB |
| Qwen3.6 27B | 0.4 GB | 2.1 GB | 8.1 GB |
| DeepSeek R1 (671B) | 0.3 GB | 2.1 GB | 8.6 GB |
- 4K context5.8 GB
- 8K context6.3 GB
- 32K context9.3 GB
- 128K context21.3 GB
Dashed lines mark 8, 12 and 16 GB cards.
Why some models need much less
- Grouped-query attention (GQA) shares keys and values across several attention heads. Almost every recent model uses it, and it's why an 8B model's cache is manageable at all.
- Sliding-window layers (Gemma, gpt-oss) only keep a short recent window (1,024 tokens on Gemma 3, 128 on gpt-oss). Only the full-attention layers grow with context.
- Linear-attention layers (Qwen3.6) keep a fixed-size state instead of a growing cache.
- Multi-head latent attention (DeepSeek) compresses the cache into a small latent vector per token. See how to run DeepSeek locally for the hardware those models need.
How to keep it in check
- Set the context to what you use. 8K–16K covers most chats. Raise it for long documents or coding agents.
- Quantize the cache. llama.cpp can store the cache at 8 bits (
--cache-type-k q8_0 --cache-type-v q8_0), roughly halving it. Our numbers assume the default FP16 cache, so they're on the safe side. - Keep the cache on the GPU. When offloading, put weights in system RAM before the cache. The cache is read for every token.
In the tool, change "Context length" to see how it moves each model's verdict.