Home / use case

Quantizing the Ollama KV cache on a laptop: half the memory, slower replies

At a glance
Hardware
Surface Laptop Ultra with RTX Spark, driver 617.42, on AC power with the Balanced plan
Models & engines
Ollama 0.40.11 running qwen3-coder:30b
OLLAMA_FLASH_ATTENTION=1 plus OLLAMA_KV_CACHE_TYPE set to f16, q8_0 or q4_0
Key gotcha
Setting the cache variable is a request, not a guarantee; on an unsupported model it can fall back to f16 without an error.

What they built

The author ran qwen3-coder:30b locally in Ollama on a Surface Laptop Ultra with RTX Spark and tested whether quantizing the attention cache would speed up replies to a 100K-token prompt. Based on memory bandwidth, they predicted q8_0 would reach 34 tokens per second and q4_0 52, up from about 20 with the default f16 cache.

Both quantized caches turned out slower: 13.8 tokens per second with q8_0 and 13.1 with q4_0, against 19.9 with f16. The memory savings matched expectations, and the smaller cache let the model fit its full 262,144-token context on the GPU.

A PowerShell script, kv-cache-bench.ps1, ran the same code prompts and long prompt for each cache type and compared the replies word by word.

Setup

  • Surface Laptop Ultra with RTX Spark, driver 617.42, on AC power with the Balanced plan
  • Ollama 0.40.11 running qwen3-coder:30b
  • OLLAMA_FLASH_ATTENTION=1 plus OLLAMA_KV_CACHE_TYPE set to f16, q8_0 or q4_0

How it works

  1. The benchmark script stops the tray app and runs ollama serve with the variables set for that process only, then restarts the tray app.
  2. Each cache type got three code prompts with 512-token replies and a 100,560-token prompt with a 384-token reply, repeated three times with the model unloaded between repeats.
  3. The script checks the llama-server command line and the llama_kv_cache size line to confirm the cache type actually took effect.

Results & numbers

  • The KV cache shrank from 9,600 MiB (f16) to 5,100 MiB (q8_0) and 2,700 MiB (q4_0).
  • Prompt processing at 100K tokens was 760, 780 and 803 tokens per second for f16, q8_0 and q4_0.
  • With q4_0 the full 262,144-token context loaded entirely on the GPU; a 242,898-token prompt took 11 minutes 36 seconds to first token and decoded at 6.5 tokens per second.
  • All nine long replies were accurate; the quantized caches described the same files with different wording.

Gotchas

  • Setting the cache variable is a request, not a guarantee; on an unsupported model it can fall back to f16 without an error.
  • A quantized cache needs flash attention, and Ollama gives keys and values the same type; mixed types need llama.cpp directly.
  • Windows PowerShell 5.1's Invoke-RestMethod never reached the freshly started server and stalled silently; PowerShell 7 worked.
  • Ollama silently kept only half the context on an early attempt with oversized prompts.
“Setting the variable is a request, not a guarantee”— Steef-Jan Wiggers

Do it yourself

  1. Quit the Ollama tray app.
  2. Set OLLAMA_FLASH_ATTENTION=1 and OLLAMA_KV_CACHE_TYPE to q8_0 or q4_0 for the process, then run ollama serve.
  3. Load your model and look for --cache-type-k and the llama_kv_cache size line in the server log.
  4. Keep f16 if your context fits; switch to q4_0 when you need a much longer context or free memory.
  5. Measure decode speed at your real prompt length before and after the change.
$env:OLLAMA_FLASH_ATTENTION = "1"
$env:OLLAMA_KV_CACHE_TYPE = "q8_0"
ollama serve
Source: Steef-Jan Wiggers (DEV Community) · Published 2026-10-27 · Added 2026-10-29. This write-up summarizes the original source; see it for full details.

Similar setups

Two Qwen Code sessions on one Ollama slot waited 46 seconds; a slot each waited one

Tensward ran two Qwen Code sessions against Qwen3.8-27B on a 24 GB L4 and found that sharing one Ollama slot raised the median wait for a response to start from about one second to 46.

Tensward · 2026-11-19

Local code review bots on one RTX 3090 with Ollama, LiteLLM and Gitea

TechLuddite runs local 4-bit models as advisory code reviewers on pull requests in a home lab, and measured which of ten models found real problems.

X (@gh_TechLuddite) · 2026-11-09

A copy-paste path to a local OpenAI-compatible LLM endpoint with LM Studio, Ollama, or llama.cpp

Mikalai Karytka’s Nov 2026 guide walks through installing LM Studio’s lms CLI (plus Ollama and llama.cpp equivalents), sizing RAM for a quantized model, serving an OpenAI-compatible API, and checking tool-calling before you build agents on top.

Mikalai Karytka (DEV.to) · 2026-11-05

LM Studio vs Ollama on an RTX 5080: memory settings mattered more than the app

LK Wood IV ran LM Studio and Ollama on the same RTX 5080 with identical GGUF files and found the apps within 14% of each other, while one wrong offload or context setting cost up to 3.4x.

LK Wood IV (DEV Community) · 2026-11-01