You want a bigger context window in Ollama, but every time you raise it the model spills out of VRAM and slows to a crawl. The fix most people miss is not a smaller model. It is K/V cache quantization, which can cut the memory the context uses to about half or a quarter, with one environment variable.
This guide explains what the K/V cache is and why it eats VRAM, how flash attention fits in, the exact OLLAMA_KV_CACHE_TYPE and OLLAMA_FLASH_ATTENTION settings, how to set them on Windows, macOS, Linux and Docker, how much memory you actually save, and when quantizing the cache hurts quality.
Everything is checked against Ollama’s official FAQ and its envconfig/config.go source as of December 2026.
- Set
OLLAMA_KV_CACHE_TYPE=q8_0to roughly halve context memory with almost no quality loss. - Values:
f16(default),q8_0(about 1/2 the memory),q4_0(about 1/4, small to medium quality loss). - Needs flash attention. Ollama uses it automatically where supported; force it with
OLLAMA_FLASH_ATTENTION=1. - Global setting: it applies to every model the server runs.
- Restart Ollama after setting the variables, or nothing changes.
New to Ollama? My complete Ollama guide covers install and first models.
What the K/V Cache Is
When a model reads your prompt, it works out two sets of numbers for every token: a key and a value. It keeps them so it does not have to recompute the whole conversation for every new word. That stored memory is the K/V cache.
The cache grows in a straight line with context length. Double the context and you double the cache. On long chats, coding agents or RAG with big documents, the cache can be as large as the model itself.
That is why raising num_ctx so often pushes a model partly onto the CPU. If you have hit that, my Ollama context length guide explains the context side; this guide is about making that context cheaper.
By default Ollama stores the cache in 16-bit floating point (f16). Quantizing it means storing those numbers in 8 or 4 bits instead.
Flash Attention in Ollama
Flash attention is a faster, more memory-efficient way to compute attention. In Ollama it also matters for another reason: K/V cache quantization only works when flash attention is on. The FAQ says the cache “can be quantized to significantly reduce memory usage when Flash Attention is enabled.”
On current versions, the FAQ says: “Ollama uses Flash Attention automatically when the selected backend and devices support it.” You can override that:
OLLAMA_FLASH_ATTENTION=1forces it on.OLLAMA_FLASH_ATTENTION=0turns it off.
One small note: the variable’s description in config.go still calls it “the experimental flash attention feature”. If you want quantized cache and are not sure flash attention is active on your hardware, setting it to 1 explicitly removes the doubt.
The Three Cache Types
The variable is OLLAMA_KV_CACHE_TYPE. The FAQ lists three values:
| Value | Memory vs f16 | Quality (from Ollama’s FAQ) | Use when |
|---|---|---|---|
f16 |
100% (default) | High precision | You have plenty of VRAM |
q8_0 |
About 1/2 | “Very small loss in precision, this usually has no noticeable impact” | Almost everyone; best default |
q4_0 |
About 1/4 | “Small-medium loss in precision that may be more noticeable at higher context sizes” | Huge contexts on small GPUs |
The FAQ adds one important detail: “Currently this is a global option – meaning all models will run with the specified quantization type.” You cannot set it per model.
The source code does not validate the value, so a typo such as Q8 or q8 will not raise an error. Use exactly q8_0 or q4_0.
How Much Memory You Save
Ollama’s docs only give the ratios. To make them concrete, here is the standard formula for the cache size:
cache bytes = 2 (K and V) x layers x KV heads x head size x context tokens x bytes per value
Take a typical 8B-class model with 32 layers, 8 KV heads and a head size of 128. At f16 (2 bytes per value) that is about 128 KB per token. My worked estimates:
| Context | f16 | q8_0 | q4_0 |
|---|---|---|---|
| 8K tokens | about 1 GB | about 0.5 GB | about 0.25 GB |
| 32K tokens | about 4 GB | about 2 GB | about 1 GB |
| 128K tokens | about 16 GB | about 8 GB | about 4 GB |
These are estimates for that example shape. Real models differ: mixture-of-experts and models with fewer KV heads need less, and some architectures use extra tricks. But the pattern holds: at 32K and above, q8_0 can free several gigabytes, often enough to keep the whole model on the GPU.
Also remember parallel requests. The FAQ says memory “will scale by OLLAMA_NUM_PARALLEL * OLLAMA_CONTEXT_LENGTH.” Four parallel slots at 32K is a 128K-sized cache.
How to Set It on Every OS
These are server settings. They must be set where the Ollama server runs, and the server must be restarted.
Windows
- Quit Ollama from the system tray (right-click the icon, Quit).
- Open Settings and search “environment variables”, then choose “Edit environment variables for your account”.
- Add a new user variable
OLLAMA_KV_CACHE_TYPEwith valueq8_0. - Add
OLLAMA_FLASH_ATTENTIONwith value1. - Click OK, then start Ollama again from the Start menu.
Or from PowerShell, then restart Ollama:
setx OLLAMA_KV_CACHE_TYPE q8_0
setx OLLAMA_FLASH_ATTENTION 1
macOS (app)
launchctl setenv OLLAMA_KV_CACHE_TYPE q8_0
launchctl setenv OLLAMA_FLASH_ATTENTION 1
Then quit and reopen the Ollama app. These launchctl values reset on reboot, so add them to a login script if you want them permanent.
On Apple Silicon, recent Ollama versions run many models on the MLX backend. If the setting seems to have no effect on a Mac, that model may be on a path where this llama.cpp-style option does not apply. My Ollama MLX on Mac guide explains the backends.
Linux (systemd service)
sudo systemctl edit ollama.service
Add under [Service]:
[Service]
Environment="OLLAMA_KV_CACHE_TYPE=q8_0"
Environment="OLLAMA_FLASH_ATTENTION=1"
Save, then:
sudo systemctl daemon-reload
sudo systemctl restart ollama
Running ollama serve by hand
OLLAMA_FLASH_ATTENTION=1 OLLAMA_KV_CACHE_TYPE=q8_0 ollama serve
Docker
docker run -d --gpus=all -e OLLAMA_FLASH_ATTENTION=1 -e OLLAMA_KV_CACHE_TYPE=q8_0 -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama
For every other variable Ollama reads, see my Ollama environment variables reference.
Check That It Worked
- Before: load a model at your usual context and run
ollama ps. Note the size and the GPU/CPU split. - Set the variables and restart the server.
- After: load the same model at the same context and run
ollama psagain. The size should be smaller, and ideally the split now shows 100% GPU.
For more detail, start the server with OLLAMA_DEBUG=1 and read the log. The model load lines show the cache type and size. My Ollama logs guide shows where the log lives on each OS.
You can also watch VRAM directly with nvidia-smi; see how to check GPU VRAM usage.
Best Settings by GPU Size
| GPU VRAM | Suggested | Why |
|---|---|---|
| 8 GB | q8_0, context 8K to 16K; q4_0 only if you must go higher | Model weights already fill most of the card |
| 12 to 16 GB | q8_0, context up to 32K | Keeps 7B to 14B models fully on GPU |
| 24 GB | q8_0, 32K to 64K | Room for 30B-class MoE models with long context |
| 32 GB+ or multi-GPU | f16 or q8_0, as needed | Use q8_0 to fit agents with 128K context |
These are my starting points, not rules. Always compare ollama ps before and after.
When It Hurts Quality
For most chat and coding use, q8_0 is invisible. Ollama’s own FAQ says it “usually has no noticeable impact”.
q4_0 is a different story. The FAQ warns its loss “may be more noticeable at higher context sizes”. In practice, watch for:
- The model forgetting details from early in a long document.
- Worse exact recall, such as quoting code or numbers from far back.
- Tool-calling agents losing track over long sessions.
The FAQ also notes that models with a high GQA count (it names Qwen2 as an example) “may see a larger impact on precision from quantization”. If a particular model gets worse with q8_0, go back to f16 for it, keeping in mind the setting is global.
A simple test: paste a long document, ask for a specific fact from the first page, and compare answers at f16 and at your chosen setting.
Related Memory Settings
| Variable | What it does |
|---|---|
OLLAMA_CONTEXT_LENGTH |
Default context when a request does not set one. The source describes the default as 4K, 32K or 256K based on VRAM. |
OLLAMA_NUM_PARALLEL |
Parallel request slots; each slot needs its own cache |
OLLAMA_MAX_LOADED_MODELS |
How many models stay loaded at once |
OLLAMA_KEEP_ALIVE |
How long models stay in memory (default 5m) |
OLLAMA_GPU_OVERHEAD |
VRAM to reserve per GPU, in bytes |
If the model still runs on the CPU after all this, work through my Ollama not using GPU fix.
Common Problems and Fixes
| Problem | Cause | Fix |
|---|---|---|
| No memory change | Server not restarted | Fully quit Ollama (tray or service) and start it again |
| Still no change | Variable set in the wrong place (shell, not service) | On Linux use systemctl edit; on Windows set user variables and restart the app |
| Cache stays f16 | Flash attention not active | Set OLLAMA_FLASH_ATTENTION=1 |
| Typo has no effect | Value not validated | Use exactly q8_0 or q4_0 |
| Answers worse on long docs | q4_0 precision loss | Switch to q8_0 |
| Docker ignores settings | Not passed to container | Add -e flags and recreate the container |
FAQ
What does OLLAMA_KV_CACHE_TYPE do?
It sets how the context memory (K/V cache) is stored: f16, q8_0 or q4_0. Lower precision uses less VRAM.
Is q8_0 safe to use?
Yes for most models. Ollama says it usually has no noticeable impact on quality while using about half the memory.
Do I need flash attention?
Yes. K/V cache quantization needs flash attention. Ollama turns it on automatically when supported, or you can force it with OLLAMA_FLASH_ATTENTION=1.
Can I set the cache type per model?
No. It is a global server setting for all models.
Does this make Ollama faster?
Indirectly. The big win is that more fits on the GPU, which avoids the slowdown of spilling to CPU.
Should I use q4_0?
Only when you need a very long context on a small card. Quality loss grows with context length.
Does it work in LM Studio too?
LM Studio uses its own model load settings rather than these variables. This guide covers Ollama only.
Sources
- Ollama FAQ (flash attention, K/V cache types, setting variables per OS)
- Ollama envconfig/config.go (variable names and descriptions)
- Ollama Linux docs (systemd overrides)




