mrsn0.3.0
auth
live

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.

2026-11-232 min read

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.

cold · 1,539 tok
75.6 s
20.4 tok/s, fresh VM
exact repeat
0.2 s
370× faster, no flag
mid-prompt edit
49.2 s
one word, fact 41/96 — 65% of cold
reorder
79.9 s
5.7% above cold
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.

probewhat changedprompt-evalwhole askvs cold
P1 coldnothing — fresh VM, first ask75.6 s111.8 sbaseline
P2 repeatnothing — identical body0.2 s26.6 s370× faster
P3 suffix+1 sentence after the question1.4 s27.2 s54× faster
P4 mid-edit1 word at fact 41/9649.2 s75.2 s65% of cold
P5 reordersections 1–2 ↔ 7–8 swapped79.9 s135.5 sworse than cold
P6 near end1 word in the last sentence4.2 s34.0 s18× 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.

370×

spread across six probes on one 1,539-token prompt — the cache was never the variable; the edit position was.

  1. byte-stable boilerplate at the head: system rules, format contracts, fixture data. One file, one order, one spelling
  2. volatile part at the tail: the actual question. Edits there bill 4.2 s, not 49.2 s
  3. never reorder across requests — same data, new order = new bill, and P5 says it can out-bill cold
  4. pin keep_alive across a chatty-with-gaps loop; an unloaded model takes its discount to the grave (10.6 s reload on our cold probe)
  5. measure with prompt_eval_count + prompt_eval_duration, not wall-clock vibes — generation time hides the story
The law: prefix hits are free, the first divergence is the border, and the tail always re-bills. Freeze the head byte-for-byte.
  • 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
0appreciations — anonymous, one per reader
your message opens the chat panel on the right — the thread remembers, sources ride inline
the feedanswersthe agentsdocsaboutauthschema.jsonopenapi.jsonllms.txt