7 Practical Techniques to Optimize Claude Code Token Usage
Cut your Claude Code costs with 7 proven token optimization techniques. Learn /compact, prompt caching, subagent strategies, and model switching.
The more you use Claude Code, the more you start watching your token usage and costs. This article shares 7 practical techniques that reduce costs while improving speed.
How Token Consumption Works
Claude Code includes the entire conversation history in context. The longer the session, the more tokens each turn consumes. Controlling this consciously is the first step to cost reduction.
Technique 1: Compact the context with /compact
The most basic move. When the conversation grows, run /compact.
> /compact
It summarizes the past conversation and dramatically reduces context size. The context is preserved in summary form, so you can keep working without breaking flow.
Rule of thumb: Run /compact once after 50 turns.
Technique 2: /clear when no longer needed
When a task fully switches, use /clear instead of /compact.
> /clear
Wipes history completely and starts from scratch. No previous task context remains, so you can focus on the new one.
See also Claude Code Getting Started Guide.
Technique 3: Shorten prompts with CLAUDE.md
Repeating the same setup in every prompt is a token waste. Put project info into CLAUDE.md and your instructions get shorter.
# CLAUDE.md
## Project Overview
TypeScript + Next.js 15 e-commerce site. Tailwind CSS, Prisma + PostgreSQL.
## Coding Conventions
- Use async/await (avoid Promise.then)
- Use absolute imports (@/...)
- Tests in Vitest
## Common Commands
- npm run dev
- npm run test
- npm run build
Just having this cuts each prompt to about 1/3 of its previous length. See CLAUDE.md Best Practices for more.
Technique 4: Pass only what’s needed via pipe
Don’t have Claude Code read the entire file. Pipe only the relevant slice.
# Bad: pass the entire file
claude -p "Improve formatDate in src/utils/helpers.ts"
# Good: extract just the function
sed -n '/^export function formatDate/,/^}/p' src/utils/helpers.ts | claude -p "Improve this function"
Same with error logs.
# Pass only the last 100 lines
tail -n 100 production.log | claude -p "Identify the cause of errors"
Technique 5: Save main context with subagents
Delegate long research and large file exploration to subagents.
Spawn 3 subagents in parallel and investigate:
1. List of endpoints in src/api/
2. Main components in src/components/
3. Coverage status in tests/
Each agent should return a summary.
Only summaries return to the main context, saving significant tokens. See Subagent Activation Patterns for more.
Technique 6: Choose the right model for the job
Switch models based on task complexity.
# Simple work with Haiku
claude --model claude-haiku-4-5 -p "Translate the comments in this file"
# Complex design with Sonnet
claude --model claude-sonnet-4-5 -p "Redesign this architecture"
# Critical tasks with Opus
claude --model claude-opus-4-6 -p "Diagnose the root cause of this performance issue"
Haiku runs at about 1/4 the cost of Sonnet, so using it for simple tasks delivers serious savings. See Claude Code Pricing Guide for more.
Technique 7: Check /cost frequently
Run /cost mid-session to see current token usage and cost.
> /cost
Total tokens: 45,231
Estimated cost: $0.42
When you notice “wait, that’s more than I expected”, reset immediately with /compact or /clear.
Daily Workflow Example
A daily routine combining all of these.
# Morning: start a new task
$ claude
> Read CLAUDE.md and confirm today's tasks
# Mid-day: after 50 turns
> /compact
# Noon: switch to a different task
> /clear
> Starting refactoring on a different feature
# Evening: check costs
> /cost
> /compact
# Night: continue tomorrow
> Use /resume to pick up where you left off
Conclusion
- Reset context periodically with
/compactand/clear - Shorten prompts with CLAUDE.md
- Pipe only the relevant data
- Parallelize and save context with subagents
- Match the model to the task
- Check
/costfrequently
Making these habits cuts monthly costs in half or better. See the Anthropic official docs for further best practices.
Level up your Claude Code workflow
50 battle-tested prompt templates you can copy-paste into Claude Code right now.
Free PDF: Claude Code Cheatsheet in 5 Minutes
Just enter your email and we'll send you the single-page A4 cheatsheet right away.
We handle your data with care and never send spam.
About the Author
Masa
Engineer obsessed with Claude Code. Runs claudecode-lab.com, a 10-language tech media with 2,000+ pages.
Related Posts
How to Navigate Large Codebases Fast with Claude Code
Even 100K+ line projects can be understood in minutes with Claude Code. Directory analysis, dependency mapping, key file identification and more.
5 Debugging Techniques to Crush Errors with Claude Code
Just paste the error message and Claude Code identifies the cause and fixes it. Five practical debugging techniques that eliminate Stack Overflow searches.
Creating Custom Slash Commands in Claude Code — Tailor Your Workflow
Learn how to create custom slash commands in Claude Code. Covers file placement, arguments, and automating frequent tasks with practical code examples.