What context cost in our tests

Ollama writes in its log how much it reserves for the context when it loads a model (the “context” column of the memory summary). These were the reservations on the 16 GB card, in MiB.

  • qwen3.5:4b: 178 at 4,096 tokens, 306 at 8,192 and 1,074 at 32,768.
  • qwen3.5:9b: 178 at 4,096, 306 at 8,192, 562 at 16,384 and 1,074 at 32,768.
  • gemma4:12b: 544 at 4,096, 768 at 8,192, 896 at 16,384 and 1,152 at 32,768.
  • Total on the graphics card (weights, context and compute): qwen3.5:4b went from 2.7 to 3.7 GiB, qwen3.5:9b from 5.0 to 6.0 GiB and gemma4:12b from 7.9 to 8.7 GiB between 4,096 and 32,768 tokens.

The formula, and why models differ so much

To write each new word, the model keeps two tables of numbers (keys and values) for each token and each layer that uses attention. That is the KV cache, and Ollama creates it in full for the context you reserve when the model loads. Cache bytes = 2 × layers with cache × KV heads × dimension per head × tokens × bytes per value (2 with the normal f16 cache).

qwen3.5:9b keeps a cache in only 8 of its 32 layers (the log says “32768 cells, 8 layers”), with 4 KV heads of dimension 256: 2 × 8 × 4 × 256 × 2 = 32 KiB per token, so 1,024 MiB at 32,768 tokens plus a 50 MiB fixed state, the 1,074 above. gemma4:12b has 8 full-attention layers at 16 KiB per token and 40 “sliding window” layers capped at 2,048 positions, 640 MiB that don’t grow, which is why it starts higher and grows slowly.

Llama 3.1 8B, a classic design where every layer keeps a cache (32 layers, 8 KV heads, dimension 128, according to its config.json), costs 128 KiB per token: about 1 GiB at 8,192 tokens, 4 GiB at 32,768 and 8 GiB at 65,536 (estimates with the formula). With its 4.58 GiB of weights in Q4_K_M, at 32,768 tokens it needs about 9 GiB with margin: no longer an 8 GB card. You can’t deduce context cost from model size.

How to measure it on your PC

Load the model twice, once with 4,096 of context and once with the context you want, and compare. The difference is what context costs you with that model.

Read the memory split in the Ollama log (PowerShell)
  1. $log = "$env:LOCALAPPDATA\Ollama\server.log"
  2. Select-String -Path $log -Pattern 'memory breakdown','CUDA0 \(','offloaded' | Select-Object -Last 6 | ForEach-Object { $_.Line }
  3. qwen3.5:9b at 4,096: | - CUDA0 (RTX 5060 Ti) | 16283 = 15172 + (5098 = 4812 + 178 + 108) + -3986 |
  4. Read it as: 16,283 MiB total, 15,172 free; the model takes 5,098 = 4,812 of weights + 178 of context + 108 of compute
  5. Then run ollama ps: the CONTEXT column confirms what it loaded, and PROCESSOR must still say 100% GPU

Real lines from the Ollama 0.40.14 log, trimmed to fit. The exact text may change with the version; search for the single words if a line is missing.

Find your maximum context, and compress the cache if needed

Raise the context step by step. Your limit with that model is the last value where ollama ps still says “100% GPU”; once it shows a CPU/GPU split, the model no longer fits and it slows down or fails (see Ollama error 500 “unable to load model”). How to set the context so your app respects it is in Ollama context length on Windows.

If the weights fit but not with the context you need, the cache can be quantized too. According to Ollama’s documentation, with flash attention enabled the OLLAMA_KV_CACHE_TYPE variable accepts f16 (the default), q8_0 (about half the cache memory) and q4_0 (about a quarter). We didn’t test it, for memory or for quality; if you use it, compare results on your own tasks.

Ollama’s default context depends on your VRAM: 4k tokens below 24 GiB, 32k from 24 to 48 GiB, and 256k from 48 GiB up. The documentation recommends at least 64,000 tokens for agents and coding tools; at that size, qwen3.5’s cache would be about 2 GiB and Llama 3.1 8B’s about 7.8 GiB, by the formula. In LM Studio, lms load <model> --context-length 16384 sets it, and lms load --estimate-only <model> prints a memory estimate without loading.

Frequently asked questions

What is the KV cache?The memory where the model keeps keys and values for every token it has read, per attention layer, so it doesn’t recompute them for each new word. Its size depends on the reserved context, not on what you write.

Does doubling the context double the VRAM?Only the cache part, and only for models that cache every layer. For qwen3.5 and gemma4 in our tests, eight times more context cost under 1 GiB more in total.

How do I reduce KV cache memory in Ollama?Lower the context, or set OLLAMA_KV_CACHE_TYPE to q8_0 or q4_0 with flash attention enabled, according to the documentation. Check quality afterwards.

Why does a model fit at 4,096 but split at 32,768?Because the cache grew past your free VRAM. Read the context column in the log to see by how much.