Why is Claude Code so expensive? The mechanics, and what we measured
TL;DR: An agent re-reads its entire conversation on every turn, so your cost compounds instead of adding up. A thirty-turn session is not thirty times the cost of one turn, it is closer to the sum of the numbers one to thirty. That is a property of how agents work, not a sign you are using it wrong. We also audited six open-source coding agents to see whether agent code is inherently wasteful. It is not: one of them scored our top grade.
Why is Claude Code so expensive?
Because the cost is quadratic in the length of the session, not linear.
Each time you send a message, the model has no memory of the previous turn. The whole conversation gets sent again as input: your earlier messages, the assistant's replies, every file it read, every tool result it saw. Turn one sends one unit of context. Turn two sends two. Turn thirty sends thirty.
Add those up and a thirty-turn session costs roughly 465 units of input, not 30. The work you feel yourself doing grows in a straight line. The bill grows like a triangle.
This is why the cost feels untethered from effort. You asked one more small question and it cost more than the first ten combined.
Why does the cost grow as the session goes on?
Three things stack on top of the re-read:
Tool definitions ride along. Every tool the agent can call is described to the model on each request, as a JSON schema. If twenty tools are attached and the turn uses one, you still paid for twenty descriptions. Attach an MCP server with a large tool surface and that fixed cost lands on every single turn for the rest of the session.
Tool results are permanent. When the agent reads a 600-line file, that file does not get summarised and discarded. It sits in the conversation and is re-sent on every subsequent turn. One careless cat of a large log is not a one-off cost, it is a tax on the remainder of the session.
Cache writes cost more than cache reads. Prompt caching is the main defence here, and it is genuinely effective: on the pricing table erabot ships, an Anthropic cached read costs about a tenth of a normal input token. But writing to the cache costs more than not caching at all, around 1.25 times the input price for the short time-to-live. So a cached prefix that gets reused many times is a large saving, and one that gets written and then invalidated is a small loss. Anything changing near the start of your context, a timestamp or a session id, invalidates everything after it.
Those figures come from the pricing table in our own repo, last verified 2026-07-21. Provider prices move, so treat the ratios as the durable part and see our LLM API pricing reference for current numbers.
Is Claude Code expensive compared to calling the API directly?
Per token, no. It is the same models at the same prices. The difference is how many tokens a turn consumes.
A direct API call sends what you decide to send. An agent decides for itself: which files to open, how many tools to offer, how much history to carry. That autonomy is the product. It is also the reason the token count is hard to predict ahead of time.
So the honest comparison is not "agent versus API". It is "a system that chooses its own context versus one where you choose it".
Does being an agent make a codebase wasteful?
This is where we can stop reasoning and look at measurements.
We ran erabot's audit over 15 open-source AI products, 32,672 source files in total. 435 of those files contained LLM calls, holding 1,086 call sites between them. Six of the 15 are coding agents or agent frameworks, and they did not cluster at the bottom:
| Agent | Language | LLM call sites found | Cost grade |
|---|---|---|---|
| Aider | Python | 5 | A+ |
| OpenHands | Python | 6 | B |
| Continue | TypeScript | 42 | B |
| CrewAI | Python | 92 | B |
| GPT Researcher | Python | 18 | C |
| MetaGPT | Python | 34 | C |
Aider scored our top grade. It is a coding agent, it does the same compounding-history thing every agent does, and the waste we found was negligible against what it spends. Worth being precise about what that means: a later agentic pass over the same repo did raise two issues, and both were about cost visibility rather than overspend, calls its own cost tracker could not see. That is a reporting gap, not a bill. Being an agent is not the problem.
So what drives the bill?
Across all 15 products the audit raised 94 cost findings. The distribution is the interesting part:
| Finding type | Count |
|---|---|
| Cost optimization | 69 |
| Caching | 8 |
| Unnecessary API call | 8 |
| Architecture | 6 |
| Model/task mismatch | 3 |
Model choice, the thing people reach for first, accounted for 3 of 94. The cheap-model swap is the most discussed lever and the one that mattered least in this corpus. The waste was overwhelmingly in how calls were made rather than which model answered them.
One more number from that run. Our structural scanner found most call sites by parsing the code, but an LLM pass recovered 56 more that the parser could not express, and 30 of those were hidden behind a project's own wrapper function. If you are trying to work out your own agent spend by grepping for anthropic.messages.create, you will miss the calls that go through your llm_client.py.
What can you do about it?
For your own session usage, the habits are well covered in how to reduce Claude Code token usage: start fresh sessions, disconnect MCP servers you are not using, compact before the context gets long, and right-size the model for the task.
For the bill that does not go away, the picture is different. Session hygiene shrinks what you spend while working. The LLM calls inside the product you ship run on every user request, forever, and no amount of compacting touches them. That is the spend our audit is pointed at.
If you want the same analysis on your own repository, the CLI runs it in your environment under your own keys. We never see your source. For a quick look first, paste a file into a free scan.
Method: figures come from erabot's audit of 15 open-source AI products. Cost and savings figures are modeled at stated call volumes and are estimates until measured against a deployment's real usage. Grades, call-site counts and finding counts are direct outputs of the audit. Pricing ratios are from our pricing table as last verified 2026-07-21. We did not audit Claude Code itself, which is closed source; the agents in the table above are the open-source ones we could read.