skip to content

With Ollama 0.35.0, what does OLLAMA_KV_CACHE_TYPE=q8_0 change, what must be enabled for it to work, and which models does it affect?

level: seniorimportance: nice to knowfreq 6%

answer

  1. precision of the context cache
  2. f16 unless you set it
  3. about half the memory
  4. flash attention must be on
  5. global to the server

basics

~20 s

OLLAMA_KV_CACHE_TYPE=q8_0 stores the KV cache at 8 bits, about half the memory of the default f16. It only works with flash attention enabled, which Ollama 0.35.0 does automatically where supported, and it applies to every model on the server.

solid answer

~40 s

`OLLAMA_KV_CACHE_TYPE` sets the type of the KV cache: `f16` by default, `q8_0` at about half the memory with a very small precision loss, or `q4_0` at about a quarter with a more noticeable loss at large contexts. It only takes effect with flash attention, which Ollama 0.35.0 uses automatically where the backend and devices support it; `OLLAMA_FLASH_ATTENTION=1` forces it on and `=0` forces it off, so a leftover `=0` defeats the setting. It is a server environment variable, read at start and applied to every model the server loads — there is no per-request or per-model version. Confirm it with `ollama ps`: SIZE should fall, and a split model should move toward `100% GPU`.

go deeper

for a junior

Recall that OLLAMA_KV_CACHE_TYPE defaults to f16 and that q8_0 roughly halves the context cache's memory.

for a middle

Explain the three values and their trade, the flash-attention dependency, and that the variable is server-wide and needs a restart.

for a senior

Diagnose a setting that did nothing: flash attention forced off, an unsupported device, or a variable that never reached the service, and verify with ollama ps.

for a principal

Weigh a global cache precision against per-workload needs on shared hardware, deciding when a second server instance is cheaper than degrading every model.

## What the setting is Every loaded model keeps a **KV cache**: stored intermediate values for each token already in its context, so it does not recompute them for every new token. Its size grows with the context length and the number of parallel slots, and on long contexts it can rival the weights. Why it grows and when shrinking it is worth the quality cost are covered by the inference foundations; this answer is about how Ollama exposes the control. `OLLAMA_KV_CACHE_TYPE` sets the numeric type Ollama stores that cache in: | Value | Memory compared with `f16` | Quality note from the docs | |---|---|---| | `f16` (default) | baseline | high precision, highest memory use | | `q8_0` | about 1/2 | very small loss, usually no noticeable impact; the recommended alternative | | `q4_0` | about 1/4 | small to medium loss, more noticeable at larger contexts | The impact depends on the model and the task; the documentation notes that models with a high GQA count (it names Qwen2) may lose more precision from cache quantization than others. The practical rule is to try `q8_0` first and test the model on your own long-context prompts before going to `q4_0`. ## The dependency: flash attention The quantized cache **only works when flash attention is enabled**. In Ollama 0.35.0 flash attention is not something you normally have to turn on: - With `OLLAMA_FLASH_ATTENTION` unset, Ollama uses flash attention **automatically** when the selected backend and devices support it. - `OLLAMA_FLASH_ATTENTION=1` forces it on. - `OLLAMA_FLASH_ATTENTION=0` forces it off. So the two variables interact. A server with `OLLAMA_KV_CACHE_TYPE=q8_0` and `OLLAMA_FLASH_ATTENTION=0` — often left over from an old troubleshooting session — has asked for something the cache type depends on to be disabled, and should not be expected to deliver the saving. The same applies on a device whose backend does not support flash attention. Flash attention also reduces memory as the context grows on its own, which is one more reason not to disable it without cause. ## The scope: one server, every model `OLLAMA_KV_CACHE_TYPE` is a **server-wide** environment variable: 1. It is read by the Ollama server, so it goes where the server's environment is configured — `launchctl setenv` for the macOS app, a `systemctl edit ollama.service` drop-in on Linux, user or system variables on Windows — followed by a restart. 2. It applies to **every model** the server loads; the FAQ calls it a global option. There is no per-request or per-model field for it. 3. Changing it for one long-context model therefore changes the cache precision of the chat, coding and embedding models on the same server as well. If one model needs `q4_0` to fit and the others should keep full precision, the options are a second Ollama server with its own environment and port (set with `OLLAMA_HOST`), or a smaller context for the demanding model instead. ## When to reach for it - **A model that splits only at the context you need.** If `ollama ps` shows a CPU/GPU split at the required CONTEXT, `q8_0` is often enough to bring it back to `100% GPU` without a smaller model. - **More parallel slots.** Each slot carries its own context, so a smaller cache type lets the same GPU serve more concurrent requests. - **Not when everything already fits.** If the model already reads `100% GPU` at the context you need, `f16` costs nothing and keeps full precision. - **`q4_0` last.** Reach for it only after measuring that `q8_0` is not enough and that the quality loss on your own prompts is acceptable. ## How to tell it worked Restart the server, load the model, and compare `ollama ps` before and after: SIZE should drop and, if the model was split, PROCESSOR should move toward `100% GPU` at the same CONTEXT. If nothing changes, check that flash attention is not forced off and that the variable reached the server process rather than your shell — on Linux the systemd unit does not inherit a variable exported in a terminal. Finally, re-run a few long prompts the application depends on, because a cache that fits but answers worse is not a fix.

  • One model on a shared Ollama server needs a q4_0 cache to fit its context, but the others should stay at f16. What are your options?
    The cache type is global, so one server cannot mix them. Run a second Ollama server with its own environment and port via `OLLAMA_HOST` for the demanding model, or keep one server and give that model a smaller `num_ctx`, or try `q8_0` for everyone if its quality loss is acceptable on your prompts.
  • You exported OLLAMA_KV_CACHE_TYPE=q8_0 in a terminal on a Linux box, but `ollama ps` shows no change. Why?
    The variable has to reach the server process, not your shell. When Ollama runs as a systemd service, set it with `systemctl edit ollama.service` as an `Environment=` line, then `systemctl daemon-reload` and restart. Also check that `OLLAMA_FLASH_ATTENTION` is not set to 0.

  • OLLAMA_KV_CACHE_TYPE can be set per request in options
  • The KV cache uses the same type as the model's weights
  • Flash attention is off in Ollama unless you enable it
  • q4_0 cache has no quality cost compared with f16
  • A quantized KV cache works with flash attention disabled

Assumes Ollama 0.35.0, which uses flash attention automatically where the backend supports it; OLLAMA_FLASH_ATTENTION now forces it on or off rather than being the only way to enable it.