Sub topic 13
Stable Prefixes and Prompt Caching
This chapter assumes you have used a coding agent for a while and know what a token is. You should also know that every request to the model carries the whole context: instructions, tool definitions, file contents, and conversation history.
Why the same text gets sent again and again
A coding agent does not keep a running memory on the model's side. On each turn it sends the full context again, plus the newest messages. In a long session, most of that text is identical to the previous request. Only the tail is new.
Prompt caching takes advantage of this. The provider stores the processed form of the beginning of a prompt. If your next request starts with exactly the same text, the provider reuses that stored work. Cached input is usually cheaper and faster than fresh input. The exact discount, minimum prompt length, and cache lifetime depend on the provider, so check your provider's documentation.
The prefix rule
A cache hit requires an exact match from the first token onward. The cache covers the longest unchanged prefix. At the first difference, everything after that point is processed fresh.
This gives one practical rule: put stable content first and changing content last.
What breaks the prefix
Anything that changes text near the start of the context invalidates the cache from that point on. Common causes:
- Editing the project instructions file in the middle of a session.
- Adding or removing tools, or reordering the tool list.
- Putting a timestamp, a random ID, or a changing status line at the top of the instructions.
- Rewriting earlier messages, which is what compaction does. After a compaction, the next request is mostly uncached.
- Switching models, since caches are not shared between models.
- Waiting past the cache lifetime. Caches expire after a short idle period, often minutes.
Habits that keep the prefix stable
- Keep project instructions short and fixed. Chapter 6 covered why. Stability is a second reason to avoid frequent edits.
- Put volatile facts at the end. If the agent needs the current date or branch name, let it learn them through a command or a late message, not through the instructions.
- Append, do not rewrite. New information should go on the end of the conversation. Do not edit earlier turns.
- Decide on tools before you start. Enable the tools the task needs at the beginning, and avoid toggling them mid-session.
- Batch your work. A burst of related turns reuses the cache. Long idle gaps let it expire.
The snippet below shows the idea in code. It builds a request with fixed content first and variable content last.
STABLE_INSTRUCTIONS = "You are a coding agent. Follow the project style guide."
TOOL_DEFINITIONS = "tools: read_file, grep, run_tests"
def build_prompt(history, new_message, today):
# Stable parts first: identical on every request, so they can be cached.
prefix = [STABLE_INSTRUCTIONS, TOOL_DEFINITIONS]
# History is append-only, so older turns stay byte-identical.
# The volatile date goes last, with the new message.
tail = [f"Today is {today}.", new_message]
return "\n".join(prefix + history + tail)
history = []
print(build_prompt(history, "Open config.py", "2026-11-27"))If today were placed in the first line instead, every day's first request would miss the cache for the whole prompt.
Caching does not shrink the context
Caching reduces cost and latency. It does not reduce the number of tokens in the window. A cached 150,000-token context still occupies 150,000 tokens, and the symptoms of degradation from chapter 3 still apply. Do not use caching as a reason to let the context grow.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Timestamp at the top of the instructions | Cache miss on every request | Move it to the end or fetch it on demand |
| Editing the instructions file mid-session | Cache miss for the rest of the prompt | Make edits between sessions |
| Toggling tools during a task | Cache miss from the tool list onward | Choose tools up front |
| Long pauses between turns | Cache expires | Work in bursts, or accept the miss |
Exercise
A team's instructions file begins with the line "Build started at <current time>", which is refreshed on every request. Sessions feel slow and costly even though the instructions are otherwise identical. Explain why, and give a fix.
Show answer
The first line changes on every request, so the prompt prefix never matches an earlier one. The cache is missed from the start, and the whole prompt is processed fresh each time. Remove the timestamp from the instructions, or place it at the end of the prompt (or let the agent run a command to get the time). The remaining stable text can then be cached.
Exercise
Does a cache hit mean you can ignore the size of your context? Explain.
Show answer
No. Caching lowers cost and latency for the repeated prefix, but the tokens still fill the context window. A large cached context can still cause the degradation symptoms described earlier, so you still need to scope, trim, and compact.