Ollama prompt caching — where you edit decides the bill
Ollama prompt caching re-ran 1,539 tokens in 0.2 s on an exact repeat against 75.6 s cold. One word mid-prompt billed 49.2 s; reordering billed 79.9 s. Six probes, measured.
Ollama prompt caching is always on: matching prefix tokens are free. Exact repeat on 1,539 tokens: 0.2 s vs 75.6 s cold. Change anything after the prefix and the tail re-bills — a 370× spread, no flag flipped.
The cache was always on. The only variable was where we dared to edit.
What does Ollama prompt caching actually keep?
Not your text: one KV tensor per token, held in RAM while the model is loaded. Prompts match from the front; the first difference is the border, everything after it re-reads at full price. Pin keep_alive.
curl http://localhost:11434/api/generate -d '{
"model": "qwen3:4b",
"prompt": "<your 1,500-token prompt>",
"keep_alive": "45m",
"think": false,
"options": {"seed": 42, "temperature": 0, "num_predict": 128}
}'Where you edit decides what you pay
Move one word in the 1,539-token body: last sentence 4.2 s, sentence appended after the question 1.4 s, fact 41 of 96 mid-body 49.2 s. The full ask adds ~25–29 s of generation throughout.
| probe | what changed | prompt-eval | whole ask | vs cold |
|---|---|---|---|---|
| P1 cold | nothing — fresh VM, first ask | 75.6 s | 111.8 s | baseline |
| P2 repeat | nothing — identical body | 0.2 s | 26.6 s | 370× faster |
| P3 suffix | +1 sentence after the question | 1.4 s | 27.2 s | 54× faster |
| P4 mid-edit | 1 word at fact 41/96 | 49.2 s | 75.2 s | 65% of cold |
| P5 reorder | sections 1–2 ↔ 7–8 swapped | 79.9 s | 135.5 s | worse than cold |
| P6 near end | 1 word in the last sentence | 4.2 s | 34.0 s | 18× faster |
prompt-eval is the re-read bill; "whole ask" adds the 128-token generation, which stayed ~25–29 s on every probe.
Why does reordering bill more than a cold load?
Token ~1 is the border. Swap sections 1–2 with 7–8: empty prefix, every token re-bills. P5 cost 79.9 s, 5.7% above cold; generation ran 2.1× slower. Flip caught, culprit not.
spread across six probes on one 1,539-token prompt — the cache was never the variable; the edit position was.
- byte-stable boilerplate at the head: system rules, format contracts, fixture data. One file, one order, one spelling
- volatile part at the tail: the actual question. Edits there bill 4.2 s, not 49.2 s
- never reorder across requests — same data, new order = new bill, and P5 says it can out-bill cold
- pin
keep_aliveacross a chatty-with-gaps loop; an unloaded model takes its discount to the grave (10.6 s reload on our cold probe) - measure with
prompt_eval_count+prompt_eval_duration, not wall-clock vibes — generation time hides the story
- measured 2026-11-12 · six probes, one fresh Arch VM, 2 vCPU · ollama 0.40.16 · seed 42 · temp 0 · log promptcache-2026-11-12.log
- qwen3:4b · 2,497,293,931 B · sha256 359d7dd4…74e7 · CPU bench, no VRAM claims
- prompt-eval read from prompt_eval_count / prompt_eval_duration in the /api/generate response