Sub topic 6
Prompt caching and keeping prefixes stable
This chapter assumes you can read basic Python, know that an agent loop resends its whole context on every turn, and know that input tokens are billed per million. If the cost of a loop without caching is unfamiliar, review the earlier chapter on agent loop cost first.
What prompt caching does
In an agent loop, each turn sends the system prompt, the tool definitions, and all earlier messages again. Most of that text is identical to the previous turn. Prompt caching lets the provider reuse its work on that repeated text. Tokens read from the cache are billed at a fraction of the normal input rate.
The cache matches on a prefix: the tokens from the start of the request up to the first point where the new request differs from the cached one. Everything before that point can be read from the cache. Everything after it is billed at the full input rate.
The cost effect
The function below computes input cost for one turn when part of the context is read from cache.
def input_cost_with_cache(context_tokens, cached_prefix_tokens, price_input, cache_read_multiplier):
"""Input cost in USD for one turn when a prefix is read from cache.
cache_read_multiplier is the fraction of the normal input rate charged for
cached tokens. It varies by provider, so take it from the provider's docs.
"""
uncached = context_tokens - cached_prefix_tokens
effective_tokens = uncached + cached_prefix_tokens * cache_read_multiplier
return effective_tokens * price_input / 1_000_000
# Placeholder numbers: 40k-token context, 35k of it a stable cached prefix.
print(input_cost_with_cache(40_000, 35_000, 3.0, 0.1)) # 0.0225Without caching, the same turn costs 40,000 × 3.0 / 1,000,000 = 0.12 USD. With a 35,000-token cached prefix it costs 0.0225 USD. The cost of a turn now depends mostly on the new, uncached tokens.
Many providers also charge extra the first time a prefix is written to the cache, and cached entries expire after a short time without use. Check your provider's current rules for write cost, minimum prefix length, and lifetime. The numbers above are placeholders.
Keeping the prefix stable
A cache hit requires the new request to match the old one exactly up to the cached point. One changed token near the start invalidates everything after it. The common ways agents break this are:
- Putting a timestamp, session ID, or random value in the system prompt.
- Reordering or editing tool definitions between turns.
- Inserting new instructions near the top instead of appending them at the end.
- Rewriting earlier messages, for example by trimming old tool output in place.
- Switching models mid-session, because caches are usually specific to a model.
The design rule is to order content from most stable to least stable:
- System prompt and tool definitions, which rarely change.
- Project instructions and other long-lived context.
- Conversation history, which only grows at the end.
- The newest user message or tool result.
Volatile details such as the current time belong in the last part of the request, not the first.
Checking that caching works
Do not assume caching is on. Provider responses usually report cached tokens in their usage data, in fields such as cache read and cache write counts. Field names differ by provider. After a few turns of a session, look at the cache read count. If it stays at zero while the context grows, something in your prefix is changing, or the prefix is below the provider's minimum length.
Exercise
An agent puts the current time at the top of its system prompt on every turn. The context grows each turn, yet the usage data shows zero cache-read tokens. Why, and what is the fix?
Show answer
The timestamp changes every turn, so the request differs from the previous one at the very start and no prefix can match. Move the timestamp to the end of the request, or remove it, so the system prompt and tool definitions stay identical across turns.