Lesson 30 of 32
Managing Cost
What actually drives token spend, how to check where yours is going, and the levers that reduce it — which turn out to be the same habits that make the output better.
Claude Code costs money — in tokens if you are on an API plan, in usage against your limits if you are on a subscription. Either way the mechanism is the same and the levers are the same.
This lesson is about where the money actually goes, which is rarely where people assume, and what to do about it. There is a pleasant surprise in it: the habits that reduce spend are, almost without exception, the habits that improve output. Both come from the same place.
What you are paying for
Not messages. Not time. Context.
Every request carries the whole conversation so far: your prompts, Claude's replies, every file it has read, every command it has run and everything those commands printed. Each time Claude uses a tool, that is another request, carrying that entire batch again.
This is why a session that has been open since breakfast is expensive even when you have barely touched it. Your one-line question at four o'clock arrives attached to six hours of accumulated history.
Two things soften this automatically. Prompt caching means repeated content is re-read at a cheaper rate rather than at full price. Auto-compaction summarises older history when you approach the context limit, which frees space. Neither makes the history free — caching makes it cheaper, not weightless, and a cache entry expires after a while, so your first message after a long break reprocesses everything at full price.
That connection back to lesson 8 is the whole reason this lesson is short. Almost everything below is something you already had a reason to do.
Checking where it goes
Guessing is a waste of time when three commands will tell you.
/usage
Token counts and an estimated cost for the current session, broken down by model. On a subscription plan it also shows how much of your plan limits recent work has used, attributed to the skills, subagents, plugins and MCP servers responsible, and flags any behaviour accounting for a large share — long context and cache misses being the two you will actually see.
/context
What is occupying the context window right now, and what each part costs. This is the one to run when you suspect something is being carried that you do not need — an oversized memory file, an MCP server you forgot was connected.
/insights
A report on how you work rather than what you spent, built from your recent sessions on this machine. It surfaces friction — misunderstood requests, repeated corrections — which is usually where the waste is hiding.
Two caveats on the figures. The cost number is computed locally from token counts at standard rates, so it will not match a bill that has a discount behind it, and it does not know about work you did on another machine. Treat it as a signal about your own habits, not as an invoice.
WhyThis session has been open for most of the day, drifting between three unrelated tasks. The prompts have started coming back slightly off, which is the symptom that usually precedes noticing the cost.
You typed: /usage
Output: Total cost: $4.18 Total duration (API): 22m 04s Total duration (wall): 7h 12m Total code changes: 41 lines added, 12 lines removedWhyTwenty-two minutes of actual model time across seven hours, and fifty-three lines of code. The wall clock is not what you pay for, but the ratio is telling you the session has been carrying a lot and producing little.
You typed: /context
Output: System prompt and tools CLAUDE.md 412 lines MCP tool definitions (3 servers) Conversation ~64%WhyTwo findings. The memory file is more than twice the length anyone can reasonably keep in view, and two of those three MCP servers are left over from a task that finished on Tuesday.
You typed: /clear
WhyClearing costs nothing and is the single largest lever here. Compacting would have been the alternative, but compaction has to read the conversation in order to summarise it, so it is itself a large request. Compact when you need the continuity; clear when you do not.
You typed: Move the database migration section out of CLAUDE.md into a skill, and leave a one-line pointer behind.
WhyThat section was being loaded into every session all week, including the ones about CSS. A skill loads when it is relevant. The bill goes down and, more usefully, the rules still in CLAUDE.md become findable again.
The levers
Three groups, in rough order of how much they matter.
Keep the context clean
This is most of the win.
/clear between unrelated tasks. Stale context is charged for on every
subsequent message and helps with none of them. If you might want the session
back, name it with /rename first so you can find it.
/compact when you need continuity but not the detail, and give it a hint —
/compact focus on the API changes — so the summary keeps what matters. You
can also put standing compaction instructions in CLAUDE.md.
Delegate verbose work to subagents. Running a large test suite, reading through a log, trawling the codebase to answer one question — all of that produces a great deal of output you will never look at again. In a subagent it stays in the subagent's context and only the summary comes back.
Keep CLAUDE.md short — under a couple of hundred lines is a reasonable
target — and move anything situational into a skill. Memory loads every
session; skills load when relevant.
Trim what is connected. MCP servers you are not using still occupy space, and a
CLI tool is usually the cheaper way to reach a service anyway. gh costs
nothing to have available; an MCP server costs context whether you use it or
not.
Match the model to the work
Sonnet handles most coding tasks well and costs less than Opus. Keep it as the
default and switch up with /model when you hit something that genuinely needs
harder reasoning — an architectural decision, a subtle bug, a plan spanning
many files. For subagents doing something mechanical, you can specify a smaller
model in the subagent's own configuration.
Extended thinking is a similar dial. It is on by default because it materially
improves hard reasoning, and the thinking is billed as output. For simple work
you can lower the effort level with /effort rather than paying for deliberation
the task did not need.
Stop paying for wrong turns
The most expensive tokens are the ones spent going in a direction you are about to abandon.
Write specific prompts. "Improve this codebase" makes Claude read everything;
"add input validation to the login function in auth.ts" makes it read one
file. Use plan mode when the approach is uncertain, so you find out you disagree
before the implementation exists rather than after. Give it something to verify
against, so it catches its own mistakes instead of handing them to you. Correct
early — the moment you see it heading somewhere wrong, press Escape, because
every turn after that point is paid for twice. And after two failed corrections
on the same point, clear and start again with a better prompt; a fresh session
almost always beats a long one full of failed approaches.
Every one of those is advice this course already gave you for reasons that had nothing to do with money.
The things that quietly add up
A few sources of spend that surprise people.
Parallel sessions and agent teams multiply usage by roughly the number of agents, because each one carries its own context window. That can be a good trade for research or review; it is a poor one for routine work.
Anything that fires while you are not there sends your full context each time it does. A scheduled task, a message arriving from another session — each is a turn, and each turn carries everything.
Claude Code uses a small amount of tokens in the background even when idle, summarising conversations so they can be resumed. It is genuinely small next to anything else on this page, and is not worth optimising.
For unattended work, --max-budget-usd caps what a -p run may spend before
it stops. Any automated loop that runs without you watching should have some
kind of ceiling on it, because the failure mode of a bad prompt in a loop is
not one expensive mistake but several hundred.
What to take away
You pay for context, not for messages, and every request carries the whole
conversation — which is why a session left open all day is expensive even when
idle, and why caching makes that cheaper rather than free. Find out where it
goes rather than guessing: /usage for the numbers, /context for what is
currently loaded, /insights for the friction in how you work. The biggest
lever is keeping context clean — clearing between unrelated tasks, compacting
when you need continuity, pushing verbose work into subagents, and keeping
CLAUDE.md to the things that apply always. After that, match the model to the
difficulty rather than defaulting to the largest, and stop paying for wrong
turns by being specific, planning when uncertain, and correcting early. All of
which is the same advice as lesson 8, arriving from a different direction,
because a clean context is both the cheap one and the good one.
Next: what an agent with your file access and your terminal can actually do wrong, and the isolation options that limit it.
Check yourself
5 questions · pass 4/5 to unlock Security and Sandboxing
1.You ask a one-line question in a session that has been open all day. Why does it cost more than the same question in a fresh session?
2.Which command shows what is currently occupying your context window?
3.When is it worth reaching for Opus rather than Sonnet?
4.Why does
/clearcost nothing while/compactcosts something?5.A CLAUDE.md file has grown to include a detailed database migration procedure. What is the cost argument for moving it into a skill?
5 left to answer