How much VRAM does your context window need?

Pick a model, a context length and a KV cache type. You get the memory the cache takes, the weights, the total, and the exact setting to use in Ollama, llama.cpp, LM Studio, vLLM or MLX.

tokens
-np
Advanced: flash attention, ubatch, sliding window
-ub, default 512
4.3 GBKV cache
4.8 GBweights at Q4_K_M
9.9 GBtotal, incl. 0.8 GB runtime overhead
125 MiBper extra 1K tokens
  • Full-attention layers: 4.3 GB (32,768 cells after padding)
  • llama.cpp’s compute buffer sits inside the overhead; at ubatch 512 it is typically 0.3–1 GB (an estimate, not from a source).
Setting to use
llama-server -m model.gguf -c 32768 -fa on

Will it fit? Check your GPU in the analyzer — it owns the fit verdict.

Arithmetic from each model’s config.json, using llama.cpp’s allocation rules (commit f3fb2fc). Figures in GB are 10⁹ bytes, like the rest of this site.

KV cache at 8K, 32K and 128K

KV cache only, llama.cpp defaults (one slot, flash attention on). Add the model weights on top.
Model8K f168K q8_032K f1632K q8_0128K f16128K q8_0
Llama 3.1 8B Instruct1.1 GB0.6 GB4.3 GB2.3 GB17.2 GB9.1 GB
Qwen3.8 27B0.7 GB0.4 GB2.3 GB1.3 GB8.7 GB4.7 GB
Gemma 4 31B1.9 GB1 GB3.9 GB2.1 GB12 GB6.4 GB

How it works

The cache is sized layer by layer from each model’s config.json, following llama.cpp’s allocation rules. Sliding-window layers keep only their window, hybrid models keep a small fixed state, and quantised caches include their block overhead.

  1. Choose a model (only models with a published attention config are listed), or enter layers, KV heads and head size yourself.
  2. Set the context you want per request, the KV cache type and the number of parallel slots.
  3. Pick your runtime. It changes how slots multiply the cache and which setting to copy.
  4. Check the total against your GPU in the analyzer, which owns the fit verdict.

Sources

Checked 2026-10-19.

Questions

How much VRAM does 128k context need?

It depends on the model’s attention layout far more than its size. With an f16 cache at 131,072 tokens: Llama 3.1 8B Instruct: 17.2 GB; Llama 3.3 70B Instruct: 42.9 GB; Mistral Small 3.2 24B: 21.5 GB; Qwen3.8 27B: 8.7 GB; Gemma 4 31B: 12 GB; GPT-OSS 120B: 4.9 GB. That is on top of the weights. A q8_0 cache needs a little over half of it.

How much VRAM does 64k context use?

About half of the 128k figure, because full-attention layers grow linearly with context while sliding-window and recurrent parts stay fixed. Llama 3.1 8B needs 8.6 GB of f16 cache at 64k and 17.2 GB at 128k.

Why does my model run out of memory at long context?

In order of how often it happens: the runtime picked a large context for you (llama-server loads the trained context unless --fit lowers it; Ollama defaults to 256k on machines with 48 GB or more of VRAM); parallel slots (OLLAMA_NUM_PARALLEL=4 makes four full caches); flash attention is off, so the attention scratch grows with context; --swa-full or a fork that allocates full sliding-window caches; and images, which add a vision projector and use context tokens.

Does quantising the KV cache hurt quality?

q8_0 is close to lossless: in the llama.cpp maintainers’ test, perplexity moved from 6.232 to 6.234 with q8_0 K and V (PR 7412, May 2024). q4_0 on the K cache is the risk — perplexity rose to 6.438 — while q4_0 on V alone stayed small. Use q8_0 when you need room: it takes Llama 3.1 8B at 128k from 17.2 GB to 9.1 GB. A quantised V cache needs flash attention.

What context does Ollama use by default?

It depends on your GPU memory: 4,096 tokens below about 24 GB of VRAM, 32,768 up to about 48 GB, and 262,144 above that, capped at the model’s trained context. Set your own with OLLAMA_CONTEXT_LENGTH, num_ctx in the API, or PARAMETER num_ctx in a Modelfile.

Do parallel slots multiply the KV cache?

In Ollama, yes: it starts llama.cpp with num_ctx × OLLAMA_NUM_PARALLEL cells, so four slots make four caches (Ollama forces one slot for Qwen3.5-family hybrids). In llama-server, -c is the total shared by all slots, so to give each of four slots 32k you pass -c 131072.

Why do other calculators give a bigger number?

Usually one of four reasons: they charge sliding-window layers for the full context (Gemma and gpt-oss come out several times too large), they apply the head count to MLA models like DeepSeek, they mix GB and GiB or 128,000 and 131,072 tokens, or they ignore Ollama’s parallel-slot multiplier.