Cut Claude Code Tokens by Pruning Dead Weight
Most of the bill is re-sent history and unused tool schemas, not hard thinking.
Agentic coding tools feel expensive because the model looks busy. The real cost driver is usually quieter: every turn re-mails a growing pile of stale files, tool schemas, and system text the model has already acted on. That snowball is why a long Claude Code session can burn millions of input tokens while the model only writes a few thousand words of useful output.
A transparent proxy experiment on Claude Code (Opus 4.6 via Bedrock) put hard numbers on this. On a fixed JavaScript fixture (nine planted bugs, three stubbed methods, 54 visible tests), an unoptimized 70-request run used about 3.9M input tokens at roughly $2.04. The same task with mid-flight context trimming finished at about 776K input tokens and $0.48, a 77% cut, with identical quality (54/54 fixture tests, same edge-test score). Those percentages are early and single-fixture; treat them as directionally strong, not universal law. The lesson still holds: cost drops when you stop carrying dead weight, not when you beg the model to "think less."
The snowball is the product architecture
Claude has no session memory between HTTP calls. Turn 30 is not "what changed since turn 29." It is turns 1 through 30 again, plus the system preamble and full tool JSON schemas. In the profiled traffic that meant roughly 5K tokens of system text and 7K of tool schemas riding every request, on top of every file the agent ever read.
Anthropic softens the pain with prompt caching: up to four cache_control breakpoints, ephemeral prefixes, cache reads billed far below fresh input (on the measured Opus 4.6 Bedrock rates, cache read sat at $0.50/MTok against $5.00 input and $25.00 output). Claude Code already places those markers well and often hits near 100% cache. So "turn caching on" is not the open question. The open question is how much of the cached snowball you can drop without breaking the prefix match or the agent's next step.
Naive intuition says pruning old messages will shatter the cache and raise the bill. On Anthropic-format traffic in the live runs, that intuition was backwards. A "protect the cache, prune nothing" run used more requests, more tokens, and cost about 31% more than baseline. Carrying ~4.3M cached tokens per turn is still more expensive than carrying ~776K when both are cache hits. Cache hit rate is not the same thing as cost.
What actually moved the needle
The effective optimize layer sat in front of the model: tidy the outgoing request, leave the reply alone, leave agent behavior alone. The big levers, by share of savings on that fixture:
- pruneStale (~57%): a file read many turns ago and already edited becomes a one-line stub (
[read scheduler.js earlier — 47 lines]). The model already acted on the full content. - pruneUnusedTools (~23%): after enough turns, drop schemas for tools never called (often most of a 15-tool set).
- insertBreakpoints: after edits, re-anchor Anthropic's four cache markers so the surviving prefix still hits.
Smaller cuts (deduping repeat reads, truncating giant blobs, skipping needless re-reads) helped at the margins. Stubbing the repeated system prompt was deliberately de-emphasized; that path is riskier and less settled.
An offline byte-prefix simulator predicted pruning would tank hit rate to roughly 67%. Live Bedrock held near 100% with only tens of uncached tokens. Anthropic's real cache tolerated in-region edits better than a strict simulator assumes. Trust live bills over toy models when they disagree.
Portability is not free. The same prune-stale approach on DeepSeek (OpenAI-style chat format) shattered automatic prefix caching and raised cost. Format and cache semantics matter; copy-paste "optimizations" across providers without measuring.
What you can do without building a proxy
Most teams will not ship a request-rewriting proxy tomorrow. The same mental model still pays off inside stock Claude Code, and independent measurements line up on the same waste classes.
Measure first. A bare "hi" can already sit on a 20K–30K token floor (system prompt, CLAUDE.md, memory, MCP schemas, skill descriptions). Commands like /context, /usage, and /memory exist to show what is already loaded before you type a real task. Past roughly 300K–400K tokens of clutter, quality degrades from attention spread ("context rot"), not from a full window.
Shrink always-on text. Keep project instructions lean; one report cut CLAUDE.md-style context by about 92% with no quality regression by stripping to a few hundred tokens of load-bearing rules. Move bulk rules into path-scoped rule files so they are not always-on. Treat MCP servers as a tax: figures in the 10K–20K token range per server per session show up repeatedly, so enable only what the session needs.
Filter what the agent reads. Build and test logs compress 80–99% when you pipe them through a filter before the model sees them. Web research is the same story: raw HTML can be tens of thousands of tokens of chrome for a couple thousand tokens of content. Clean extraction tools change the math on research-heavy agents.
Structure the session. Plan, then build, then verify is not process theater. Separate modes and deliberate model routing (cheap model for grunt work, stronger model for hard steps) show double-digit token and cost cuts in practice, including up to roughly 75% in some routing cases. Progressive skills (name and short description at startup, full body only when invoked) beat dumping every playbook into the system prompt.
Do not "protect" the cache by refusing to prune. On Anthropic traffic, volume of cached tokens still costs money. Re-anchoring breakpoints after surgical stubs beat both no-opt and naive cache-protection in the measured runs.
A concrete adoption path for a working developer this week:
- Run
/contextand/memoryon a cold session; write down the floor. - Cut CLAUDE.md and global rules to what actually changes agent behavior; move the rest path-scoped or into on-demand skills.
- Disable unused MCP servers for the default profile.
- Wrap noisy commands (test, lint, build) so Claude sees summaries or tails, not 200-line dumps that live forever in the transcript.
- Prefer short, scoped tasks over multi-hour monologues; use compact/clear/rewind before the window turns toxic.
- If you control the path to the API (proxy, gateway, or custom agent loop), implement prune-stale + drop-unused-tools + breakpoint re-anchor and A/B on a fixed fixture with real quality checks, not vibes.
Caveats worth more than the headline
The 77% figure is one fixture, one model path, early results. Request counts rose under optimization (70 → 94) even as tokens and dollars fell; more turns with thinner prompts is a fine trade if the bill drops and tests still pass. Quality stayed flat on that suite, but your codebase may depend on re-reading an old file the stub would hide. Keep a kill switch. Re-verify cache hit rates and dollar cost after every Anthropic pricing or Claude Code change.
Process plugins that force clarify → design → plan → code → verify can cut rework (one controlled set of sessions reported about 14% fewer tokens and 9% lower cost with better results on non-trivial tasks). That is a different lever: fewer wrong builds, not thinner transcripts. Use them on ambiguous features; skip them for one-line CSS tweaks where the ceremony costs more than the mistake.
Sound hooks that meow when a turn finishes are charming. They do not shrink the bill. Cost lives in the request body.
The actual takeaway
Claude Code is not "too chatty" in the abstract. It is a stateless loop that re-ships history, and the default history is full of paperwork. Prompt cache makes that paperwork cheaper per token; it does not make infinite paperwork free. Mid-flight pruning of stale tool results and unused schemas, with cache breakpoints re-placed afterward, is the highest-leverage idea in the measured work. Everyone else can still win by lowering the always-on floor, starving the agent of HTML soup and raw logs, and ending sessions before context rot sets in.
If you only change one habit: stop treating a 100% cache hit rate as success. Ask how many tokens are in that hit, and whether the model still needs them to finish the job.
Sources & further reading
- How to make Claude Code go “meow meow” (hook edition) — dev.to
- What I Learned Cutting Claude Code's Token Bill by 77% — dev.to
- Making a Bloated Claude Code Fast Again: Auditing Context Injection Down From 228KB to 48KB — dev.to
- 12 Ways to Cut Token Consumption in Claude Code — firecrawl.dev
- 5 Claude Code Skills That Cut Token Costs by Up to 70% — Benchmarked Across Real Sessions | MindStudio — mindstudio.ai
Rachel has been embedded in the developer tooling ecosystem for nearly eight years, covering everything from IDE wars and package-manager drama to the quiet rise of AI-assisted coding. She has a soft spot for open-source maintainers and an unhealthy number of terminal emulators installed on a single laptop.
Discussion 0
No comments yet
Be the first to weigh in.