Caching is enabled, a forty-turn session answers correctly, the logs look normal, and the invoice is five times what the token counter suggested. In the loop priced below, those forty turns cost $0.43 with a stable prefix and $2.67 with a prefix that never hits.
The difference is one line of documentation that the pricing page compresses away: a cache hit needs the same prefix, byte for byte, up to the point where you marked it. When part of that prefix changes, you pay the write price, which is higher than the normal input price, and you pay it again on the next turn.
Key takeaways
- A cache hit requires an identical prefix up to your breakpoint, and a change at any level invalidates that level and everything after it.
- A prefix that changes every turn is worse than no caching: you pay the cache write price, which is above the normal input price, and never collect the read discount.
- On the same forty-turn loop, a stable prefix cost $0.43, no caching cost $2.16, and a prefix that changed every turn cost $2.67.
- The transcript, not the system prompt, is where the money goes: it was 76% of the input tokens over forty turns.
- Total input cost grows roughly with the square of the turn count, so doubling the loop length multiplies the bill by about three to four.
What a hit actually requires
The first condition is length. A prefix needs a minimum to be cacheable: 512 tokens on the newer Claude models, 1,024 tokens for GPT-5.6 and later, and a figure that varies by model and request settings for older ones (Anthropic, OpenAI). Hidden system content does not count toward that minimum. On OpenAI's supported models caching is on by default, so the question is never whether it is enabled.
The second is identity. The cache is a hash of the prefix, so a single changed byte produces a different entry. This breaks in production because the things added to prompts for observability are the things that change: a request id, a timestamp, the user's name, a shuffled list of tools, a retrieval result inserted above the conversation.
The third is where you put the marker. Anthropic's documentation describes the cache as a hierarchy, tools then system then messages. A change at one level invalidates that level and every level after it, so a timestamp in the tools array invalidates the system prompt and the entire message history.
One line in the wrong place
The usual version of this bug looks harmless in review:
{ "tools": [...], "system": "You are a support agent. Session started 2026-11-03T09:41:22Z." }
The date is there for debugging. To the cache, it makes the prompt new every time the clock ticks: every turn rewrites the full prefix at the write premium and reads nothing back.
Why the timestamp feels like a good idea
Telling the model what time it is seems obviously useful. It is a fact the model cannot otherwise know, and an agent that does not know the date makes quiet mistakes about deadlines, schedules, and anything it is asked about "today". The cost is not obvious until you see where the line sits.
Current time: 2026-11-04T01:17:00Z in the system prompt changes on every request. The hash of the prefix changes with it, so nothing below it can be reused, and the write premium applies to the whole prompt again.
On a router the damage is larger. OpenRouter tracks sticky routing per conversation so the next request reaches the provider that still has your prefix cached, and by default it identifies the conversation by hashing the first system message and the first user message. A system prompt that changes every minute changes that key too, so the conversation moves to a different provider, where nothing is cached. Sticky sessions also expire after 10 minutes of inactivity (OpenRouter).
Two fixes keep the feature. Put the time in the last user message instead of the system prompt, or round it to the day if the agent only needs the date.
The arithmetic, with the assumptions written down
Take a loop with a 6,000-token static block of system prompt and tool definitions, 1,000 tokens appended per turn, 300 output tokens per turn, and forty turns. Price it at $2 per million input tokens, $10 per million output tokens, a cache write at 1.25x the input rate, and a cache read at 0.1x. The multipliers are similar across both providers above, and the absolute rates are not.
| Scenario | Input tokens sent | Cost |
|---|---|---|
| No caching | 1,020,000 | $2.16 |
| Stable prefix, cache hits | 45,000 written, 975,000 read | $0.43 |
| Prefix changes every turn | 1,020,000, all written | $2.67 |
A prefix that never hits is 24% more expensive than turning caching off and 6 times more expensive than getting it right, for the same answers. The write price sits above the input price, so the feature adds a surcharge to every request.
OpenAI charges 1.25x for the default 30-minute lifetime and 2.5x for a 12-hour minimum. Anthropic's table for a current Sonnet lists $2.00 per million input against $2.50 for a five-minute cache write and $4.00 for a one-hour write.
The curve is quadratic
The accumulated cost matters more than the per-turn cost, because the prompt grows: turn 40 in this model sends 45,000 input tokens, against 6,000 on turn 1.
| Turns | No caching | Stable prefix |
|---|---|---|
| 10 | $0.24 | $0.09 |
| 20 | $0.68 | $0.18 |
| 40 | $2.16 | $0.43 |
| 80 | $7.52 | $1.16 |
Each doubling multiplies the no-caching bill by about 2.8, then 3.2, then 3.5, heading toward 4 as the transcript dominates. Caching divides the constant, roughly by five here, and leaves the exponent alone.
That makes trimming the system prompt the wrong lever once the loop is long. Over those forty turns the static block was 24% of the input tokens and the growing transcript was 76%. Making the system prompt 20% shorter saves about a twentieth of the bill, and ending the loop four turns earlier saves considerably more.
The expiry nobody plans for
Caches expire. The default lifetime is five minutes on Anthropic and thirty minutes on OpenAI. A reader who thinks for ten minutes before a follow-up comes back to a cold cache. That turn rewrites the entire prefix at 1.25x. If your interface invites people to pause, the pause is a cost.
What caching does not buy
OpenAI's documentation notes that cached input tokens still count toward tokens-per-minute limits, so the win is money, not headroom. If the loop hits a rate limit, a better cache will not move it, and a shorter transcript will.
Measure your own loop
The tables above are arithmetic on stated assumptions. Change the numbers to yours and run it.
# loop_cost.py: edit the six numbers, then run python3 loop_cost.py
S, A, OUT, N = 6000, 1000, 300, 40 # static prefix, tokens per turn, output per turn, turns
IN_RATE, OUT_RATE = 2.0 / 1e6, 10.0 / 1e6
WRITE, READ = 1.25, 0.10
def no_cache():
tokens = sum(S + A * (k - 1) for k in range(1, N + 1))
return tokens * IN_RATE + N * OUT * OUT_RATE
def hits():
cost = S * IN_RATE * WRITE # first turn writes the prefix
for k in range(2, N + 1):
cost += (S + A * (k - 2)) * IN_RATE * READ # read what was written last turn
cost += A * IN_RATE * WRITE # write the new segment
return cost + N * OUT * OUT_RATE
def never_hits():
tokens = sum(S + A * (k - 1) for k in range(1, N + 1))
return tokens * IN_RATE * WRITE + N * OUT * OUT_RATE # writes only, no reads
print(f"no caching ${no_cache():.2f}")
print(f"cache hits ${hits():.2f}")
print(f"never hits ${never_hits():.2f}")
Then replace the model with the numbers from the provider's response. The cached token count and the cache write count are reported per request, so the hit fraction is a measurement.
What this does not cover
The multipliers come from two providers' documentation in November 2026 and differ by model, so replace the rates with yours. Output-side costs, reasoning tokens, batch discounts, and the interaction between caching and streaming are outside this model.
Order the prompt by how often each part changes, put the marker at the end of the stable part, and measure the cache hit rate first. If it is near zero and the prompt looks static, look for a timestamp in the tools array or the first line of the system prompt.