Reduce Claude Code API Costs: 7 Strategies That Cut Our Bill 73% Without Losing Quality
Claude Code can run $300–500/month per heavy user on the API. These 7 strategies — model routing, prompt caching, context pruning, sub-agent discipline — cut our bill from $480 to $128/month with no quality drop.
Heavy Claude Code usage on the API can hit $300–500 per developer per month. We were on track for $480/month before optimization. After applying the 7 patterns below, our bill is $128/month — same workflow, same code shipped, same quality. 73% reduction. Here's exactly what we changed.
If your bill is under $50/month, most of these still apply but won't move the needle much. If you're over $200/month, you'll see savings within a week.
The Cost Anatomy of a Claude Code Session
Before optimization: know what you're paying for. Every Claude Code turn has three cost components:
- Input tokens — your prompt + system prompt + CLAUDE.md + tool definitions + prior turns + tool results. Sonnet 4.5 charges $3/M tokens uncached, $0.30/M tokens cached (90% discount).
- Output tokens — what the model writes back. Sonnet 4.5 charges $15/M tokens. There's no caching discount on output.
- Tool execution — free on the API side, but tool calls produce more input tokens (results) on the next turn.
Our average heavy session before optimization: 200K input tokens, 15K output tokens. That's $0.60 + $0.225 = ~$0.83 per session. 600 sessions/month = ~$500. After optimization: 50K cached + 30K uncached input, 8K output = $0.015 + $0.09 + $0.12 = ~$0.225 per session. ~$135/month. The math compounds.
Strategy 1: Model Routing (Save 50–60%)
The default Sonnet-everywhere setup is the biggest leak. Different tasks deserve different models.
Sonnet 4.5 ($3/$15 per M tokens) — code writing, planning, complex refactors, anything that produces real code changes.
Haiku 4 ($1/$5 per M tokens) — file reads, simple edits, classification, "summarize this file", tool routing, sub-agent exploration. Roughly 3x cheaper input, 3x cheaper output vs. Sonnet, with quality that's plenty for these tasks.
Opus 4.5 ($15/$75 per M tokens) — only when Sonnet visibly struggles. ~5–10% of our workload.
Configure in .claude/settings.json:
{
"defaultModel": "claude-sonnet-4-5",
"subAgentModel": "claude-haiku-4",
"fallbackEscalation": "claude-opus-4-5"
}
Or per-task: claude --model haiku for "read this codebase and summarize" tasks. This single change cut our bill ~$180/month.
Strategy 2: Prompt Caching (Save 60–85% on Input)
Anthropic's prompt caching gives a 90% discount on cached input tokens for 5 minutes (1 hour with extended caching, slightly higher cost). Claude Code uses it automatically on stable prefixes — system prompt, tool definitions, CLAUDE.md.
The key insight: the cache hits on identical prefix bytes. So if your CLAUDE.md changes mid-session, you blow the cache. If you re-order tool definitions, you blow the cache. If your CLAUDE.md is 800 lines and stable, you save ~$0.0024 per turn vs. ~$0.024 — 10x cheaper.
Practical rules:
- Keep CLAUDE.md stable within a work session. Don't edit it mid-task.
- Don't pass volatile data in the system prompt. Put dates, IDs, dynamic context in user messages.
- For long sessions, prefer one big session over many short ones. Cache hits compound; small sessions never warm the cache.
Caching alone cut our monthly input cost from $220 to $35.
Strategy 3: Context Pruning (Save 30–40%)
Sessions accumulate stale tool results. After 90 minutes, 60% of your context might be old file reads no longer relevant.
Three techniques:
/clear between unrelated tasks. Don't carry 90 minutes of refactor context into a 5-minute typo fix. Hard reset is free.
Use sub-agents for exploration. A sub-agent does the reading in an isolated context, returns a 200-word summary, and the parent's context stays lean. We saw 40% lower per-turn input costs after adopting this pattern. See sub-agents guide.
Be explicit about what to keep in scope. Tell the agent: "Don't re-read files we've already discussed unless I mention them." Models follow this instruction reliably.
Strategy 4: Lean CLAUDE.md (Save 5–15%)
Every line of CLAUDE.md gets loaded into every session. A 1,500-line CLAUDE.md adds ~6K tokens per turn. After 50 turns, that's 300K tokens of CLAUDE.md alone — even with caching, that's $9 just on the file you forgot to trim.
Aim for 200–400 lines. What to cut:
- Outdated decisions ("we used Express until April 2024…")
- Generic best practices Claude already knows
- Long file structure trees the agent can grep itself
- Commented-out experimental rules
What to keep:
- Stack with versions
- Active conventions (max 10 rules)
- "Never do" list (max 5 items)
- Commands cheat sheet
See our CLAUDE.md guide for the lean template.
Strategy 5: Sub-Agent Discipline (Save 15–30%)
Sub-agents cost real money — typically 3–6x a single-agent run for a fan-out workflow. They earn it back via wallclock savings, but only when you use them right.
Wrong: spawning 8 sub-agents "to be safe" when 2 would do.
Right: spawn the minimum number that gets you the parallelism win. 4 is the sweet spot for most exploration; rarely need more.
Also: default sub-agents to Haiku. Most exploration tasks don't need Sonnet. Set it in .claude/settings.json (subAgentModel: "claude-haiku-4"). Same workflow, ~3x cheaper sub-agent calls.
Strategy 6: Batch Mode for Repetitive Tasks (Save 50%)
If you're running the same task across 100 files (add a docstring, write a test, lint-fix) — the Anthropic Message Batches API processes async over 24h at 50% off.
Claude Code itself runs synchronous, but you can extract a batch task: have Claude Code generate the prompt template, then submit the batch via SDK, then have Claude Code apply results.
Workflow:
# Step 1: Generate batch
claude "For each file in src/components/, write a JSDoc block. Output a batch.jsonl file with prompts only — don't run them."
# Step 2: Submit batch (async, 50% cheaper)
python submit_batch.py batch.jsonl
# Step 3: Apply results
claude "Read batch_results.jsonl and apply each docblock to its target file."
For our internal "doc all the things" sweeps across 200+ files, this saves ~60% vs. running it interactively.
Strategy 7: Hybrid Pro + API (Save 30–50% for Solo Devs)
If you're under heavy use solo, the cheapest setup is hybrid:
- Daily IDE work on Claude Pro ($20/month). Covers ~5–10 hrs/week of active coding within the cap.
- API key for everything else. Long batch jobs, scripts, CI, multi-agent experiments.
claude logout switches back to API mode. claude login returns to Pro. Add a shell alias if you switch often.
This pattern works because Pro's cap is generous for interactive sessions but doesn't reset across batch jobs. Use the right tool for each job.
What Doesn't Save Money
A few things people try that don't work:
Switching to Haiku for everything. Quality drops on real code-writing tasks. You'll spend 30 minutes fixing what Sonnet would have done right the first time. Not worth $0.30 saved.
Aggressively short prompts. The model needs context to work. Over-pruning prompts produces bad output, which costs more turns to fix.
Running open-source models locally. Llama 3.3 70B is ~3x slower and weaker on tool use vs. Sonnet 4.5. Net productivity loss > API savings for most workflows.
Disabling auto-context (--no-cwd or similar). You'll spend more time pasting context manually than you save in tokens.
Our Before/After Numbers
For full transparency, here's what we tracked over 4 weeks:
| Metric | Before optimization | After optimization | Change | |--------|--------------------|--------------------|--------| | Avg input tokens / session | 200K | 80K (50K cached) | -60% | | Avg output tokens / session | 15K | 8K | -47% | | Avg session cost | $0.83 | $0.21 | -75% | | Sessions per month | 600 | 600 | unchanged | | Monthly bill | $498 | $128 | -73% | | Quality complaints (internal) | baseline | unchanged | 0 | | Wallclock per session | baseline | -10% | faster |
Same work, same quality, $370/month back in the budget.
Putting It All Together
The order to apply these in, ranked by ROI:
- Prompt caching — auto-applied; just keep prefix stable. Biggest single win.
- Model routing — Haiku for sub-agents and reads, Sonnet for code, Opus only when stuck.
- Lean CLAUDE.md — trim to 200–400 lines.
- Context pruning —
/clearbetween unrelated tasks, sub-agents for exploration. - Sub-agent discipline — fan out 4 max, Haiku as default sub-agent model.
- Batch mode — for repetitive cross-file tasks.
- Hybrid Pro + API — for solo devs under ~15 hrs/week.
Track your spend weekly in the Anthropic console. When a week trends 30%+ over the last, look at what changed. Cost optimization is about discipline, not one-shot tweaks.
Want the full Claude Code workflow that uses these patterns by default? See our Claude Code 101 course.
Frequently Asked Questions
How much does Claude Code typically cost per month?
Light use (~5 hrs/week, simple edits): $20–40/month on the API, or covered by Claude Pro at $20/month. Heavy use (~20+ hrs/week, full agent workflows): $250–500/month on the API. Our heaviest internal users hit $480/month before optimization. Costs scale roughly with: (a) tokens read into context, (b) output tokens generated, (c) model choice. Sonnet 4.5 is our default; Opus runs 5x more expensive.
Should I use Claude Pro or the API for cost?
Pro at $20/month wins for solo developers under ~10 hours/week of active coding — its included usage covers most workflows and the cost is fixed. The API wins for: (a) heavy users (>20 hrs/week) who can optimize, (b) teams who want shared billing visibility, (c) anyone running scripts/automations where Pro's rate limits would block you. Many teams run a hybrid: Pro for daily IDE work, API key for batch jobs and CI.
Does prompt caching actually work for Claude Code?
Yes — and it is the single biggest cost lever. Anthropic's prompt caching gives a 90% discount on cached input tokens for 5 minutes (up to 1 hour with extended caching). Claude Code uses it automatically on tool definitions, system prompts, and CLAUDE.md content. Real numbers from our usage: caching dropped our average input cost from $3/M tokens to ~$0.45/M tokens over multi-turn sessions — about 85% savings on the input side. The catch: caching only kicks in on repeated prefix content, so short one-off sessions don't benefit much.
How does model routing save money in Claude Code?
Different tasks deserve different models. Haiku 4 ($1/$5 per M tokens) handles file reads, simple edits, classification, and tool routing — 80% of agent work. Sonnet 4.5 ($3/$15) handles planning, complex refactors, and code writing. Opus 4.5 ($15/$75) is for hardest reasoning only. Routing 60% of work to Haiku cuts your bill by 50–60% with negligible quality drop on appropriate tasks. Configure via --model flag or in your .claude/settings.json.
What is the biggest mistake people make on cost?
Letting context bloat over long sessions. After 2 hours of agent work, your context can hit 150K+ tokens — and every new turn pays for that whole context (minus caching). Two fixes: (1) run /clear between unrelated tasks to reset, (2) use sub-agents for exploration so the parent's context stays lean. Doing both alone cut our long-session costs by ~40%.
Can I cap Claude Code spend per day?
Yes — Anthropic's API console lets you set monthly spend limits and per-key rate limits. Configure them. We set hard monthly caps at the workspace level, then use per-developer keys with individual rate limits as a second safety net. For team use, add Datadog/Grafana dashboards on the Anthropic billing API so cost spikes alert before the invoice arrives.
Is it cheaper to run open-source models locally?
For simple tasks, yes — but for full agent workflows, almost never. Llama 3.3 70B running on a Mac M3 Max is ~3x slower than Sonnet 4.5 and weaker on tool use. The API cost saved (~$200/month) often does not justify the productivity loss (~10 hrs/month at any meaningful hourly rate). The sweet spot for local models: offline embedding, batch summarization, and dev-environment exploration where latency is hidden.
Continue Learning
Get the free AI Builder Newsletter
Weekly deep-dives on AI tools, automation workflows, and builder strategies. Join 5,000+ readers.
No spam. Unsubscribe anytime.
Go deeper with AI Builder Club
Join 1,000+ ambitious professionals and builders learning to use AI at work.
- ✓Expert-led courses on Cursor, MCP, AI agents, and more
- ✓Weekly live workshops with industry builders
- ✓Private community for feedback, collaboration, and accountability