Blog

Cut Claude Code token usage: context, compaction and model choice

Anthropic's own numbers put the average Claude Code bill for enterprise teams at about $13 per developer per active day, and under $30 a day for 90% of users. The other 10% are rarely doing harder work. Usually one session has grown huge, a test runner dumps 3,000 lines into the context, or every small rename runs on the most expensive model at full effort.

Each request Claude Code sends includes the whole conversation so far, so a token you add early gets paid for on every turn that follows. This post covers how to see where your tokens go and the habits that cut them without making the agent worse. All of it works with stock Claude Code.

Where the tokens go

A request contains more than your last message. Roughly, it carries:

Part Size Sent on
System prompt and tool list Fixed per version Every request
CLAUDE.md files and rules without paths Whatever you wrote Every request
MCP server names and instructions Small per server Every request
Earlier messages and tool results Grows all session Every request
Extended thinking Up to tens of thousands of tokens Billed as output, per request

Prompt caching makes the stable start of that prompt cheaper. Claude Code sets the cache markers for you, and cached input is billed at a lower rate. It doesn't make it free, though. A long session still pays for the whole conversation on every turn, and the first message after a break longer than the cache lifetime misses the cache and reprocesses everything at full price.

Measure before you cut

Two built-in commands tell you what's going on:

  • /usage (also /cost and /stats) shows the session's estimated cost, usage by model and, on recent versions, a Prompt cache (main) line with your cache hit rate and the last miss. On paid Claude plans it also attributes usage to things like a large CLAUDE.md or a particular MCP server, and pressing f applies its suggested fixes.
  • /context draws the current context as a coloured grid and flags heavy tools, memory-file bloat and how close you are to the limit. /context all expands it.

Run /context once at the start of a fresh session in your main repo. That's your fixed cost per request before you've typed anything, and it's the number the next three sections reduce.

Clear between tasks, compact within one

These two commands are the biggest lever you have, and they do different jobs.

/clear starts a new conversation with an empty context. It costs nothing. Use it whenever you switch to unrelated work: the details of the auth bug you fixed an hour ago don't help with the CSS change you're starting now, and they're billed again on every message. If you might want the old thread back, run /rename auth-bug first and return to it later with /resume.

/compact summarises the conversation so far and carries on from the summary. Use it when you're mid-task and the context is getting long but you still need the thread. It takes focus instructions:

/compact Keep the failing test names, the files changed so far and the decision to keep the v1 API. Drop the exploration of the old cache layer.

Three things about compaction aren't obvious:

  1. Compacting a big context is itself a big request. The model has to read everything it summarises. Compact on purpose at a natural break rather than waiting for auto-compaction to fire in the middle of an edit.
  2. Compact before you step away. After a long break the cache has expired and your next message reprocesses the whole context. A compacted context is far cheaper to reprocess. The /usage fix list suggests exactly this.
  3. You can set standing compaction rules. Add a section headed # Compact instructions to CLAUDE.md with a line such as "When compacting, keep test output and code changes; drop file listings." Compaction follows it from then on.

If you'd rather not keep the conversation at all, /rewind (or double-tap Escape) rolls the conversation and code back to an earlier checkpoint, which beats spending tokens explaining what to undo.

Keep CLAUDE.md under 200 lines

Every line in your CLAUDE.md is sent with every request, including the ones where it's irrelevant. Claude Code's docs suggest staying under 200 lines per file, both for cost and because long files get followed less consistently.

Check what you're carrying. This prints line counts and a rough token estimate for the files that load at startup. Four characters per token is a crude average for English, so treat the number as a size class, not a bill:

for f in CLAUDE.md CLAUDE.local.md .claude/rules/*.md ~/.claude/CLAUDE.md; do
  if [ -f "$f" ]; then
    printf '%6d lines  ~%6d tokens  %s\n' "$(wc -l <"$f")" "$(( $(wc -c <"$f") / 4 ))" "$f"
  fi
done

When something's too big, move it rather than delete it:

  • Procedures go into skills. A ten-step release checklist only needs to be in context when you're releasing. Skills load their content on demand.
  • Area-specific rules go into path-scoped rules. A file in .claude/rules/ with paths: front matter loads only when Claude touches matching files.
  • Subdirectory CLAUDE.md files load lazily. One in packages/web/ is read once Claude works on a file in that directory, not at launch.

Imports with @path/to/file don't save anything. Imported files load along with the file that imports them. More on structuring these files is in AGENTS.md and CLAUDE.md: a practical guide.

Stop test output from flooding the context

A failing test suite is the most common way a session goes from 40k to 200k tokens. The agent runs npm test, gets 2,000 lines of passing tests and stack traces, fixes one thing and runs it again. Every one of those runs stays in the conversation.

A small wrapper fixes most of this. It hides output when the command passes and prints only the error lines and the tail when it fails:

#!/usr/bin/env bash
log=$(mktemp)
"$@" >"$log" 2>&1
status=$?
if [ "$status" -eq 0 ]; then
  echo "PASS: $* ($(wc -l <"$log") lines of output hidden)"
else
  echo "FAIL (exit $status): $*"
  grep -nE 'FAIL|Error|error:|panic' "$log" | head -n 40
  echo "--- last 60 lines ---"
  tail -n 60 "$log"
fi
rm -f "$log"
exit "$status"

Save it as scripts/test-quiet.sh, make it executable, and tell Claude about it in CLAUDE.md:

- Run tests with `scripts/test-quiet.sh npm test` (or `... pytest -x`). It prints only failures.
- Rerun a single failing test file instead of the whole suite.

In our test, a passing run that printed 500 lines came back as one line, and a failing run that printed about 400 came back as 64, with the failing test name and assertion at the top. The exit code passes through unchanged, so the agent still knows the run failed. Adjust the grep pattern to whatever your test runner prints on failure.

If you'd rather enforce it than ask for it, a PreToolUse hook on Bash can filter test commands automatically. Anthropic's cost docs include one that keeps the last 100 lines, and our Claude Code hooks guide covers how hooks are wired up.

Trim MCP servers you're not using

MCP tool definitions are deferred by default now: only server names and their instructions enter the context until Claude actually calls a tool. That makes idle servers cheap, but not free, and a server whose tools return large results costs a lot when it's used.

  • Run /mcp and disable servers you don't need for the current project.
  • Prefer a CLI the model already knows (gh, aws, gcloud, sentry-cli) over an MCP server that wraps the same API. A CLI adds no listing at all, and you can pipe its output through head or jq before it reaches the model.

Match the model and effort to the task

Model choice changes the price of every token, so it's worth getting right per task, not per account:

  • Use a mid-tier model by default. Anthropic's guidance is that Sonnet handles most coding tasks and costs less than Opus. Save Opus for architecture decisions and long multi-step reasoning. /model switches mid-session and shows per-million-token prices in the picker.
  • Turn effort down for routine work. Thinking tokens are billed as output, and the default budget can be tens of thousands of tokens per request. /effort lowers it for renames, small fixes and boilerplate. The Claude 5.5 and Fable models always think, so effort is the control there. On models with a fixed thinking budget you can also set MAX_THINKING_TOKENS=8000.
  • Watch what subagents inherit. A subagent without its own model setting uses the session's model, so switching to Opus for one hard question also moves every inheriting subagent to Opus.

Send verbose work to a subagent

A subagent runs in its own context window and only its summary comes back to your main conversation. That makes it the right place for work that reads a lot and reports a little: running the full test suite, searching logs, reading library docs.

A project subagent is a Markdown file in .claude/agents/. Giving it a small model keeps the bulky part cheap:

---
name: test-runner
description: Runs the test suite and reports only failing tests with file, line and assertion. Use after code changes.
tools: Bash, Read, Grep
model: haiku
---

Run the project's tests with scripts/test-quiet.sh. Report each failing test as
one line: file:line, test name, assertion message. Do not paste full stack traces.
If everything passes, reply with the pass count only.

This isn't free. The subagent's own requests still count against your usage, and many subagents returning long reports can fill the main context anyway. Ask for short, structured reports.

The same caution applies more strongly to agent teams. Anthropic measures them at roughly 7 times the tokens of a normal session when teammates run in plan mode. Keep teams small and shut teammates down when they're done.

Prompts that don't scan the whole repo

The cheapest token is the one the agent never reads:

  • Be specific. "Improve this codebase" makes the agent scan everything. "Add retry with backoff to fetchInvoice in src/billing/client.ts" makes it open one file.
  • Plan first on anything non-trivial. Shift+Tab cycles into plan mode, so you can fix a wrong approach before any code is written.
  • Stop early. If the agent heads the wrong way, press Escape. Letting a bad edit run to the end and then explaining the problem costs more than interrupting it.
  • Give it a way to check itself. A test to make pass, or an expected output, ends the loop sooner than "does this look right?".
  • Work in small steps. Write one file, run it, then go on. A 600-line change that fails is expensive to debug from inside one context.

Running Claude Code through ChromaLab

If you point Claude Code at ChromaLab, two more levers are available:

  • Cheaper background model. ANTHROPIC_SMALL_FAST_MODEL handles Claude Code's background tasks. Any cheap id from the models page works there, while ANTHROPIC_MODEL stays on a strong model.
  • Hard limits per key. Each key can carry a daily token budget, reset at UTC midnight, and a cap on max_tokens. Both are checked before the request leaves ChromaLab, so a runaway unattended session stops at the budget. Settings are under per-key controls.

Prompt caching keeps working on this path. The cache_control markers Claude Code sets are forwarded upstream for Anthropic-backed models, so the habits above that protect your cache still pay off.

Checklist

  • Run /context in a fresh session and note your fixed cost per request.
  • /clear when you switch tasks. /rename first if you'll want the thread back.
  • /compact with focus instructions at natural breaks, and before you step away.
  • Add # Compact instructions to CLAUDE.md.
  • Keep each CLAUDE.md under 200 lines; move procedures to skills and area rules to .claude/rules/.
  • Wrap test commands so passing runs print one line.
  • Disable unused MCP servers with /mcp; prefer CLIs.
  • Default to a mid-tier model; lower /effort for routine edits.
  • Put verbose work in a subagent with model: haiku and short reports.
  • Check /usage weekly and apply its suggested fixes.

Sources: Anthropic's Claude Code docs on costs, commands, memory and subagents, read on 22 November 2026.