← Blog · · 7 min read
MLX prompt cache on an M3 Max: 16k-token TTFT drops 115x on Gemma 3, 50x on Qwen3.6
On this page (7 sections)
- We expected the hybrid model to be the problem
- A one-minute wait became half a second
- The hybrid model's smaller speedup comes from a faster cold start
- Cached answers differed from cold ones after the first token
- Why a cached turn still takes a few hundred milliseconds
- How we tested
- What this means for you
If you run a local agent, it sends the same system prompt and tool definitions on every turn, and the model prefills all of them again before it says anything. On our M3 Max, Gemma 3 12B took 61.5 seconds to its first token on a 16k-token agent prompt. With the prompt cache that mlx_lm.server keeps by default, the next nine requests took a median of 533 ms.
TL;DR On a 16k-token prefix, just starting
mlx_lm.serverand reusing its prompt cache cut time to first token (TTFT) 115x on Gemma 3 12B and 50x on the hybrid MoE Qwen3.6-35B-A3B. The hybrid model's ratio is smaller because its cold prefill is faster, not because its cache works worse.
We expected the hybrid model to be the problem
Our earlier posts measured decode speed, KV quantization, batching and speculative decoding. None of them measured prefill, the step that processes your prompt before the first token appears. In an agent loop with a big system prompt, prefill is most of what you wait for.
Prompt caching is the usual fix. You compute the model state for the shared prefix once, and after that you only process each new question. Hybrid models like Qwen3.6-35B-A3B mix attention layers with a recurrent-style state, and MLX cannot trim that state back to an earlier position. We expected reuse on Qwen to fail quietly, to be expensive to copy, or to save much less than on a dense model.
So we compared it with Gemma 3 12B, a dense model with sliding-window attention, at prefixes of 2k, 8k and 16k tokens. The first surprise came before any timing: MLX reported that Gemma's cache could not be trimmed either. Both models had to be forked with a deep copy instead of a trim.
These models differ in size and design. The question is how much caching helps each one, not which model is faster.
A one-minute wait became half a second
We started mlx_lm.server with default settings and sent 10 streaming chat requests per prefix length, each with the same system prompt and a different user question. Request 1 paid for the full prefill. Requests 2 to 10 hit the server's cache, which reported 16290 cached tokens for Gemma and 16371 for Qwen at 16k.
| Prefix | Gemma 3 12B: first → repeat | Qwen3.6-35B-A3B: first → repeat |
|---|---|---|
| 2k | 7.4 s → 431 ms (17x) | 1.9 s → 243 ms (7.7x) |
| 8k | 31.5 s → 524 ms (60x) | 6.6 s → 284 ms (23x) |
| 16k | 61.5 s → 533 ms (115x) | 16.9 s → 335 ms (50x) |
TTFT measured on the client. "Repeat" is the median of requests 2 to 10; request 1 is n=1. Ratios use unrounded values.
At 16k tokens, caching removed about 99% of Gemma's wait and 98% of Qwen's. The cold cost grows with the prefix, while the cached cost barely moves.
On this machine that held for both designs, and we wrote no caching code. But this is one series of 10 requests per length in a single session, so treat it as what you get by default, not a ceiling.
The hybrid model's smaller speedup comes from a faster cold start
If you only read the ratios, the hybrid model looks worse: 50x against 115x. The absolute numbers tell a different story. Gemma's cold prefill at 16k took 3.6 times as long as Qwen's, so it had more to save: about 61.0 s per turn, against 16.6 s for Qwen.
With the cache, both models landed in the same range: 533 ms for Gemma and 335 ms for Qwen. The hybrid cache that can't be trimmed caused no trouble.
We also ran Qwen in-process with a 32k prefix. Cold TTFT was 32.7 s, and with a copied cache it was 323 ms, a 101x speedup. Building that cache once took 32.0 s, about the cost of one cold turn, so it pays off from the next turn on.
Keeping a cache does cost space. At 2k tokens, Gemma's cache file was 0.47 GB and Qwen's was 0.11 GB, 4.4 times smaller. Qwen's 32k cache was 0.73 GB. Hypothesis: Qwen's linear-attention layers keep a fixed-size state instead of per-token keys and values, so only its attention layers grow with the prompt.
Reusing the cache also lowered peak memory. In the Qwen 32k cell, peak memory during the cold run rose 4.39 GB above where the run started, and the cached run rose 0.83 GB. These in-process numbers are single samples, for reasons we explain below.
Cached answers differed from cold ones after the first token
For each question, we compared the first 64 greedy tokens (temperature 0) of the cached run with the cold run.
The first token matched every time. The full 64-token answer never matched. Gemma at 2k diverged at token 52, Qwen at 2k at token 13, and Qwen at 32k at token 7. At 2k, the server's responses did not match the in-process cold text either.
Three sequences are too few to give a rate, but none of them matched. But we did not check whether two uncached runs match each other, so we can't separate caching from ordinary run-to-run differences. If your tests compare agent output against a golden transcript recorded without caching, they may fail; check before relying on exact matches.
Why a cached turn still takes a few hundred milliseconds
On Gemma at 2k, a cached turn took 420.7 ms. The deepcopy returned in 0.45 ms (0.36 ms for Qwen); any deferred copy cost is inside the measured cached TTFT, which is what matters. The other 420.3 ms covered prefilling the 27-token question and producing the first token.
The cached cost still grows slowly with the prefix: from 431 to 533 ms on Gemma and from 243 to 335 ms on Qwen, going from 2k to 16k. Hypothesis: the new question tokens attend over every cached token in the attention layers, so a longer prefix makes even a short question more expensive.
Our hypothesis for the drift is numerical. A cold run also prefills in chunks, but a cached run splits the computation at a different boundary, the end of the prefix. That changes the order of floating-point operations, and when two candidate tokens are nearly tied, the pick can flip. A second hypothesis for Gemma: its sliding-window cache may treat a prefix-then-question split differently from a single prefill. We have not verified either.
import copy
import mlx.core as mx
from mlx_lm import load, stream_generate
from mlx_lm.generate import generate_step
from mlx_lm.models.cache import make_prompt_cache
model, tok = load("mlx-community/Qwen3.6-35B-A3B-4bit")
prefix = make_prompt_cache(model) # prefill once per system prompt
for _ in generate_step(mx.array(prefix_ids), model, max_tokens=0, prompt_cache=prefix):
pass
cache = copy.deepcopy(prefix) # fork per turn: not trimmable, so copy
for r in stream_generate(model, tok, mx.array(suffix_ids), prompt_cache=cache, max_tokens=64):
...
How we tested
- Apple M3 Max with 64 GB unified memory, macOS 27.0.1, on AC power. Python 3.13.5, mlx 0.31.2, mlx-lm 0.31.3, and the
mlx-community4-bit builds of both models. - Prompts: a seeded synthetic agent system prompt (rules plus made-up JSON tool schemas, no real data), sized to each length with each model's own tokenizer and chat template. We used 10 short user questions and checked the shared prefix token by token.
- Server: default
mlx_lm.serverwith temperature 0 and thinking off, 10 streaming requests per length, TTFT measured on the client. - In-process:
stream_generatewith no cache versus a deep copy of a prefilled cache. Our disk-reload step failed, so in-process cells are n=1 (2k for both models, 32k for Qwen) and we have no disk-load timings. Saving a cache to disk took 0.02 to 0.42 s. - The full run is
./run.sh.
What this means for you
- If your agent sends the same system prompt every turn, put
mlx_lm.serverin front of it, or keep one prefilled cache and deep-copy it for each turn. On our machine that cut a wait of seconds, or a minute, to a few hundred milliseconds. - Don't rule out a hybrid MoE model because of caching worries. Qwen3.6-35B-A3B reused its full prefix and had the faster cached turn of the two.
- If you need output identical to an uncached run, as in golden-transcript tests, check before relying on exact matches. Caching doesn't apply if your prefix changes from turn to turn, because only the shared prefix is reused.
Code, raw numbers and charts: https://hackbig.com/blog-data/mlx-prompt-cache-ttft/
If you run prompt caching on another Apple Silicon chip or another hybrid model, what first-request and cached TTFT do you see at 16k tokens?
Code and raw data for this post
How this was made: an AI agent (Claude) designed and ran this experiment on our own MacBook in a sandbox; every number comes from the run's results file (linked below), checked by script before publishing.