New Tool Radar

Local AI

How to Set the KV Cache Type in Ollama with OLLAMA_KV_CACHE_TYPE

Shrink Ollama's KV cache with a quantized cache type so longer conversations fit in memory.

Schematic illustration with three step boxes: Pick a cache type; Set the variable; Check a long chat.

Every token in a conversation takes space in the KV cache, and that cache can grow larger than the model weights in a long chat. OLLAMA_KV_CACHE_TYPE lets you store the cache in a quantized form, such as q8_0 or q4_0, instead of the default f16, trading a little precision for a lot of memory. This guide picks a cache type and applies it.

Before you start

  • Ollama installed and running.
  • A model you use for long conversations, for example qwen3:8b.
  • A terminal, and the way you start Ollama on your system.

Pick the cache type

The documented values are f16 (the default), q8_0 and q4_0. q8_0 roughly halves the cache size, q4_0 roughly quarters it. Start with q8_0: it is the safer step, and the quality loss is usually too small to notice in chat.

Set the variable and restart

Set the variable before starting Ollama:

OLLAMA_KV_CACHE_TYPE=q8_0 ollama serve

On Linux with systemd, stop the service, add the variable to the service environment, and start it again. The setting applies to models loaded after the restart.

Check a long conversation

Start a chat, paste a long document, and keep asking follow-up questions. Watch ollama ps and your system memory while the conversation grows. The model should stay loaded further into the conversation than it did with the f16 cache.

Worked example

Worked example: with q8_0, paste a 12,000-word report into a chat and ask ten follow-up questions about it. Compare the peak memory used with the same conversation under the default f16 cache; the q8_0 run should use noticeably less memory for the same number of tokens.

Expected result

UNTESTED in this guide: no server was restarted while writing it. The expected result is that Ollama starts normally, the long conversation stays loaded, and peak memory is lower than with the f16 cache.

Limitations

A quantized KV cache can change long-context answers slightly, and q4_0 is more aggressive than q8_0. If answers start to drift in very long chats, go back to q8_0 or f16. The cache type does not change the model file on disk.

Sources

See all