KV cache quantization — the bill your context hides
q8_0 stores the KV cache at exactly 17/32 of f16, q4_0 at 9/32. At 32k context the cache bills 4608 MiB against a 2382 MiB model. Receipts, crossover, speed tax — measured, not vibes.
KV cache quantization thins the K/V tensors every past token contributes: q8_0 stores the cache at exactly 17/32 the size of f16, q4_0 at 9/32 — the loader prints it to the byte. The catch nobody prices: on our CPU bench the thin caches were also the slowest at depth.
Download the weights once — the context invoices every session.
What is KV cache quantization?
The cache is the meter. K is the address of a past token's meaning; V is the meaning itself. Quantizing thins their storage and never touches the weights on disk.
Two boot lines from the receipt, unedited — the loader confesses the whole matrix:
llama_kv_cache: size = 4608.00 MiB ( 32768 cells, 36 layers, 1/1 seqs), K (f16): 2304.00 MiB, V (f16): 2304.00 MiBllama_kv_cache: size = 1296.00 MiB ( 32768 cells, 36 layers, 1/1 seqs), K (q4_0): 648.00 MiB, V (q4_0): 648.00 MiB
How much memory does the KV cache use?
Per token, this model's f16 cache costs 144 KiB — 36 layers × 8 KV heads × 128 head_dim × 2 (K+V) × 2 bytes. Nine runs, one model:
| ctx | f16 | q8_0 | q4_0 |
|---|---|---|---|
| 4,096 | 576 MiB | 306 MiB | 162 MiB |
| 16,384 | 2304 MiB | 1224 MiB | 648 MiB |
| 32,768 | 4608 MiB | 2448 MiB | 1296 MiB |
Loader-printed sizes, K+V, 36 layers. The ratios are arithmetic, not vibes — 17/32 and 9/32, exact on all nine rows.
tokens of context before the f16 cache outweighs the model's own 2,382 MiB of weights. Past that, the rent out-weighs the flat.
The crossover, division printed: 2,381.59 MiB × 1024 ÷ 144 KiB/token = 16,935.7. Call it 17k.
Does KV quantization cost speed?
q8_0 is free. q4_0 is not. Same nine runs, generation speed:
| ctx | f16 | q8_0 | q4_0 |
|---|---|---|---|
| 4,096 | 7.04 tok/s | 6.98 tok/s | 6.68 tok/s |
| 16,384 | 7.07 tok/s | 6.99 tok/s | 7.04 tok/s |
| 32,768 | 7.19 tok/s | 6.97 tok/s | 6.55 tok/s |
Eval-time tok/s from verbose timing, 4-token generations. Wobble yardstick: prompt eval swung 13.35–14.0 tok/s across all nine identical setups (±0.3). f16→q8_0 at 32k is 0.22 — inside wobble; f16→q4_0 is 0.64 (−8.9%) — past it. The GPU record agrees in direction: 64K context, f16 KV 116 tok/s vs q4_0's 94 — there the cache type was a VRAM necessity, not a speed choice.
When should you quantize the KV cache?
- everything fits at f16 — leave it; lossless by construction, fastest at depth in both our benches
- context doesn't fit — q8_0: half-ish the cache, speed flat within wobble
- still doesn't fit — q4_0: under a third of f16, ≈ −9% generation at depth
the two knobs, verbatim
llama.cpp -ctk q8_0 -ctv q8_0 # or q4_0 / f16 ollama OLLAMA_KV_CACHE_TYPE=q8_0
Never mix K and V types; the slow path is real. Cache quality at q4_0 was not re-measured here — that number stays the public record's problem, not an invention.
- measured 2026-11-03 — llama.cpp b12069-e6192de0a, CPU-only VM (4 vCPU / 8 GB), nine runs
- model: Qwen3-4B Q4_K_M — 2,497,280,256 bytes = 2,381.59 MiB of weights, loader-printed
- knobs are public record (llama.cpp -ctk/-ctv · Ollama OLLAMA_KV_CACHE_TYPE)
One number to remember: the meter passes the purchase price near 17k tokens — on a model small enough for a jacket pocket.