Cost Optimization Intermediate
Understanding how Claude Code costs work and applying smart optimization strategies can reduce your spending by 50% or more without sacrificing quality. Learn token economics, model selection, and budget management.
Token Usage Breakdown
Every Claude Code interaction involves two types of tokens:
| Token Type | What It Includes | Cost Impact |
|---|---|---|
| Input tokens | Your prompt, CLAUDE.md, file contents read, command outputs, conversation history | Lower cost per token |
| Output tokens | Claude's responses, code it generates, explanations | Higher cost per token (3-5x input) |
/compact saves so much money.
Model Selection Impact
| Model | Best For | Relative Cost | Quality |
|---|---|---|---|
| Claude Sonnet | Routine tasks, simple fixes, code generation | $ | Very Good |
| Claude Opus | Complex reasoning, architecture, tricky bugs | $$$$ | Excellent |
# Use Sonnet for routine tasks (default) $ claude -p "Add input validation to the signup form" # Switch to Opus for complex tasks $ claude -p "Redesign the caching layer to support Redis Cluster" --model claude-opus-4-20250514 # In interactive mode, switch models mid-session > /model claude-opus-4-20250514
Cost-Saving Strategies
1. Use /compact Regularly
The /compact command summarizes the conversation, dramatically reducing input tokens for subsequent turns:
# After 10 turns of conversation (~50K tokens accumulated) > /compact # Conversation is summarized to ~5K tokens # Next 10 turns cost 90% less in input tokens!
2. Write Shorter, More Specific Prompts
# Expensive: vague prompt causes Claude to search and read many files > I think there's a bug somewhere in the auth system, can you find it? # Cheap: direct prompt, Claude goes straight to the fix > Fix the null check on line 45 of src/auth/session.ts - it should check for undefined too
3. Use -p for Quick Tasks
One-shot mode has zero conversation overhead - no history accumulation:
# Each call is independent - no growing context $ claude -p "Fix the typo on line 12 of README.md" $ claude -p "Add a .gitignore entry for .env.local" $ claude -p "Remove the unused import on line 3 of src/app.ts"
4. Leverage Caching
Claude Code benefits from prompt caching when conversation prefixes are reused. This happens automatically - your CLAUDE.md content and early conversation history are cached across turns.
5. Monitor with /cost
# Check current session cost > /cost # Output example: # Session cost: $0.47 # Input tokens: 125,430 # Output tokens: 8,210 # Cache hits: 89,200 tokens (saved ~$0.25)
Cost Comparison: Task Types
| Task | Estimated Cost | Optimization |
|---|---|---|
Quick fix (1 file, -p) |
$0.01 - $0.05 | Already optimal |
| Feature implementation (5-10 files) | $0.20 - $1.00 | Use specific file paths, Sonnet model |
| Large refactoring (20+ files) | $1.00 - $5.00 | /compact regularly, break into sessions |
| Full codebase review | $2.00 - $10.00 | Review in sections, use Sonnet |
| Architecture planning | $0.50 - $3.00 | Use Opus only for the planning phase |
Monthly Cost Estimation
| Usage Pattern | Estimated Monthly Cost |
|---|---|
| Light (5-10 quick tasks/day) | $10 - $30 |
| Moderate (20+ tasks/day, mixed complexity) | $50 - $150 |
| Heavy (all-day usage, complex projects) | $150 - $500 |
| Team (5 developers, moderate usage) | $250 - $750 |
Setting Budget Alerts
Set up monitoring to prevent unexpected costs:
- Check the Anthropic Console for usage dashboards and spending limits
- Set monthly spending limits on your API key
- Use
/costat the end of each session to track trends - Create a simple tracking script that logs daily costs
Try It Yourself
Check your current session cost with /cost and try using /compact to see the impact. Try using Sonnet for your next few routine tasks. Next: mastering CLAUDE.md files.
Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX