Claude Cost Optimization: 12 Techniques
Twelve habits that cut Claude Code and Claude API spend without lowering quality.
Claude Code
CLAUDE.md project memory
Context once
- When it helps
- Every session starts by re-explaining the stack, the conventions and the constraints.
- What to do
- Write a 300–500-word CLAUDE.md with the stack, key folders, commands, rules and the verification gate. Update it whenever you catch yourself repeating an explanation.
- Why it works
- It is loaded once per session, so the setup tokens are paid once instead of in every prompt, and the answers follow your conventions from the first turn.
- Example
- CLAUDE.md, twelve lines: "Next.js 16 static export, Tailwind 4, MDX in content/en|es. Strings live in messages/en.json and are mirrored in es.json. Gate before any push: npm run lint && npx tsc --noEmit && npx vitest run && npm run build. Never commit CLAUDE.md." From then on no session asks where the strings live.
- Typical saving
- 500–2,000 tokens saved per session (estimate)
Claude Code
Targeted file inclusion
Only what's needed
- When it helps
- You are changing one function but the whole directory, tests included, ends up in context.
- What to do
- Name the file being changed, the files it imports and the type definitions in use, nothing else. Skip test files when implementing and feature files when writing tests.
- Why it works
- Input tokens are the bulk of the bill and extra files add cost without improving the answer.
- Example
- Prompt: "Change the og:title in lib/seo/metadata.ts. It imports messages/en.json and lib/site.ts: read those two and nothing else." Three files in context instead of the whole lib/ folder and its tests.
- Typical saving
- 60–80 % fewer tokens per task (estimate)
Claude Code
Specific prompts
File, function, change
- When it helps
- "Improve this codebase" or "what could be better here?" starts a long exploration you did not ask for.
- What to do
- State the file, the function, the exact change and how to verify it. One task per prompt; keep open-ended analysis for a dedicated session.
- Why it works
- A scoped prompt needs less context and produces a shorter, more accurate answer.
- Example
- Before: "Improve the SEO of the site." After: "In lib/seo/jsonld.ts, add dateModified to the Article node using post.lastmod; verify with npx tsc --noEmit and the existing unit tests." One file, one change, one check.
- Typical saving
- 2–5× fewer tokens than a vague prompt (estimate)
Claude Code
Batch related tasks
One session, one context
- When it helps
- Three small changes to the same module are made in three separate sessions.
- What to do
- Group the changes that touch the same files into one session, then move on to the next module.
- Why it works
- The module is read into context once instead of three times.
- Example
- One session for the three changes to the testimonial carousel (new quote, photo crop, alt text), back to back; a fresh session afterwards for the blog post. The carousel files are read once, not three times.
- Typical saving
- 20–40 % less spend on those tasks (estimate)
Claude Code
/compact at checkpoints
Shrink history
- When it helps
- A session has run for 30–45 minutes and every new prompt drags the whole conversation along.
- What to do
- Run /compact after each unit of work, like a commit. Ask it to keep the decisions you still need before compacting.
- Why it works
- The history is replaced by a short summary of decisions and state, so later turns stop paying for early ones.
- Example
- After the testimonial lands: "/compact keep the decision to crop photos to 330×330 and the list of files still to touch." The next prompt starts from a summary instead of forty tool outputs.
- Typical saving
- 10–20k tokens of history → 1–3k (estimate)
Claude Code
Measure the spend
/cost and --verbose
- When it helps
- The bill surprises you and nobody knows which prompts caused it.
- What to do
- Run Claude Code with --verbose for a full day and check /cost per session; note the two or three prompts that dominate.
- Why it works
- A handful of broad context loads usually account for most of the spend, and you can only fix what you can see.
- Example
- claude --verbose for a day, then /cost in each session: two prompts ("explain the whole repo", "fix all lint errors") cost more than the other twenty together. Those two get a CLAUDE.md note and a narrower scope.
Claude Code
Plan mode first
Agree, then build
- When it helps
- A feature touches several files and a wrong first attempt would be expensive to undo.
- What to do
- Switch to plan mode, let it read and propose, correct the plan, then approve the implementation.
- Why it works
- Reading is cheaper than rewriting: a plan reviewed in minutes avoids paying twice for the same change.
- Example
- In plan mode: "Add a Spanish version of the FAQ." Claude reads data/faq.ts and the ES page, proposes the file list and the string keys, you strike one wrong assumption, then it implements. No code written against the wrong design.
Claude API
Right model per task
Haiku → Sonnet → Opus
- When it helps
- Every request, including classification and extraction, runs on the most capable model.
- What to do
- Route by task: Haiku 5.5 for routine extraction and classification, Sonnet 5.5 for everyday coding and agents, Opus 5.5 where judgement matters. Measure quality on a sample before switching a route.
- Why it works
- List prices per million input tokens are $0.10 for Haiku 5.5, $2 for Sonnet 5.5 and $4 for Opus 5.5, so the tier decides the bill more than any prompt trick.
- Example
- Support pipeline: Haiku 5.5 classifies the ticket (billing, bug, feature), Sonnet 5.5 drafts the reply, Opus 5.5 reviews only refund decisions above a threshold. Each route measured on 200 past tickets before the switch.
- Typical saving
- $0.10 vs $2 vs $4 per million input tokens (official figure)
Claude API
Effort level
Depth you pay for
- When it helps
- Chat, classification or high-volume routes run at the same reasoning depth as hard engineering tasks.
- What to do
- Set output_config.effort per route: low or medium for chat and classification, high or xhigh for agentic coding; try a lower effort on the newest model before building a multi-model cascade.
- Why it works
- Effort trades thinking tokens for cost inside one model, and one model keeps one prompt cache.
- Example
- The chat endpoint on Sonnet 5.5 with output_config.effort set to "medium"; the agentic coding job keeps "high". Same model, shorter reasoning on the easy route, checked on a week of traffic before touching the other routes.
Claude API
Prompt caching
Stable prefix
- When it helps
- The same system prompt, tool list or reference document is sent on every request.
- What to do
- Put the stable content first, add cache_control at the end of it, keep timestamps and user input after it, and check usage.cache_read_input_tokens is not zero.
- Why it works
- Cached input is read at about a tenth of the normal input price, and the prefix only has to match byte for byte.
- Example
- System prompt and the 40-page policy document first, cache_control: {"type": "ephemeral"} on that last block, the user question after it. From the second call on, usage.cache_read_input_tokens is about the size of the document.
- Typical saving
- Cached reads cost about 90 % less (official figure)
Claude API
Batch API
Async at half price
- When it helps
- Evaluations, backfills, nightly reports or any job that does not need an answer within seconds.
- What to do
- Submit the requests with messages.batches.create, poll until the batch has ended and match results by custom_id.
- Why it works
- Batch requests are billed at half the standard price in exchange for asynchronous processing.
- Example
- Nightly: 5,000 product descriptions to classify. messages.batches.create with one request per product and custom_id = the product id; poll in the morning and match results by custom_id. Half price, no rate-limit juggling.
- Typical saving
- 50 % off standard prices (official figure)
Claude API
Lean agent context
Compact, trim, budget
- When it helps
- A long-running agent loop resends every old tool result and the prompt grows turn after turn.
- What to do
- Enable server-side compaction for long conversations, clear old tool results with context editing, and give agentic loops a task budget so they pace themselves.
- Why it works
- Smaller prompts cost less on every turn and keep the model focused on the current step.
- Example
- An agent that makes sixty tool calls: compaction enabled, tool results older than five turns cleared, a task budget of 200k tokens. Turn fifty costs about the same as turn five instead of resending every old file read.
Pick a technique on the map to see when it helps, what to do and why it works.
Official figures: Anthropic pricing and API documentation. Estimates: LOW/CODE Agency, "Claude Code Cost Optimization" (Sep 2026), and project experience; ranges vary by codebase.
