# Prompt caching in an agent loop: notes from taking one apart

URL: https://madeby.dev/notes/prompt-caching-agent-loop
Date: 2026-11-29
Summary: Notes from digging into prompt caching.

I'm learning how a coding agent harness works by building one from scratch. First, I put a logging proxy between Claude Code and the API, to get a glimpse of what it actually sends and how it looks under the hood. Then Claude Code and I build a small agent harness of our own. We are following a local atlas, adding features step by step. Claude Code writes the code and I make the decisions. Before each block we both make bets on the results and then we check the bets against the real requests. Prompt caching was the part I had guessed worst, so here is what turned out interesting.

Earlier I looked at where my Grok Bot setup spent its usage, and about 66% of the cost was cached re-reads of long contexts. That made me want to see caching at the request level, not just the price list.

### Claude Code moves its cache marker, and the cache doesn't care

Through the proxy, a normal Claude Code session (version 2.1.296, 23 built-in tools) had a cached prefix of about 29k tokens and a cache share of 90.1% over 12 calls. In another pair of sessions, a brand-new session read 11.5k tokens (the tools and the system prompt) that the session before it had written minutes earlier.

The odd part was the cache marker. Claude Code moves it forward on every call (message 4, then 7, then 10, and so on in the trace) and the cache reads still climbed, from 29.0k to 31.8k tokens.

![Bar chart of prompt tokens per call in a 12-call Claude Code session. After a small first side call, call 2 wrote 29.0k tokens. Cache reads then climbed from 29.0k on call 3 to 31.8k on call 12, and 90.1% of the input was read from cache.](https://madeby.dev/media/notes/prompt-caching-claude-code-cache-reads.webp)

Our own loop rebuilds the whole request from one append-only log on every turn, and checks that the new request starts with exactly the bytes of the previous one. The check is a small function:

```ts
export function checkPrefix(prev, next) {
  const { messages: pm, ...pHead } = prev;
  const { messages: nm, ...nHead } = next;
  if (JSON.stringify(pHead) !== JSON.stringify(nHead)) return { ok: false, where: 'head' };
  for (let i = 0; i < pm.length; i++)
    if (JSON.stringify(pm[i]) !== JSON.stringify(nm[i])) return { ok: false, where: `messages[${i}]` };
  return { ok: true };
}
```

My bet was that the check would pass on a real Claude Code trace and the cache would hit. The cache hit, the check failed. The moving marker changes the bytes, but the marker isn't part of the cache key. So a byte check is stricter than the cache, and you have to know which part of the request is allowed to move.

### Our own loop on Claude Haiku 5.5

For a live run of our loop, we chose Claude Haiku 5.5 with a tiny task: list a folder, read two files, multiply two numbers, answer. Four calls, with Anthropic's automatic caching on, and the same task again with it off.

![Table of four API calls on Claude Haiku 5.5 with the cache on. Call 1 listed the folder: 658 prompt tokens, no cache read, 654 cache write, $0.000123. Call 2 read two files: 761 prompt tokens, 654 cache read, 105 cache write, $0.000078. Call 3 multiplied: 957 prompt tokens, 759 cache read, 196 cache write, $0.000058. Call 4 answered: 1015 prompt tokens, 955 cache read, 58 cache write, $0.000019. The whole run cost $0.000277 with the cache on and $0.000466 with it off, 41% cheaper.](https://madeby.dev/media/notes/prompt-caching-haiku-calls.webp)

With the cache on, the run cost $0.000277. With it off, $0.000466. That is 41% cheaper. The last call read 94% of its prompt from the cache. Call 1 was about 15% more expensive with the cache on, because cache writes cost 1.25 times the normal input price, so it only pays from call 2.

I had three bets on this, and I got most of them wrong:

![Table of three bets on caching before the Haiku 5.5 run. On whether call 1 writes to the cache: I said no, Claude Code said yes, the result was yes, 654 tokens. On the cache share of the last call: I said about 60%, Claude Code said about 75%, the result was 94%. On cost with cache versus without: I said 20 to 30% cheaper, Claude Code said the same, the result was 41% cheaper.](https://madeby.dev/media/notes/prompt-caching-bets.webp)

The first one is the useful one. Our request is about 300 tokens by size, but the API counted 658 on call 1. Anthropic adds roughly 350 tokens of its own text when tools are present. That pushed the first call over Haiku's 512 token minimum for caching, so it wrote to the cache when I expected nothing. You can't count tokens from the text you send. You have to read the usage numbers.

### The same task on DeepSeek, and switching between them

The same task on DeepSeek (deepseek-v4-flash) also took 4 calls. Its cache behaves differently: it works in 64-token blocks, lags by a call and sat around 56 to 72% in my runs. On Claude each call read exactly what the previous one wrote. DeepSeek also read one file the task didn't need, every run, while Haiku always read only files that were mentioned in the task.

I also tried switching models in the middle of a conversation. DeepSeek to Claude worked. Claude's first call had no cache at all and wrote the whole 1025-token prompt, because a cache belongs to one provider and one model. Claude to DeepSeek failed with HTTP 400: the `reasoning_content` in thinking mode has to be passed back. We didn't have any reasoning from Claude's turns, and making some up didn't feel right, so that's parked.

### Byte-exact tool arguments that didn't matter

One thing the loop does is send the tool arguments back exactly as the model wrote them, instead of parsing them and calling `JSON.stringify` again:

```ts
arguments: x.rawInput ?? JSON.stringify(x.input)
```

DeepSeek writes `{"path": "x"}`, and rebuilding gives `{"path":"x"}`. My reasoning was that the provider had cached the model's own bytes, so rebuilding would miss the cache from that message on.

So I ran it both ways on DeepSeek, three runs each, with a random id at the top of each system prompt so runs couldn't share the cache. There was no difference: the same cache reads call by call, 56 to 62% of each run. DeepSeek caches what you send, not what the model wrote back, and `JSON.stringify` gives the same bytes every turn anyway. I kept the byte-exact replay because it costs nothing, but the cache reason was wrong. I didn't run this on Anthropic: it takes a parsed object, so the same trick doesn't apply.

Still not measured: the fixed cache marker at the end of the system block, and anything longer than five calls.

### Caveats

This is one tiny task, mostly run once per setup (the argument A/B ran three times a side), on one account, and every run cost a fraction of a cent. Cache behaviour changes per account, provider and model, so these are just my local observations. Numbers from a real workload could look different.
