KV-cache prefix stability

cache hitprefill computemutated block
Append-onlyTurn 1Turn 2Turn 3Turn 4Turn 56 blocks prefilledPrefix mutationTurn 1Turn 2Turn 3Turn 4Turn 520 blocks prefilled
  1. Cold start: full prefill, no cached KV.
  2. Append-only: the prior prefix is a cache hit.
  3. Prefix mutation: every downstream block misses.
  4. Same tokens, far less prefill on the left.
Notes & references

Use when

Static system and tools first, deterministic serialization, dynamic state last.

Avoid

Timestamps in the prefix and rewriting earlier messages.

# Bad: changes every call, so nothing is reused
system = f"You are helpful. The time is {now()}."

# Good: the start never changes
system = "You are helpful."
messages.append({ role: "user",
                  content: f"{question}\n(time: {now()})" })