KV cache quantization, the short answer: set the cache to q8_0 first. It cuts the memory used by long context almost in half with very little quality loss on most models, while q4_0 saves more but is the setting most likely to hurt long conversations. KV cache quantization stores the attention cache (the keys and values a model keeps for every token in its context) at lower precision than the default 16 bits. You switch it on with q8_0 in llama.cpp, Ollama and LM Studio, or with an FP8 type in vLLM and SGLang. It does not shrink the model weights at all. It only frees memory for longer context or more simultaneous users, and on our CPU test it made generation slower, not faster.
Why the KV cache matters
Every token in your context stores a key vector and a value vector in every attention layer, so the cache grows linearly with context length. Our guide to how much VRAM you need for an LLM walks through the formula:
2 × layers × KV heads × head dimension × bytes per value × tokens
For Llama 3.1 8B, with 32 layers, 8 key value heads and a head dimension of 128, that is 128 KiB per token at 16 bit precision. A 32,768 token context costs 4 GiB, and the full 131,072 token context costs 16 GiB, which is more than the model's own weights at 8 bit. That is the memory KV cache quantization goes after. If you also serve several requests at once, multiply again: Ollama's FAQ says required RAM scales with OLLAMA_NUM_PARALLEL times OLLAMA_CONTEXT_LENGTH.
How much memory each cache type saves
llama.cpp's q8_0 and q4_0 types store values in blocks of 32 with one 16 bit scale per block, according to the block definitions in ggml's source. That works out to 8.5 and 4.5 bits per value. Applied to the Llama 3.1 8B example:
| Cache type | Bits per value | 32k context | 128k context | Where you use it |
|---|---|---|---|---|
| f16 (default) | 16 | 4 GiB | 16 GiB | Everywhere |
| q8_0 | 8.5 | about 2.1 GiB | about 8.5 GiB | llama.cpp, Ollama, LM Studio |
| fp8 | 8 | 2 GiB | 8 GiB | vLLM, SGLang, llama.cpp (f8_e4m3) |
| q4_0 | 4.5 | about 1.1 GiB | about 4.5 GiB | llama.cpp, Ollama, LM Studio |
| nvfp4 | 4.5 | about 1.1 GiB | about 4.5 GiB | SGLang (experimental) |
These numbers are for the cache alone; the weights come on top. Models with sliding window or linear attention layers, such as recent Gemma and Qwen releases, already have much smaller caches, so quantizing them saves fewer gigabytes.
What we measured on our own box
We checked the arithmetic instead of trusting it. On Nov 18, 2026 we loaded the SmolLM2-135M-Instruct Q4_K_M GGUF (30 layers, 3 key value heads, head dimension 64) in llama.cpp build b12521 with an 8,192 token context and read the cache size llama.cpp prints at load time:
| Cache type | KV cache size at 8,192 tokens | Share of f16 |
|---|---|---|
| f16 | 180.00 MiB | 100% |
| q8_0 | 95.62 MiB | 53% |
| q4_0 | 50.62 MiB | 28% |
That is exactly the 16, 8.5 and 4.5 bits per value ratio above, and the f16 figure matches the formula. The log also showed llama.cpp switching Flash Attention on by itself ("required for quantized V cache") for both quantized runs.
Speed went the other way on this CPU only machine. With llama-bench at a context depth of 4,096 tokens, prompt processing measured about 291 tokens per second with f16, 156 with q8_0 and 93 with q4_0, and generation fell too. The box is a shared virtual machine, so the run to run spread was wide and only the direction is meaningful. These numbers say nothing about GPUs, where the fused Flash Attention kernels behave differently. The lesson: treat cache quantization as a memory setting, and measure speed on your own hardware.
Quick settings table
| Runtime | Setting | 8 bit value | 4 bit value | Default |
|---|---|---|---|---|
| Ollama | OLLAMA_KV_CACHE_TYPE environment variable | q8_0 | q4_0 | f16 |
| llama.cpp | -ctk and -ctv | q8_0 or f8_e4m3 | q4_0 | f16 |
| LM Studio | K cache and V cache quantization in the model load settings | q8_0 | q4_0 | off (full precision) |
| vLLM | --kv-cache-dtype | fp8 | none documented | auto (model dtype) |
| SGLang | --kv-cache-dtype | fp8_e4m3 or fp8_e5m2 | nvfp4 (experimental) | auto |
| mlx-lm | --kv-bits | 8 | 4 | off |
How to turn it on, runtime by runtime
llama.cpp
llama.cpp sets the key and value caches separately. The server documentation lists -ctk / --cache-type-k and -ctv / --cache-type-v (environment variables LLAMA_ARG_CACHE_TYPE_K and LLAMA_ARG_CACHE_TYPE_V), with allowed values f32, f16, bf16, f8_e4m3, q8_0, q4_0, q4_1, iq4_nl, q5_0 and q5_1, and f16 as the default. The f8_e4m3 floating point option is new since our last check in October.
llama-server -m model.gguf -c 65536 -ctk q8_0 -ctv q8_0
Two details matter. First, a quantized value cache needs Flash Attention. The default --flash-attn auto turns it on for you, as our test log showed. Second, on NVIDIA GPUs, keep the key and value types the same. The build documentation says standard CUDA builds compile Flash Attention kernels only for the pairs q4_0 with q4_0, q8_0 with q8_0, f16 with f16 and bf16 with bf16. Any other pair falls back to the f16 kernel with a warning unless you build with GGML_CUDA_FA_QUANTS=all or list your pair. A separate draft model for speculative decoding has its own cache flags, -ctkd and -ctvd (long form --spec-draft-type-k and --spec-draft-type-v), with the same allowed types. Our llama.cpp how to covers the rest of the server setup.
Ollama
Ollama has a single global setting, documented in its FAQ. Set the environment variable before starting the server:
OLLAMA_KV_CACHE_TYPE=q8_0 ollama serve
If you run the macOS app rather than ollama serve, set the variable with launchctl setenv OLLAMA_KV_CACHE_TYPE q8_0 and restart the app; on Linux, add it to the systemd service with systemctl edit ollama.service.
The options are f16 (the default), q8_0 and q4_0. Ollama describes q8_0 as using about half the memory of f16 with a very small loss in precision that usually has no noticeable impact, and q4_0 as about a quarter of the memory with a small to medium loss that may be more noticeable at higher context sizes. Quantization only works when Flash Attention is active; Ollama enables it automatically on supported hardware, and you can force it with OLLAMA_FLASH_ATTENTION=1. Because the setting is global, every model you run uses it. Ollama's default context is 4,096 tokens, so the setting only starts to pay off once you raise OLLAMA_CONTEXT_LENGTH or OLLAMA_NUM_PARALLEL. Our Ollama vs vLLM guide explains that tradeoff, and Ollama vs llama.cpp vs LM Studio covers the other differences between these tools.
LM Studio
LM Studio exposes K cache and V cache quantization among a model's load settings, and the same options exist in its SDK as llamaKCacheQuantizationType and llamaVCacheQuantizationType. Its API reference notes that value cache quantization requires Flash Attention and that you set either option to false for full precision. Because LM Studio runs GGUF models on llama.cpp, the matching pair advice above applies on NVIDIA cards too.
vLLM
vLLM uses 8 bit floating point instead of llama.cpp's block formats. Pass --kv-cache-dtype fp8; the vLLM documentation lists fp8_e4m3 (CUDA 11.8 or newer and ROCm) and fp8_e5m2 (CUDA 11.8 or newer). Without calibration every scale defaults to 1.0, so the docs recommend calibrating scales on a dataset with llm compressor, which also enables per attention head scales on the Flash Attention backend. With the Flash Attention 3 backend, queries are quantized to FP8 too. You can leave sensitive layers at full precision with --kv-cache-dtype-skip-layers sliding_window or a list of layer numbers. For when to choose these servers at all, see vLLM vs llama.cpp.
SGLang
SGLang takes the same --kv-cache-dtype flag. Its quantized KV cache docs offer fp8_e4m3 (more precision, smaller range) and fp8_e5m2 (wider range, less precision), plus two experimental FP4 recipes, nvfp4 and fp4_mx_block16. SGLang says FP4 holds about 3.56 times as many tokens as BF16 once scale overhead is counted. Two warnings from the docs: FP8 scales default to 1.0 if neither the checkpoint nor a JSON file supplies them, and an attention backend without fused dequantization can be slow enough to cancel the memory gain.
MLX on a Mac
MLX's mlx-lm package has a --kv-bits option, a --kv-group-size that defaults to 64, and a --quantized-kv-start setting that, per the generate script, starts quantizing only after step 5,000 by default. If you are choosing between formats on Apple Silicon, read our MLX vs GGUF guide.
Does KV cache quantization hurt quality?
It can, and the effect depends on the model and the task. A few points are well supported:
- 8 bit is the safe default. Ollama calls q8_0 the recommended choice if you are not using f16, and halving the cache rarely changes answers noticeably.
- 4 bit is a trade, and hard reasoning shows it first. SGLang's preliminary tests on Qwen3-235B-A22B kept GSM8K flat (0.9168 at 16 bit, 0.9181 at FP8, 0.9186 at FP4) but AIME 2025 fell from 0.7733 to 0.7333 at FP8 and 0.6000 at FP4. Those are SGLang's own numbers on its FP4 format, not on llama.cpp's q4_0.
- Keys and values behave differently. The KIVI paper found that keys have outlier channels and are best quantized per channel, while values suit per token quantization. llama.cpp's simple block formats treat both the same, which is one reason people keep keys at higher precision. On NVIDIA, though, remember the matching pair limitation.
- Smarter grouping can go lower. KIVI's own 2 bit method kept Llama, Falcon and Mistral models at almost the same quality while using 2.6 times less peak memory (including the weights), allowing up to 4 times larger batches and 2.35 to 3.47 times the throughput, according to its abstract. That is a research method, so do not expect the same result from
q4_0. - Model layout matters. Ollama's FAQ notes that models with a high grouped query attention ratio may lose more precision from cache quantization, with Qwen2 as its example.
How to test it on your own setup
- Pick one long prompt from your real work, ideally near the context length you need.
- Run it at f16 and save the answer. With llama.cpp,
llama-bench -ctk f16 -ctv f16 -d 0,4096also records speed at depth. - Repeat with q8_0 for both caches and compare answers and the KV size line in the load log.
- Try q4_0 only if you still need memory, and check answers that depend on details from early in the context.
- On NVIDIA, keep both types equal unless you built llama.cpp with extra kernel pairs.
Pros and cons
- Pros: frees gigabytes for longer context or more parallel users; costs nothing to try; no redownload needed; 8 bit is close to lossless for most models.
- Cons: 4 bit can degrade long context and reasoning answers; requires Flash Attention for the value cache in llama.cpp based tools; mixed key and value types fall back to slower kernels on NVIDIA; can slow CPU inference; does nothing for the weights.
When to use it
- You hit out of memory errors only at long context: switch both caches to q8_0 first.
- You need even more room: compare a smaller weight quant against a q4_0 cache. Our GGUF quantization guide explains the weight side, and how to quantize LLM models covers making your own.
- You serve many users with vLLM or SGLang: use FP8 with calibrated scales.
- Your model already uses sliding window or linear attention: check the real cache size first; the gain may be small. Gemma 4 is a current example.
- You run a DGX Spark or another unified memory box as a server: cache size decides how many users fit; see SGLang vs vLLM on DGX Spark.
FAQ
What is KV cache quantization?
It stores the attention keys and values that a model caches for each token at lower precision, usually 8 bit or 4 bit instead of 16 bit, to cut memory use during long conversations. The weights stay as they are.
Should I use q8_0 or q4_0 for the KV cache?
Start with q8_0. In our llama.cpp test it used 53% of the f16 cache, and Ollama describes its quality loss as very small. Use q4_0 (28% of f16) only if you still need memory, and test long prompts, because errors are more noticeable at higher context sizes.
How do I set the KV cache type in Ollama?
Set OLLAMA_KV_CACHE_TYPE to q8_0 or q4_0 before starting the server (with launchctl setenv for the macOS app). It applies to all models and requires Flash Attention, which Ollama enables automatically on supported hardware.
How do I use KV cache quantization in LM Studio?
Open the model's load settings, turn on Flash Attention, then set K cache and V cache quantization to q8_0 (or q4_0 if you need more room) before loading. In the SDK the same options are llamaKCacheQuantizationType and llamaVCacheQuantizationType. On NVIDIA cards, keep both types the same.
Does KV cache quantization make inference faster?
Not directly. On our CPU only test, q8_0 and q4_0 caches were slower than f16 at a 4,096 token depth. It can help speed indirectly by letting a model stay entirely on the GPU instead of spilling to system RAM. For raw speed, look at speculative decoding.
Is FP8 in vLLM the same as q8_0 in llama.cpp?
No. Both use about 8 bits per value, but FP8 is a floating point format with per tensor or per head scales, while q8_0 is an integer format with a scale for every block of 32 values. llama.cpp now also offers a floating point f8_e4m3 cache type.



