Coding-Agent Cost per Session: Caching, Routing, Context Editing and Compaction Ranked on 150k Sessions
A coding-agent session that grows to 150,000 tokens costs about $11 on Claude Sonnet 5.5 without prompt caching and $1.77 with it. What comes next depends on which levers break the cache.
A coding-agent session that grows to 150,000 tokens over 60 turns costs about $11.10 on Claude Sonnet 5.5 at Anthropic's list prices if nothing is cached, and about $1.77 if prompt caching hits on every turn. That one change cuts cost per session by 84 percent, because almost all of the bill is the same context re-sent as input on every turn. After caching, the biggest remaining costs are output tokens, idle gaps that let the cache expire, and session length, so the next levers are routing exploration to small-model subagents, trimming output and tool results, and keeping sessions short. Context clearing and compaction come last: each one forces a cache rewrite and pays back only in long sessions.
Those figures are a worked model built on list prices, not traces from a production agent. The assumptions are spelled out below so a team can swap in its own numbers. They also assume "150k-token session" means a context that grows to 150,000 tokens. If it means 150,000 billed tokens in total, a Sonnet 5.5 session costs under a dollar and the problem lies elsewhere, so check which one your logs show first.
Why a session costs what it does
A coding agent has no memory between API calls. Each turn sends the whole conversation again: system prompt, tool definitions, every file it has read, every command output, every earlier answer, plus the newest step. Claude Code's documentation describes its own loop this way: "Claude Code re-sends the full context" on every request, and new content is appended at the end.
That makes cost grow faster than session length. Take a session that starts at 20,000 tokens (system prompt, tools and task) and grows by about 2,200 tokens a turn to 150,000 by turn 60, with 1,500 output tokens a turn, thinking included. Over the session the model reads 5.1 million input tokens but writes only 90,000 output tokens. Without caching, Sonnet 5.5 bills those 5.1 million tokens at $2 per million: $10.20 of the $11.10 total is re-sent input.
Prompt caching changes the price of the repeated part. Anthropic's pricing page lists a cache read on Sonnet 5.5 at $0.10 per million tokens, one-twentieth of the $2 input price (Opus 5.5 and Sonnet 5.5 cache hits are priced at 0.05x base input; most other Claude models use 0.1x). Writing to the 5-minute cache costs 1.25x input, $2.50 on Sonnet 5.5; the 1-hour cache costs 2x, $4. When caching works, each turn reads the previous turn's context from cache and writes only the new 2,200 tokens.
| Model (60 turns, 20k to 150k) | No caching | Caching hits every turn | Sessions per $40 (cached) |
|---|---|---|---|
| Claude Fable 5.2 | $44.40 | $6.09 | 6.6 |
| Claude Opus 5.5 | $22.20 | $3.54 | 11.3 |
| Claude Sonnet 5.5 | $11.10 | $1.77 | 22.6 |
| Claude Haiku 5.5 | $1.78 | $0.32 | 124 |
On Sonnet 5.5 with caching, the $1.77 splits into $0.50 of cache reads, $0.38 of cache writes and $0.90 of output. Output is now half the bill. Haiku 5.5 looks cheap here, but it has a catch covered below: any request whose prompt exceeds 100,000 tokens pays five times the price for the whole request, so 23 of these 60 turns are billed at the higher rate.
The table also explains the complaint behind the question. If a $40-a-month plan runs on Sonnet 5.5 with no caching, three or four long sessions use it up. Even with caching working perfectly, 22 sessions use it up, which a power user running ten a day reaches in two days. Caching is necessary, but it is not enough on its own.
The one rule that governs every other lever
Anthropic's caching works by exact prefix match. Claude Code's prompt-caching guide puts it plainly: "a change anywhere in the prefix recomputes everything after it. There is no per-file or per-segment caching." And on models: "Each model has its own cache. Switching models recomputes the entire request even when the content is identical."
So every lever that edits earlier context or changes model throws away the cache and pays to write the whole remaining context again. On Sonnet 5.5 a write costs 25 times a read ($2.50 against $0.10), which is why a cache miss is so expensive relative to a hit. The penalty for one miss, over what a hit would have cost, is:
- Sonnet 5.5: $0.12 at 50,000 tokens of context, $0.24 at 100,000, $0.36 at 150,000.
- Opus 5.5: $0.24, $0.48 and $0.72.
A single miss at 150,000 tokens on Sonnet 5.5 costs about as much as ten ordinary cached turns. That number is the price of switching models, clearing tool results, compacting, or a user who goes for coffee.
The levers, ranked
Each figure below is the change in modelled cost per session against the cached Sonnet 5.5 baseline of $1.77, except the first row, which is measured against no caching.
| Lever | Session cost | Saving | Touches the cache? |
|---|---|---|---|
| Prompt caching that hits | $11.10 to $1.77 | 84% | Is the cache |
| 1-hour TTL when users idle (six gaps) | $3.11 to $2.00 | 36% | Avoids misses |
| Exploration on Haiku 5.5 subagents | $1.77 to $1.17 | 34% | No (separate conversation) |
| Output cut from 1,500 to 1,000 per turn | $1.77 to $1.47 | 17% | No |
| Tool-output caps (30% less growth) | $1.77 to $1.56 | 12% | No |
| Compaction or clearing at 100k | $1.77 to $1.64-1.68 | 5-7% | Yes, one rewrite |
Keep the cache warm. The 5-minute cache is cheaper per write but expires, and Anthropic's docs note the lifetime is measured from the start of the request, so a response that takes four minutes to stream leaves about a minute for the next one. A user who pauses six times in a session past five minutes turns a $1.77 session into $3.11. The 1-hour cache costs $0.225 more in writes across this session, the same as one miss at about 94,000 tokens. If your users read diffs, run tests by hand or answer messages between turns, the 1-hour TTL pays for itself with a single long pause once the context is past about 94,000 tokens. Claude Code itself defaults to the 1-hour cache only for subscribers within plan usage, and to five minutes on API keys, so an agent built on the API has to ask for it.
Route exploration to subagents, not the main loop. Routing is where the cache rule bites hardest. Switching the main conversation from Sonnet 5.5 to Haiku 5.5 at 150,000 tokens means Haiku writes the whole context at its over-100k rate, and switching back after Sonnet's cache has expired costs another $0.36 rewrite. Haiku has to run about 19 turns before that round trip pays off at 150,000 tokens, about 8 turns at 100,000 and 5 at 50,000. If you return to Sonnet within its cache lifetime, the break-even falls to a few turns, but a mid-session switch remains a gamble on timing.
A subagent avoids the problem. It runs in its own conversation with its own short context, and Claude Code's docs say the parent's cache is unaffected because the subagent's call and result simply append to the parent conversation. Anthropic's context-engineering guide describes subagents that return "a condensed, distilled summary of its work (often 1,000-2,000 tokens)." In the model, 20 of 60 turns are exploration (searching, reading files, reading logs) and move to four Haiku 5.5 subagents of five turns each, each returning 2,000 tokens. The subagents cost $0.025 in total. The main loop runs 40 turns instead of 60 and peaks near 114,000 tokens instead of 150,000. The session falls to $1.17.
A cheaper alternative for some turns is lowering effort instead of switching model. Thinking is billed as output, and Claude Code's docs say that on Opus 5.5, Sonnet 5.5 and Haiku 5.5, changing effort keeps the cache when calling with an API key. Anthropic's API docs are more cautious, saying effort changes "can invalidate message blocks," so check the cache fields after the first change.
Cut output. Once input is cached, output is the largest line. Going from 1,500 to 1,000 output tokens a turn saves $0.30 a session, 17 percent. The practical ways are lower effort on routine turns, edits as diffs or string replacements rather than whole-file rewrites, and short commentary between tool calls.
Cap tool output at the source. Every token a tool returns is re-read on every later turn. Claude Code's cost guide gives the pattern: a hook that greps a 10,000-line log for ERROR and passes on only the matches, "reducing context from tens of thousands of tokens to hundreds," and a sample hook that keeps the last 100 lines of test output. Cutting context growth by 30 percent saves 12 percent a session in the model; cutting it by half saves 20 percent. For agents with many integrations, deferred tool loading belongs here too. Anthropic's engineering team measured a five-server MCP setup at about 55,000 tokens of tool definitions, and its Tool Search Tool loads about 500 tokens upfront and the rest on demand. Its post says deferred loading "doesn't break prompt caching because deferred tools are excluded from the initial prompt entirely." Carrying 55,000 tokens of definitions on every turn makes the model session $2.23 instead of $1.77.
Clear and compact last. Anthropic's context-editing docs say tool-result clearing (clear_tool_uses_20250919) "invalidates cached prompt prefixes when content is cleared," and advise setting clear_at_least so each rewrite removes enough to be worth it. Server-side compaction (compact_20260112, default trigger 150,000 tokens, minimum 50,000) replaces history with a summary, and the docs note it "requires an additional sampling step, which contributes to rate limits and billing." With a cache breakpoint at the end of the system prompt, the docs say the system prompt stays cached and only the summary is written fresh.
In a 60-turn session these barely pay: clearing 60,000 tokens at 100,000 saves 5 percent, compacting at 100,000 saves 7 percent. The break-even explains why. Clearing 60,000 tokens at 100,000 costs about $0.04 more on the next turn than a cached turn would, and saves $0.006 on every turn after, so it pays back after about seven turns. Clearing 25,000 at 50,000 pays back in about four, and clearing 100,000 at 150,000 in about six. The saving grows with length: on a 120-turn session that would otherwise reach 282,000 tokens, compaction at 100,000 saves 22 percent and clearing saves 19 percent. Anthropic's compaction overview gives a second reason to use it: response quality degrades as a conversation grows.
On Haiku 5.5 the logic flips. Keeping every request under the 100,000-token threshold avoids the five-times price on the whole request, so compaction at 95,000 cuts a cached Haiku session from $0.32 to about $0.10. Whether Haiku can carry a whole coding session at acceptable quality is a separate question nobody has published an answer to.
How the levers combine
Savings do not add up. Caching plus Haiku subagents plus 30 percent tool-output caps plus 1,000-token output brings the session to $0.84, a 52 percent cut from $1.77 and 47 sessions per $40. Adding the individual savings would suggest 63 percent. Each lever shrinks the base the next one works on: subagents remove turns, so output cuts apply to fewer of them, and smaller contexts leave less for caps to remove.
The interference that matters most is between the cache and anything that rewrites it. A sound order is to make caching reliable first, then apply the levers that never touch the main prefix (subagents, output limits, tool caps, deferred tools), and only then use the ones that force a rewrite (clearing, compaction, model switches), timed so the rewrite happens once and early. One useful pairing: if a budget rule moves a user to a cheaper model, do it right after a compaction, when the context is a short summary and the rewrite is small.
Anthropic's documentation describes no way to carry one model's cache to another. Parsec, a small language model lab, says on its routers page that its custom-model routing service uses a universal K/V cache, so that a switch mid-conversation costs 15 to 19 percent of a full re-read. That claim has not been independently verified, and the service has no published price. Separately, Parsec sells a context layer for Claude Code, Codex and opencode that trims unused tool output and thinking, free to start according to its site. In Parsec's own benchmark of 100 SWE-bench Verified tasks on Claude Sonnet 4.6, the run with the layer cost $89.65 against $147.30 without it. The code and per-task results are public and the benchmark can be re-run, but no independent re-run has been published.
Finding the misses you are already paying for
A cache that silently misses looks like one that works until you read the usage fields. Anthropic's API returns three input fields on every response: cache_read_input_tokens, cache_creation_input_tokens and input_tokens (only the tokens after the last breakpoint), plus output_tokens and a cache_creation split between 5-minute and 1-hour writes. Log all of them per turn, along with model, effort and a timestamp. With compaction on, sum usage.iterations, because the top-level fields leave out the compaction step. With clearing on, log cleared_input_tokens from context_management.applied_edits. Claude Code's guide states the test: "A high read-to-creation ratio means caching is working well. If creation stays high turn after turn, something is changing in your prefix."
The usual causes, all documented by Anthropic:
- A timestamp or per-request state in a block that carries the cache breakpoint. The docs call this a common mistake: "No cache hit. You pay for a fresh cache write on every request."
- Tool definitions that change between calls. Changing them "invalidates the entire cache"; so do changes to
tool_choice, images, and thinking settings for the message layer. - Edited or reordered history. Any change to an earlier message recomputes everything after it.
- Idle gaps longer than the TTL, counted from the start of the last request.
- A breakpoint pushed more than 20 blocks past the last write, outside the lookback window, or a gateway that strips
cache_controlmarkers, which bills the whole history as uncached input on every turn.
The long tail
Cost per session grows faster than length. In the model, a 45-turn Sonnet 5.5 session costs $1.26, a 120-turn one $4.29 and a 240-turn one $11.71: about five times the turns of the 45-turn session for more than nine times the cost. Anthropic's Claude Code cost guide shows how uneven usage is even inside enterprises: about $13 per developer per active day on average, with 90 percent of users below $30, and it traces unexpectedly high spend to "long sessions that were never cleared or to Opus left as the default model." What share of a coding product's spend comes from its top 5 percent of sessions has not been measured in any published primary source.
The guardrails follow from the cost curve: a per-session budget (Claude Code exposes one as --max-budget-usd) that moves the session to a cheaper model at a compaction point rather than cutting the user off; loop detection on repeated failing commands; turn and retry caps; and a prompt to start a new session when a task is done. Their effect on task completion rates has not been measured.
Method and calculator
The model assumes 60 turns, context growing linearly from 20,000 to 150,000 tokens, 1,500 output tokens per turn, Anthropic list prices, a 5-minute TTL and perfect hits unless a miss is stated. Haiku 5.5 is priced per request at the over-100k rate when that request's prompt exceeds 100,000 tokens. Token counts are each model's own tokens; Anthropic says Claude 4.7 and later models produce about 30 percent more tokens for the same text than Sonnet 4.6 and earlier. The core calculation fits in a few lines:
# rates in $ per million tokens: (input, cache_write, cache_read, output)
SONNET_55 = (2.00, 2.50, 0.10, 10.00)
OPUS_55 = (4.00, 5.00, 0.20, 20.00)
def session_cost(rates, turns=60, start=20_000, end=150_000,
out_per_turn=1_500, cache=True, misses=()):
inp, write, read, out = rates
growth = (end - start) / (turns - 1)
cost, cached, ctx = 0.0, 0, start
for t in range(turns):
if not cache:
cost += ctx * inp
else:
hit = 0 if t in misses else cached
cost += hit * read + (ctx - hit) * write
cached = ctx
cost += out_per_turn * out
ctx += growth
return cost / 1e6
print(session_cost(SONNET_55)) # 1.77
print(session_cost(SONNET_55, cache=False)) # 11.10
Replace the turn count, growth and output with your own logged medians and the rates with your contract prices. The model ignores quality: it assumes every lever leaves the task outcome unchanged, which only your own evaluations can confirm.
Bottom line
If your agent does not cache, or caches with a hit rate you have not checked, fix that first: it is the difference between $11 and $1.77 a session on Sonnet 5.5. If your users pause between turns, switch to the 1-hour TTL. If sessions still outrun the plan, move exploration to small-model subagents and cut output and tool results, because none of these touches the main cache. Use clearing and compaction for sessions that run past 100 turns, and on Haiku 5.5 to stay under 100,000 tokens. Avoid switching the main loop's model mid-session unless the switch comes right after a compaction or will last many turns.
Sources
- Anthropic, Pricing: https://platform.claude.com/docs/en/about-claude/pricing
- Anthropic, Prompt caching: https://platform.claude.com/docs/en/build-with-claude/prompt-caching
- Anthropic, Context editing: https://platform.claude.com/docs/en/build-with-claude/context-editing
- Anthropic, Compaction overview: https://platform.claude.com/docs/en/build-with-claude/compaction
- Anthropic, Compaction at a token threshold: https://platform.claude.com/docs/en/build-with-claude/compaction-threshold
- Anthropic, How Claude Code uses prompt caching: https://code.claude.com/docs/en/prompt-caching
- Anthropic, Claude Code: Manage costs effectively: https://code.claude.com/docs/en/costs
- Anthropic Engineering, Introducing advanced tool use (Nov. 24, 2025): https://www.anthropic.com/engineering/advanced-tool-use
- Anthropic Engineering, Effective context engineering for AI agents (Sept. 29, 2025): https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents
- Parsec, Parsec vs routers: https://parseclab.ai/compare/routers/
- Parsec, Parsec for coding agents: https://getparsec.ai/
- Parsec's public benchmark, code-compression-bench: https://github.com/daseinlabs/code-compression-bench