Knaph

Using Agents Well32 of 375 min read

The Same Start Every Time

Leila's agent session ran for 40 turns. She remembers from Paying by the Token that every turn sends the whole conversation again, so she adds it up: about 2.8 million input tokens, which at 4 dollars per million should be about 11 dollars. Her usage page says about a dollar and a half. Did she get the sum wrong?


Leila I checked my sum twice. Every turn really does send everything again.

Omid It does. Put two requests side by side: turn 39 and turn 40. What's different?

Leila Turn 40 is turn 39 with a bit added at the end: the model's last reply and the result of its last tool call. Everything before that is identical.

Omid And what happens to all those identical tokens on turn 40?

Leila The model reads them again, from the start. More than a hundred thousand tokens it already read a few seconds ago.

Omid Does it have to?

Leila It's stateless, we said. It can't remember the last request.

Omid True, the model keeps nothing. But reading a prompt means doing a lot of calculation on each token, and those results could be stored somewhere outside the model, for a little while.

Leila So the provider's computers keep the work they did on the start of my request, and if the next request starts the same way, they skip doing it again?

Omid That's prompt caching. And because skipping the work is cheaper for them, it's cheaper for you.


How caching works

When a model reads a request, it does a large amount of calculation for every token, from the first to the last. Prompt caching means the provider keeps those results for the start of a request, called the prefix, for a few minutes. If the next request begins with exactly the same tokens, that part doesn't need working out again: it's a cache hit. (A cache is a store of work kept close at hand so it doesn't have to be done twice.)

Two requests one turn apart start with the same tokens: system prompt, instruction file and the conversation so far. On the second, that shared start is read from the cache and only the new part at the end is processed in full.

Two things follow:

  • It's cheaper. Cached input tokens cost about a tenth of the normal price, and on some models even less: in late 2026, Claude's largest everyday model charges about 4 dollars per million input tokens and 0.20 dollars per million for tokens read from the cache, a twentieth. The sums in this lesson use a tenth, to keep them simple. Storing a prefix in the cache the first time costs a little more than normal input.
  • It's faster. Skipped work is time saved, so a long request with a cache hit starts answering sooner.

That's where Leila's money went. On turn 40, about 106,000 tokens came from the cache and only the last 2,000 were paid in full. Over the session, the total dropped from about 11 dollars to about 1.50.

The input cost of each turn of a 40-turn session. Without caching each bar grows as the history grows; with caching almost all of each bar is cached and costs a tenth as much.

Only an exact match

The cache works on an exact prefix. The provider compares the new request with the stored one from the very first token; at the first difference, the match stops, and everything after it is processed again at full price, however much of it is the same.

That's why harnesses build every request in the same order, with what changes least first: the system prompt and tool descriptions, then project files like CLAUDE.md and memory, then the conversation, with new turns added at the end. Adding to the end never breaks the cache. Changing something near the start does.

In Claude Code (as of 2026), these break the cache and make the next turn slower and dearer, after which the new start is cached again:

  • switching the model, or changing the effort setting, in the middle of a session;
  • connecting or removing an MCP server, in setups that load every tool description up front;
  • compacting the conversation, which rewrites it.

These don't: editing files in your project (they're tool results added at the end), using a skill (added at the end too), starting subagents (they have their own requests). Even editing CLAUDE.md mid-session doesn't break it, because the change only takes effect when you clear or compact.

It doesn't last

A stored prefix expires if nobody uses it for a while: after 5 minutes by default on the API, and after an hour for the main conversation on Claude's subscriptions. Every hit restarts the clock, so an agent working steadily stays cached all session.

But if you leave for lunch and come back to the same session, the first turn processes the whole history again at full price. That's not a reason to keep a session alive for its own sake: if you're starting a new task, When the Window Fills Up already says to clear and start fresh.

Try it

In Claude Code, after a few turns, run /usage: it shows how much of your input came from the cache. Then switch the model with /model and send one more message: that turn has to start over. If you program, the API reports cached tokens in each response's usage (cache_read_input_tokens for Claude). Or try the exercise after this lesson, which works out what caching saves.

Using it well

  1. Choose the model and effort at the start. Switching in the middle of a long session throws the cache away; if a task needs a different model, start a new session for it.
  2. Settle your setup between sessions. Add MCP servers and rewrite CLAUDE.md before you start, not halfway through.
  3. Keep working sessions steady, and end them on purpose. A session you come back to after a long break pays for its whole history once more. For a new task, clear instead.
  4. If you build your own agent, keep the start fixed. Put what never changes first and what changes (today's date, the user's message) last. One changing word at the top makes every request pay full price.

Words to know

  • prompt caching - the provider keeping the work done on the start of a request, so a later request that starts the same way is cheaper and faster
  • prefix - the start of a request, from the first token up to some point; the part a cache can match
  • cache hit - a request whose start matches a stored prefix exactly, so that part is read from the cache instead of processed again

Big idea

Every turn resends everything, but the start of each request is the same as last time, and that part is cached at a tenth of the price. Keep the start fixed and add to the end, and long sessions stay affordable.

Test your understanding

Sign in to test your understanding of this lesson and track your progress.