Coding Agent Cost: Why Sessions Get Expensive
Keep the project on disk, treat lunch as a bill, and score the first finished tasks rather than dollars per million tokens
Takeaways
- API models are stateless, so every turn re-sends the whole thread: a one-word "hi" after a 1 million token session bills as 1 million input tokens, $1 at the teaching rate of $1 per 1M, and $5 on Opus 5 when the stored copy is gone versus $0.50 on a hit.
- The default cache lifetime is 5 minutes, so coming back from lunch re-bills the entire prefix at full input price (200,000 uncached Opus 5 tokens cost $1.00 versus $0.10 on a hit), while holding the copy costs 1.25x base input for 5 minutes and 2x for 1 hour.
- Cache-hit discounts are provider-specific: Anthropic 0.1x, the OpenRouter agent blend about 1/5, Grok 4.6 at 0.25x, DeepSeek V4-Flash off-peak about 0.03x, and a16z puts more than 85% of agentic OpenRouter tokens on cached prompt (OpenRouter's head of insights: about 70% of July sessions).
- Cheap list price lies twice: one mid-session model switch cost 5.1x to 11.1x a warm turn because prompt cache is model-specific, and the fair scoreboard is the first two or three finished tasks, not Artificial Analysis's $0.84 per Intelligence Index task for Grok 4.6 that drifted to $0.94 (about 12%) in six days.
- Claude Code and Codex compact around 200,000 to 300,000 tokens, not the advertised 1 million, so the project has to live on disk as a byte-stable prefix (
ARCHITECTURE.md, skills, a stable system prompt), because aCLAUDE.mdthat injectsgit statusor a clock is a miss engine.
You left a long coding-agent thread over lunch.
Then the stored copy of that history is already gone which was cheap, so the first message you send when you come back re-sends every prior turn at full input price, which is why you run coding work as session, task, and project, and why walking away is a bill.
The models you call via API are stateless and do not keep the conversation in memory between calls, so every turn re-sends the whole thread, and a one-token tool result still pays that whole history.
Arena AI's recent lecture "The Real Cost of Agentic AI: Tokens, Caching & Context Costs Explained" is the 26-minute walkthrough of that bill.
It walks through:
- Why the model re-sends the whole thread every turn,
- Why the bill still grows even when most of that thread is stored cheaply (cached),
- Why walking away can make one message cost more than the afternoon (cache-miss),
- How coding apps shorten the thread when it gets huge (compaction),
- Why the first finished jobs beat dollars per million tokens.
The worked example is a one-word "hi" after a 1 million token session. So if the model costs $1 per 1M input tokens, that "hi" is priced at $1.
Opus 5 bills that same input million at $5 when the stored copy is gone, and $0.50 when the provider still has it, on Anthropic's pricing page.
Pin the model and the provider so that stored copy stays cheap to re-read, because the project does not live in the thread.
What follows is the session lifetime and the lunch bill, how to score a finished task, where the project lives, four cache prices, and how to mix cheap models without paying twice.
What happens when you leave a long agent session
Coming back after the cheap stored copy dies bills the whole prefix at full input price. The session is scratch paper with a lifetime, so pin the model and the provider.
Providers store a cheap copy of the prefix, the start of the prompt that is everything already in the thread, for a short window.
A hit is a later turn that still finds that copy. A miss is a later turn that does not find it, so you pay the full input price to send the whole history again.
If you pinned Opus 5 on Anthropic in the morning and started a failing test, then left for lunch, the default 5-minute cache is already dead when you sit back down.
Why a cache miss can cost more than an afternoon of hits
Miss spikes can beat the rest of an afternoon of cheap hits. Those dollars are arithmetic on Anthropic's pricing page, not a session I sat through.
On that page, 200,000 uncached Opus 5 input tokens are $1.00 and a hit is $0.10. One million uncached is $5.00, and a hit is $0.50.
By default, Anthropic keeps the cache for five minutes and drops it if you do not use it. Storing a copy with a 5-minute life costs 1.25x the base input price, and a 1-hour life costs 2x.
I ran these tests in Model Routing Layers and the Prompt Cache Tax: 12-turn coding sessions through OpenRouter on the same synthetic prefix, and the cache died mid-run.
Grok 4.5 (not 4.6), at a 30,000-token prefix, paid a full-price re-read twice (turns 2 and 10) after the 5-minute cache expired, which is shorter than lunch. The expensive turn is the first one after you walk back in.
Cache is not 10% on every provider
Cache-hit discounts are provider-specific. Anthropic's 0.1x read is not a number you can paste onto OpenRouter, Grok, or DeepSeek.
Sources: Anthropic pricing page · OpenRouter's head of insights · Artificial Analysis · DeepSeek pricing page
a16z says more than 85% of agentic OpenRouter tokens are cached prompt. OpenRouter's head of insights put all July sessions at about 70%.
Cache hits still need the start of the prompt to be the exact same bytes as last time. A timestamp in the system prompt, a git status dump every turn, a shuffled tool list, or a model or provider switch makes it a different prompt, so you pay full price.
Compact before you leave, or pay to hold the cache
When the thread gets huge, Claude Code and Codex drop or summarize old turns so the next request is smaller. That is compaction, and it cuts the future bill.
The first turn after compaction is a miss, because the new shorter prefix is not the copy the provider stored.
So compact before you leave, and the miss is small, though you still pay one miss. Buy the 1-hour write, and a return can be a hit, though you paid 2x to store the copy.
Claude Code and Codex often compact around 200,000 to 300,000 tokens, not the advertised 1 million (That is how those apps behave in practice, not according to Anthropic or OpenAI docs).
Each turn re-sends everything so far, so the bill is the area under a rising line, the cost triangle. Filling the million on purpose is how you buy the expensive part of it.
"Two triangles: a growing turn-by-turn cost triangle, and a task triangle where task 1, task 2, and task 3 sit on a shared prefix"
But the usual advice is to compact at 90% of the window. I'd gate that advice on the miss, which is the spike.
Compaction is itself a miss, so compacting when you return still buys the expensive turn.
Ninety percent of a 1-million-token window is not when those coding apps compact. And the decision that made the session worth returning to has to live on disk, or compaction throws it away.
How to score a coding task, not a token bill
Score the first two or three finished tasks on your setup, not dollars per million tokens. A task is one job with a done condition, larger than a turn, and you mix cheap open-source volume with frontier only while the session cache is still warm.
Say the morning failing test is task 1, and the next ticket in the same thread is task 2. If you bounce to a cheap OpenRouter quote after the pin is dead, you buy the prefix again.
Shop the first two or three finished tasks
Turns add lines to the cost triangle, so the fair scoreboard is the first two or three finished tasks. A model that finishes in 2 turns can beat a cheaper model that takes 10, even when its list price is higher.
The dangerous model is the almost-capable loop. Extra thinking gets fed back in and repaid as input until you compact.
Price those first two or three tasks so a model you actually come back to is not punished for retention. A week-long thread total is not a quality metric.
But Artificial Analysis billed Grok 4.6 at $0.84 per Intelligence Index task when ARK wrote on 21 Aug. The same page was $0.94 on 27 Aug, about 12% in six days, a different object from first-three-tasks on a live coding harness and a bad coding-PR budget.
Mix cheap models only while the cache stays warm
Cheaper list price still loses if the switch kills the prefix. In the tests I ran earlier, one mid-session model change cost 5.1x to 11.1x a warm turn, because prompt cache is model-specific (often provider-specific) and the next model re-reads the prefix at full freight.
That run used a synthetic code prefix, Grok 4.5, and single un-repeated sessions at 30k and 60k.
Cheap output did not win on that run. Grok 4.5 came in 0.2% and 7.9% under never-routing in the two clean runs, while Sonnet 5 and Kimi K3 cost 16% to 47% more.
Same pattern on a cheap quote: Robot Ventures, in The Souq on GLM-5.2 via OpenRouter, needed about 5 repeats against an aggressive repricer before the user paid more than the builder API. Pin a provider that actually caches, then argue list price.
NVIDIA's same-family cache mapper (August 2026) is research on matched caches inside one family, not a way to prefill cheap and decode on Claude, Grok, or GPT this week.
Where should project context live?
Project context lives on disk, as a byte-stable prefix the agent re-reads after compaction: repo files, skills, architecture notes, a stable system prompt, upload-once file ids. Compaction is a cost control only if the thing you still need is not sitting in the discarded middle of the thread.
That prefix survives compaction and is re-read as the same bytes. Tool logs, failed attempts, and "try this" detours in the thread do not.
For eg. ARCHITECTURE.md may be re-injected (re-read) after compaction. If after compaction the agent has to explore the repo from scratch, as if it has never seen that ARCHITECTURE.md, the setup failed and all you really had was a chat transcript, not a durable project setup.
Most of the token pile is already the stored prefix, so that pile should be the project, not a log of tool calls.
Byte-stable also means the prefix cannot rewrite itself every turn. A CLAUDE.md that injects git status or a clock is a miss engine.
Put the failing-test invariants on disk before you compact. Afternoon compaction is then a smaller miss, because the files are still there.
DeepSeek's Files API is the image version of that upload-once idea: you upload, pass a file_id, and reference the same bytes again. Docs cap a file at 64 MiB and a user at 25 GiB, and image tokens still bill when the file is used.
Use that as the pattern for images. Text project files still live in your repo.
Two tasks in one session still share a prefix. That prefix should be the project.
What to change this week
Label one real week, then Promote, Hold, or Reject.
I'd run long sessions when the project is on disk and the cache is warm. I'd reject one giant thread for the repo, and shopping by dollars per million tokens or a lab Index $/task.
So treat a return from lunch as a bill. Treat a cheap OpenRouter quote as untrusted until repeats show cached tokens.
Always-frontier is still true when the job is one hard step. It is false when the job is a long tool loop.
Gavin Baker's mix still includes computationally efficient humans, cheaper open-source tokens, and frontier tokens. If you cannot say what "done" is, you cannot tell a cheap loop from a stuck one.
Pick one:
- Reject if the repo's invariants exist only inside a thread.
- Hold if they are on disk but there is no idle policy, or no provider pin.
- Promote if the prefix is byte-stable and you can state pin plus idle policy.
Then one next change.
Monday:
- Move architecture, skills, and invariants into files the agent reads as a stable prefix.
- Pin model and provider for the session.
- Compact or buy 1-hour cache before lunch if the prefix is large.
- Price your last three finished tasks. Do not compare them to Artificial Analysis's Index $/task.
That million-token "hi" after lunch is $5 if the cache is dead, and $0.50 if it hits. The files you wrote before you left are what make the cheap number available after you compact.
Questions?
Why does a short tool call still cost so much in a coding agent?
The model does not keep the conversation in memory between calls, so every turn re-sends prompts, tool logs, and assistant text. A tool result the size of one token still repays the whole prefix. Cache changes the slope of that bill, not the shape.
How much does a cache miss cost after you leave a long Claude session?
On Anthropic Opus 5, 1 million uncached input tokens are $5 and a hit is $0.50. Two hundred thousand uncached is $1.00 versus $0.10 on a hit. The $1 "hi" is a one-word message after a 1 million token session, priced at a teaching rate of $1 per million, not Opus list.
Should I shop coding models by dollars per million tokens or by cost per task?
Shop the cost of the first two or three finished tasks on your harness. List price misses turn count, cache hits, and extra thinking repaid as input. Artificial Analysis's $0.84 then $0.94 per Intelligence Index task is a Pareto hint that moved about 12% in six days, not a coding-PR budget.
Why do Claude Code and Codex compact before 1 million tokens?
Those apps often compact around 200,000 to 300,000 tokens, because filling the advertised 1 million window and walking away re-sends the whole history at full price. That is not a quote from Anthropic or OpenAI product docs.
Can I switch to a cheaper model mid-session without killing the prompt cache?
Usually you pay a checkout tax instead. One switch cost 5.1x a warm turn at 10,000 tokens and 11.1x on Sonnet 5 at 60,000. NVIDIA's mapper is same-family research, not a Claude, Grok, or GPT feature this week.