← 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)
  1. We expected the hybrid model to be the problem
  2. A one-minute wait became half a second
  3. The hybrid model's smaller speedup comes from a faster cold start
  4. Cached answers differed from cold ones after the first token
  5. Why a cached turn still takes a few hundred milliseconds
  6. How we tested
  7. 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.server and 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

What this means for you

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.