# 4.2 KV-cache prefix stability

Append-only context maximises cache hits; a prefix mutation invalidates everything downstream.

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.

- **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()})" })
```

## References

- [Context engineering: lessons from building Manus](https://manus.im/blog/Context-Engineering-for-AI-Agents-Lessons-from-Building-Manus) (Manus)
- [Prompt caching](https://platform.claude.com/docs/en/build-with-claude/prompt-caching) (Anthropic)
- [Prompt caching](https://platform.openai.com/docs/guides/prompt-caching) (OpenAI)
- [Unrolling the Codex agent loop](https://openai.com/index/unrolling-the-codex-agent-loop/) (OpenAI)
