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.

Model4K32K128KAttention
Llama 3.1 8B Instruct0.5 GB4.0 GB16.0 GB32 full-attention layers, 8 KV heads (grouped-query)
Qwen3 8B0.6 GB4.5 GB5.6 GBmax 40K36 full-attention layers, 8 KV heads (grouped-query)
Qwen3 32B1.0 GB8.0 GB10.0 GBmax 40K64 full-attention layers, 8 KV heads (grouped-query)
Gemma 3 27B0.7 GB2.9 GB10.4 GB10 full-attention, 52 sliding-window (1024 tokens) layers
gpt-oss-20b0.1 GB0.8 GB3.0 GB12 full-attention, 12 sliding-window (128 tokens) layers
Qwen3.6 27B0.4 GB2.1 GB8.1 GB16 full-attention, 48 linear-attention layers
DeepSeek R1 (671B)0.3 GB2.1 GB8.6 GBMulti-head latent attention (MLA)
Llama 3.1 8B Instruct at Q4_K_M: the weights stay put, the KV cache grows
  1. 4K context5.8 GB
  2. 8K context6.3 GB
  3. 32K context9.3 GB
  4. 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.

More guides