On a long chat, the KV cache, the model short-term memory of the conversation, grows with every token and can fill the memory. Quantizing the KV cache to 4 or 8 bits shrinks it, so a long run fits where it otherwise would not.
What you need
- A Mac with Apple Silicon and mlx-lm installed
- A downloaded MLX model
- A long prompt or a long conversation to test with
Step 1: Run the long prompt without KV quantization
Run mlx_lm.generate with a long prompt and note whether it runs slowly, swaps, or fails for memory. This is the baseline.
Step 2: Run again with a quantized KV cache
Run the same prompt again with –kv-bits 4 or –kv-bits 8 added. The cache takes less memory, so the same prompt should run more comfortably.
Step 3: Compare memory use and answer quality
Check whether the run completes without heavy swapping, and read the answer for quality. A small quality drop is the trade for the memory saved; if the answer degrades badly, try 8 bits instead of 4.
Worked example: synthetic KV cache quantization
The table below is a synthetic example. It shows the expected result of quantizing the KV cache; no run was made.
| Run | KV bits | Result |
|---|---|---|
| Long prompt, no flag | Default | Slow, heavy memory use |
| Long prompt, –kv-bits 8 | 8 | Completes, answer fine |
| Long prompt, –kv-bits 4 | 4 | Completes, answer slightly weaker |
Expected result (UNTESTED). the unquantized run is slow and memory-heavy, the 8-bit run completes with a fine answer, and the 4-bit run completes with a slightly weaker answer. This check was not run live; the table shows the expected result for the synthetic example.
Limits
- Quantizing the KV cache trades a little quality for memory; check the answer before relying on it.
- You can also cap the KV cache with –max-kv-size, which uses less memory but lowers quality on long chats.
- If the model itself does not fit in memory, KV quantization will not save it; pick a smaller model.
