From 3.4 s to 1.2 s: caching a system prompt the hard way
Why the standard prompt cache did nothing for Qwen3.5, and how snapshotting the model state after the system prompt made my robot answer almost 3× faster.
This is a deep dive into one optimisation from Billou, my local voice assistant. The setup: a Reachy Mini robot, a base Mac mini M4, and Qwen3.5-4B (4-bit, through mlx-vlm) writing short French replies.
The problem: re-reading the same 960 tokens every turn
Each reply took about 3.4 seconds. That’s a lot, given that the answer itself is only 20 to 40 tokens long. So where did the time go?
Every turn, the model receives:
- the system prompt: the persona, the date, and the documentation of every tool. That’s ~960 tokens, and it’s identical every time;
- the last few exchanges of the conversation;
- the new message.
That’s ~1100 tokens to process before writing a single word. At ~420 tokens/s on this machine, the “reading” part (the prefill) alone took ~2.7 s. Most of the time was spent re-reading a prompt the model had already read on the previous turn.
The obvious fix that did nothing
mlx-vlm has a prompt cache (PromptCacheState): keep the model state from the previous turn, and if the new prompt starts with the same tokens, only process the new part.
| Config | Time per reply |
|---|---|
| 4B as is | 3.43 s |
4B + PromptCacheState |
3.45 s |
No effect at all. And no warning either.
Why: Qwen3.5 can’t rewind
In a classic transformer, the cache is a list of keys and values per token. If the new prompt only shares the first 1000 tokens with the old one, you just cut the cache at token 1000 and continue from there.
Qwen3.5 is a hybrid model: 3 layers out of 4 are Gated DeltaNet, a form of linear attention. Instead of storing something per token, those layers keep a single recurrent state that gets updated with every token. It’s what makes them cheap for long contexts. But there’s a catch: you can’t “cut” a recurrent state at token 1000. Once tokens 1001 to 1100 have been mixed into it, they can’t be taken back out.
So mlx-vlm only reuses the cache if the new prompt extends the cached tokens exactly. In a real chat, that almost never happens: the time in the system prompt changed since last turn, the history got trimmed to the last 4 exchanges, the previous message was stored slightly differently… One token of difference anywhere, and it silently recomputes everything from scratch.
The fix: a snapshot of the system prompt
The part that never changes is the system prompt. So:
- Once, at startup: run the model on the system prompt alone, and keep the resulting state (the snapshot).
- Every turn: start from a copy of that snapshot, and only process the history + new message (~150 tokens instead of ~1100).
# Once: prefill the system prompt and keep the state.
cache = lm.make_cache()
lm(mx.array([system_ids]), cache=cache)
mx.eval([c.state for c in cache])
snapshot = (system_ids, cache)
# Every turn: restore a copy, so the original stays clean.
state = PromptCacheState()
state.update(system_ids, copy.deepcopy(cache))
stream_generate(model, processor, prompt, prompt_cache_state=state, ...)
The copy is important: generating a reply updates the recurrent state in place, so without it the next turn would start from a corrupted snapshot.
One annoying detail: Qwen’s chat template refuses to render a conversation with only a system message. So I render it with a dummy user message, and cut the text right where the user turn starts.
The catch: the prefix must never change
Since the snapshot must match the start of the prompt byte for byte, nothing in the system prompt can change between turns. That broke something: the current time was in the system prompt, so Bilou could answer “what time is it?”. The time changes every minute, which would invalidate the snapshot every minute.
The fix:
- The date stays in the system prompt. It changes once a day, so the snapshot is rebuilt once a day. Old snapshots are dropped.
- The time moves to a small tag at the end of each user message:
[heure : 18h21]. The persona tells Bilou the tag is there, and not to mention it unless asked.
Results
Benchmarked on a real 8-turn conversation, with the real system prompt:
| Config | Time per reply | Verdict |
|---|---|---|
| 4B as is | 3.43 s | |
| 4B + standard prompt cache | 3.45 s | no effect |
| 4B + system prompt snapshot | 1.17 s | ✅ kept: same quality, less memory (3.7 vs 4.2 GB) |
| + speculative decoding (MTP) | 1.25 s | no gain: replies are too short |
| + speculative decoding (DFlash) | 1.67 s | slower, and +2 GB |
| Qwen3.5-2B + snapshot | 0.98 s | ❌ wrong tools, made-up weather |
Almost 3× faster, with the same model and the same answers. Speculative decoding, the usual trick to speed up generation, did nothing here: it speeds up writing, and my replies are only a sentence or two. The bottleneck was reading.
Takeaways
- Measure where the time goes before optimising. I’d have bet on the generation. It was the prefill.
- Know your architecture. Hybrid and linear-attention models (DeltaNet, Mamba-style layers) don’t cache like classic transformers. The usual tricks can silently fall back to doing nothing.
- Put what changes at the end of the prompt. Anything that varies (time, sensor data) belongs after the stable part, never inside it.
You can turn it off with --no-lm-cache to compare. The code is in voice_agent/lm/chat_lm.py.