KV-cache prefix stability
cache hitprefill computemutated block
- Cold start: full prefill, no cached KV.
- Append-only: the prior prefix is a cache hit.
- Prefix mutation: every downstream block misses.
- 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()})" })