Lesson 29
Cost Optimization
AI-generated
Cost Optimization
Understand how Claude Code pricing works
Monitor costs with /cost and the status line
Choose the right model for each task
Reduce context usage without losing effectiveness
Build cost-aware workflows
Claude Code uses API tokens for every interaction. More complex tasks, longer sessions, and larger contexts cost more. Understanding the cost model helps you work efficiently and avoid surprise bills.
This lesson covers pricing basics, monitoring tools, and practical strategies for cost optimization.
Claude Code is billed through the Anthropic API by tokens:
Input tokens: What Claude reads (your prompts, context, file contents)
Output tokens: What Claude generates (responses, code, commands)
Different models have different rates:
Opus - Most capable, highest cost
Sonnet - Balanced capability and cost
Haiku - Fast and cheap, good for simple tasks
Output tokens typically cost more than input tokens per unit.
What consumes tokens:
Every message you send
Every file Claude reads
CLAUDE.md context loaded each turn
Previous conversation history
Tool outputs Claude receives
Claude's responses and reasoning
The /cost command:
Shows token usage for your current session:
Total input tokens
Total output tokens
Estimated cost in dollars
Breakdown by turn (optional)
Run this periodically to track usage.
Status line:
Configure the status line to show context usage:
A visual indicator helps you notice when context is filling up.
Session review:
Running compact shows you context usage before and after optimization.
Different tasks need different models:
Use Opus for:
Complex architectural decisions
Multi-step refactors across many files
Tasks requiring deep reasoning
When quality matters more than speed
Use Sonnet for:
Most daily coding tasks
Feature implementation
Bug fixes
Code review
Use Haiku for:
Simple questions
Quick lookups
Repetitive operations
CI/CD automation
When speed matters most
Switching models:
Or set environment variable:
Cost comparison:
Haiku can be 10-20x cheaper than Opus for simple tasks. Use the cheapest model that can do the job well.
Context is the biggest cost driver. Here is how to minimize it:
Compact sessions:
Claude summarizes and clears old conversation history. Do this regularly in long sessions.
Focused prompts:
Instead of:
Try:
Narrow scope means less context.
Specific file references:
Instead of:
Try:
Claude reads only what it needs.
Fresh sessions:
Start new sessions for unrelated tasks. Do not carry context you do not need.
Clear context before big operations:
Then:
Starting clean reduces baseline cost.
How you prompt affects cost:
Be specific:
Vague prompts cause Claude to explore broadly, reading more files and trying more approaches.
One thing at a time:
Break large tasks into focused steps.
Provide context in prompts:
If you already know relevant files:
Claude skips exploration.
Ask for plans before execution:
Review the plan, then execute. Catches wrong directions early.
Effort levels control how much Claude explores:
Minimal exploration
Quick responses
Lower cost
Moderate exploration (default)
Balanced speed and thoroughness
Deep exploration
Most thorough
Higher cost
Use low effort for well-defined tasks. Use high effort when you need deep analysis.
The /batch skill can be cost-efficient for repetitive tasks:
Without batch:
Each interaction has overhead.
With batch:
One planning phase, parallel execution. Often more cost-effective for 5+ files.
However:
Batch spawns subagents, which have startup costs. For 2-3 files, direct execution may be cheaper.
Research then execute:
Use Haiku or low effort to research
Switch to Sonnet for implementation
Use Opus only for critical decisions
Progressive refinement:
Get a rough implementation fast
Refine specific areas as needed
Less thorough initial pass, then focused improvements.
Cache your knowledge:
Add findings to CLAUDE.md so Claude does not have to rediscover them:
Claude reads this instead of exploring.
Session management:
Named, compacted sessions are cheap to resume.
Check usage regularly:
The Anthropic console shows API usage and costs. Review weekly.
Set spending limits:
The Anthropic console lets you set budget caps. This prevents runaway costs.
Track by project:
Use different API keys for different projects if you need cost attribution.
CI budgets:
For automated tasks, set timeouts and consider using Haiku:
Claude Code costs depend on input tokens, output tokens, and model choice
Monitor costs with /cost and status line context indicators
Use Haiku for simple tasks, Sonnet for daily work, Opus for complex reasoning
Compact sessions regularly to reduce context costs
Write specific, focused prompts to minimize exploration
Use effort levels to control depth of work
Add project knowledge to CLAUDE.md to avoid rediscovery
Set spending limits in the Anthropic console
Practice cost-aware usage:
Start a new Claude Code session.
Run /cost to see baseline usage (should be near zero).
Ask Claude to explain a complex part of your codebase.
Run /cost again and note the increase.
Run /compact to reduce context.
Run /cost and notice the reduction.
Try /model haiku and ask a simple question.
Compare the cost to previous queries.
Configure your status line to show context: /statusline show context
Watch the indicator as you work.
This exercise builds cost awareness.
https://code.claude.com/docs/en/costs - Pricing and cost documentation
https://code.claude.com/docs/en/models - Model selection guide
https://code.claude.com/docs/en/context - Context management