New Tool Radar

Local AI

How to Reuse a Long Prompt Without Reprocessing It Using mlx_lm.cache_prompt

Cache a long prompt once with mlx_lm.cache_prompt, then reuse the saved cache file for follow-up questions instead of processing the same text again.

Schematic diagram with three boxes: a long practice text file, the command mlx_lm.cache_prompt saving a cache file, and two follow-up questions answered from the cached prompt.

Asking several questions about the same long document means sending the whole document every time — and the model re-reads it every time. Prompt caching avoids that repeated work: caching a prompt can substantially speed up reusing the same long context with different queries. mlx_lm.cache_prompt processes the long text once and saves the result to a file, and later questions use that file as a prefix instead of the full text.

Cache the long text once

Make a practice file, practice-notes.txt, with three fictional lines:

The lighthouse keeper is named Mara.
Mara checks the lamp at noon.
The supply boat comes on Tuesdays.

Then cache it. The command reads the prompt from standard input with --prompt - and writes the cache to a .safetensors file:

cat practice-notes.txt | mlx_lm.cache_prompt \
  --model mistralai/Mistral-7B-Instruct-v0.3 \
  --prompt - \
  --prompt-cache-file practice_prompt.safetensors

Ask follow-up questions from the cache

Use the cached prompt with mlx_lm.generate. The cached prompt is treated as a prefix to the supplied prompt, and when using a cached prompt the model to use is read from the cache and need not be supplied explicitly:

mlx_lm.generate \
    --prompt-cache-file practice_prompt.safetensors \
    --prompt "
Summarize the above text."
mlx_lm.generate \
    --prompt-cache-file practice_prompt.safetensors \
    --prompt "
When is the lamp checked?"
Check What you should see
Cache file A file named practice_prompt.safetensors exists after the first command
First answer A summary naming Mara, the lamp check, and the supply boat
Second answer The answer says the lamp is checked at noon, from the cached text
No model flag Neither follow-up command needs a --model flag — the model comes from the cache

If the second answer does not say noon, the cache was not used — check that the --prompt-cache-file path points at the file the first command wrote. Expected result. We checked the MLX LM README for the cache command and the reuse pattern. We did not run mlx_lm.cache_prompt or mlx_lm.generate.

Limits

The cache only helps when the long text stays the same: if you change the text, the old cache no longer matches it and you must cache the new version. The cache file belongs to one model, so a different model cannot reuse it. Prompt caching saves reprocessing the prefix; the model still generates each new answer fresh, so a wrong answer is a generation mistake, not a cached answer. We did not run mlx_lm.cache_prompt, so no cached file or answer here is a measured result.

Sources

See all