← Back to Journal

Where the Tokens Actually GoMeasuring a Claude Code session instead of guessing

One question in Claude Code was eating about 4% of my context window. I was sure the cause was the pile of MCP servers and connectors I had installed. I was wrong. Here is what measuring showed, and the one rule that now decides what I optimise.

The suspect everyone blames

My setup is crowded. Claude Code writes code, Codex reviews it, and both share one knowledge base through an MCP server. On top of that the desktop app has connectors for design tools, mail, a browser extension and computer control. When a single question started costing a visible slice of the context window, the obvious move was to disconnect things.

Before deleting anything, I measured one real working session instead.

What one 82k-token session was made of

So the thing I was about to rip out cost nothing, and the expensive part was something I had not even looked at.

The rule: paid every turn vs. paid once

The fixed overhead is paid at the start of a session and then served from the prompt cache. Messages are different: everything that enters the conversation is re-read on every later turn. A 5,000-token tool result on turn 3 of a 40-turn session is not a 5,000-token cost. It rides along for the other 37 turns.

That gives one question to ask about any token cost: is this paid once, or paid every turn? Optimise the second kind first. Almost every useful fix I found came from this split.

Fix 1: the search reply that was nine times too big

The biggest per-turn item was my own knowledge-base search. Each call returned eight results, and each result carried its full metadata: score breakdowns, excerpts, related links, privacy flags, timestamps and supersession chains. One search reply was over 9,000 bytes. The agent only needed a title and a one-line summary to decide what to open next.

The fix was small:

The same query went from 9,000+ bytes to about 1,560 — roughly 83% smaller. Full notes are still one call away, so nothing was lost. Because Claude Code and Codex share the same search function, both agents got the saving at once.

Fix 2: my own formatting rules were inflating the context

The second surprise was embarrassing. I had asked my agents to present every decision as five options, each assessed on effect, risk, cost, maintenance and fit. That made every option a small essay. Those essays were messages, so they were re-read for the rest of the session.

I kept the five options and cut each one to a single line: one benefit, one cost, one recommendation. The decision quality did not drop. If I need more detail I ask, and only then is it paid for.

The lesson generalises: output-format rules are a recurring cost. Every instruction that makes answers longer is multiplied by the number of turns that follow.

Fix 3: keep long digging out of the main conversation

Some work is long to do and short to report. Searching many files, running a test suite or reading a long log produces large intermediate output for a conclusion of a few lines. If that output lands in the main conversation, it is paid for on every later turn.

Now that kind of work goes to a sub-agent with an explicit instruction: return the conclusion only, not the raw content. The main thread receives a few hundred tokens instead of tens of thousands. The sub-agent still spends tokens, but once, not on every later turn of a long session.

The most expensive token is the one spent on rework

Context size was not the biggest cost I found. I pulled the logs for a single feature ticket in one of my iOS projects:

The same ticket was built three times. The two rework runs used about 60% of the total. The root cause was not the model: the project's tests could not run locally, so the agent could not check its own work before handing it over. Each broken handoff meant another full run with a long context.

Making the tests runnable and trimming their output was worth more than any prompt trick. A run that cannot verify itself will eventually be paid for twice.

One more trap: what you store is not what you pay for

A related problem showed up on disk, not in the bill. A computer-control plugin in another agent saved every screenshot inline, as base64 text inside its session log. One log file reached 1.6 GB with only about 45,000 lines, roughly 37 KB per line, while normal conversation lines are a few hundred bytes. Monthly log growth went from 52 MB to 2.2 GB in four months. Turning off that plugin when I was not using it stopped the growth.

It is not a token cost, but the habit that finds it is the same: measure the artefact, and do not trust the label.

A checklist you can run on your own setup

  1. Measure one real session before changing anything. Split it into messages, fixed overhead and deferred tools. Your guess about which is biggest is probably wrong; mine was.
  2. Classify every cost as per-turn or once. Ignore the once-per-session items until the per-turn ones are fixed.
  3. Put your own tools on a diet. Return summaries and let the agent fetch full content deliberately. Default result counts are usually too high.
  4. Audit your formatting rules. Anything that makes every answer longer is multiplied by the session length.
  5. Move long digging into sub-agents that report conclusions, not raw output.
  6. Make the agent able to test its own work. Rework is the most expensive line on the bill and the one no prompt setting can fix.
  7. Keep a baseline. Write down the numbers before a change, or you will never know whether it helped.

None of this needed a new tool. It needed one habit: separate what you pay once from what you pay on every turn, and measure before you cut.

Claude CodeToken CostContext WindowMCPCodex