Claude Code Cost Optimization: Model Selection and Usage Monitoring
I was burning through my Claude Code budget way too fast. After a week of heavy usage, I realized I’d spent $50 on tasks that could have cost $10. The culprit? Using Opus for everything—writing comments, formatting code, and simple refactors.
Let me show you how I fixed this.
The Problem: Wrong Model, Wrong Task
I started using Claude Code with the default Opus model for everything. It felt great—Opus is incredibly smart. But then I checked my costs:
/costThe result shocked me. Simple tasks like adding comments to a function were costing 10x more than necessary.
The Solution: Match Model to Task Complexity
The key insight: not every task needs Opus-level reasoning.
Here’s the mental model I developed:
Task Complexity Spectrum========================
Simple ───────────────────────────────────────────────── Complex
Haiku Sonnet Sonnet Opus Opus Opus │ │ │ │ │ │ ▼ ▼ ▼ ▼ ▼ ▼Comments Bug Feature Multi- Archi- ComplexFormat Fixes Dev file tecture Debug Simple Refactor
Cost: Low ──────────────────────────────────────► HighSpeed: Fast ────────────────────────────────────► SlowModel Selection Commands
Switching models is straightforward:
# Interactive model selection/model
# Shows available models and lets you pick# Options typically include:# - claude-3-5-haiku (fast, cheap)# - claude-3-5-sonnet (balanced)# - claude-4-opus (most capable)I created a simple rule for myself:
| Task Type | Model | Why |
|---|---|---|
| Writing comments | Haiku | No complex reasoning needed |
| Formatting code | Haiku | Pattern matching task |
| Simple bug fixes | Sonnet | Moderate debugging |
| Feature implementation | Sonnet | Balanced capability |
| Multi-file refactors | Opus | Needs context tracking |
| Architecture decisions | Opus | Deep reasoning required |
| Complex debugging | Opus | Full analysis needed |
Monitoring Costs: Two Essential Commands
Claude Code provides two commands for tracking usage:
/cost - Actual Billing
/costThis shows:
- Current session cost
- Total monthly spend
- Token breakdown (input/output)
Use this to understand your actual spending.
/usage - Rate Limits
/usageThis shows:
- Remaining quota before rate limits
- Current tier status
- Reset time if limited
Use this when you hit rate limits or want to avoid them.
Trial and Error: Learning the Hard Way
I tried several approaches before finding the right balance:
Attempt 1: Always Use Haiku
I switched to Haiku for everything. Bad idea. Complex refactors produced incorrect code that I had to fix manually. The “savings” were eaten up by my time correcting mistakes.
Attempt 2: Always Use Opus
Back to Opus for everything. My costs tripled again.
Attempt 3: Strategic Selection (The Winner)
Finally, I developed a workflow:
# Start session with Sonnet (good default)/model claude-3-5-sonnet
# For simple tasks, stay with Sonnet or switch to Haiku# Example: "Add JSDoc comments to all functions in utils.ts"
# For complex tasks, upgrade to Opus/model claude-4-opus# Then: "Refactor the authentication system to use JWT"
# Always check cost periodically/costAlternative Models: Significant Savings
For even more savings, consider alternative models like Kimi K2.5 or GLM-4.7:
# Configure Kimi for routine workexport ANTHROPIC_BASE_URL=https://api.moonshot.cn/anthropicexport ANTHROPIC_MODEL=kimi-k2.5I tested these alternatives on my daily tasks:
| Task | Opus | Kimi K2.5 | GLM-4.7 |
|---|---|---|---|
| Code comments | ✓ | ✓ | ✓ |
| Bug fixes | ✓ | ✓ | ✓ |
| Feature dev | ✓ | ✓ | ~80% |
| Architecture | ✓ | ~70% | ~60% |
| Complex debug | ✓ | ~60% | ~50% |
For 60-70% of my daily work, the alternatives performed competitively at a fraction of the cost.
Cost-Saving Strategy Summary
Here’s my final workflow:
1. Start with Sonnet (balanced default) │ ├── Simple task? ──► Stay or switch to Haiku │ └── Complex task? ──► Switch to Opus │ └── Check cost every 30 min ──► /cost
2. Hit rate limit? ──► /usage ──► Wait or switch model
3. End of session? ──► Review /cost for learningWhen to Pay the Premium
Don’t be penny-wise and pound-foolish. Opus is worth the cost for:
- Architectural decisions: One wrong choice costs more than the tokens
- Complex debugging: Opus finds bugs others miss
- Security reviews: Don’t skimp on security
- Multi-file refactors: Context tracking matters
The Numbers Don’t Lie
After implementing this strategy:
| Metric | Before | After |
|---|---|---|
| Weekly cost | ~$50 | ~$15 |
| Task success rate | 95% | 94% |
| Time per task | Same | Same |
I reduced costs by 70% with negligible impact on quality.
Summary
In this post, I showed how to optimize Claude Code costs by matching models to task complexity. The key point is that Haiku/Sonnet are sufficient for most tasks, and you should reserve Opus for complex reasoning.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments