Claude Code in Practice (5): Model Mix Strategy

Claude Code in Practice (5): Model Mix Strategy
Tests with Haiku, refactoring with Sonnet, architecture with Opus. Learn how to optimize both cost and quality by selecting the right model for each task.
TL;DR
- Haiku: Fast and cheap → Tests, simple fixes, code search
- Sonnet: Balanced → General development, refactoring, debugging
- Opus: Best performance → Architecture design, complex problem solving
- Strategy: Dynamically switch models based on task complexity
1. Why Model Mix?
The Problem with Single Models
Using Opus for everything:
- 10x cost increase
- Excessive latency for simple tasks
- Overkill performance for most work
Using Haiku for everything:
- Quality degradation on complex tasks
- Mistakes in architectural decisions
- More time spent on repeated corrections
The Optimal Strategy
Choose the model based on task complexity:
Low → Haiku: Tests, lint fixes, code search, documentation
Medium → Sonnet: Feature development, bug fixes, refactoring, API implementation
High → Opus: Architecture design, large refactoring, complex algorithms, security review
2. Model Characteristics
Claude 3.5 Haiku
Suitable tasks:
- Writing test code
- Fixing type errors
- Fixing lint errors
- Simple code generation
- Codebase search
- Documentation/comments
Claude 3.5 Sonnet
Suitable tasks:
- New feature implementation
- Bug fixes
- Code refactoring
- API endpoint development
- Component development
- Debugging
Claude 3.5 Opus
Suitable tasks:
- System architecture design
- Large-scale refactoring planning
- Complex algorithm implementation
- Security vulnerability analysis
- Performance optimization strategy
- Technical decision making
3. How to Switch Models
In CLI
# Set default model
claude config set model sonnet
# Switch during session
/model haiku
/model sonnet
/model opusConfiguration File
`~/.claude/settings.json`
{
"model": "sonnet",
"modelOverrides": {
"test": "haiku",
"architecture": "opus"
}
}Hints in Prompts
Tasks sufficient for Haiku:
- "Add unit tests to this function"
- "Fix the type errors"
- "Just fix the lint errors"
Tasks suitable for Sonnet:
- "Implement login functionality"
- "Find and fix this bug"
- "Refactor this component"
Tasks requiring Opus:
- "Design a microservices architecture"
- "Redesign the entire auth system"
- "Analyze performance bottlenecks and create optimization strategy"
4. Model Mapping by Task
Writing Tests (Haiku)
/model haiku"Add tests for the ProductCard component"
Why Haiku is suitable:
- Test patterns are repetitive
- Written by referencing existing code
- Fast feedback is important
Feature Development (Sonnet)
/model sonnet"Implement a user profile page. Reference the existing UserCard component, include profile image upload functionality."
Why Sonnet is suitable:
- Requires modifying multiple files
- Extends while following existing patterns
- Requires appropriate judgment
Architecture Design (Opus)
/model opus"I want to split our current monolithic structure into microservices. Design which services to split into, what communication method to use, and how to maintain data consistency."
Why Opus is suitable:
- Complex tradeoff analysis
- Long-term impact consideration
- Creative problem solving needed
5. Practical Workflows
Bug Fix Flow
- Analyze logs with Haiku:
/model haiku→ "Analyze this error log" - Fix bug with Sonnet:
/model sonnet→ "Fix the bug based on the analysis" - Add tests with Haiku:
/model haiku→ "Add tests for the fixed part"
New Feature Development Flow
- Design with Opus:
/model opus→ "Design a payment system. Consider: PCI-DSS, idempotency, retries" - Implement with Sonnet:
/model sonnet→ "Implement PaymentService according to the design" - Test with Haiku:
/model haiku→ "Write tests for PaymentService"
Refactoring Flow
- Strategy with Opus:
/model opus→ "Create a strategy for refactoring this legacy code" - Execute step-by-step with Sonnet:
/model sonnet→ "Step 1: Clean up dependencies" → "Step 2: Extract functions" → "Step 3: Add tests" - Clean up with Haiku:
/model haiku→ "Fix lint errors and format"
6. Cost Optimization Strategy
Cost Comparison (Example)
Monthly Cost Simulation
Single model (Sonnet only): Total $12.00/month
- 100 tests: $5.00
- 20 features: $3.00
- 50 debug sessions: $2.50
- 5 architecture tasks: $1.50
Model mix: Total $11.50/month + Better architecture quality
- 100 tests (Haiku): $1.00
- 20 features (Sonnet): $3.00
- 50 debug sessions (Sonnet): $2.50
- 5 architecture tasks (Opus): $5.00
7. Automation Strategy
Auto-Switch with Hooks
`.claude/settings.json`
{
"hooks": {
"PreToolCall": [
{
"matcher": "Edit|Write",
"pattern": "*.test.ts|*.spec.ts",
"command": "claude model set haiku",
"description": "Use Haiku for test files"
},
{
"matcher": "Edit|Write",
"pattern": "**/architecture/**|**/design/**",
"command": "claude model set opus",
"description": "Use Opus for architecture docs"
}
]
}
}Guidelines in CLAUDE.md
Add a model selection guide to your CLAUDE.md:
Use Haiku: Writing/modifying test code, fixing type errors, fixing lint errors, code search and analysis
Use Sonnet (Default): New feature implementation, bug fixes, refactoring, API work
Use Opus: Architecture design, large-scale change planning, security review, performance optimization strategy
8. Prompt Tips by Model
Optimizing for Haiku
Good prompt: "Add tests for the getUser method in UserService.test.ts. Follow the existing test patterns."
Bad prompt: "Create a testing strategy and design a comprehensive test suite"
Key: Specific and narrow scope tasks
Optimizing for Sonnet
Good prompt: "Implement shopping cart functionality. Manage state with CartContext, persist to LocalStorage, add 'Add to Cart' button to existing ProductCard"
Bad prompt: "Make a shopping cart" (too vague)
Key: Clear context and requirements
Optimizing for Opus
Good prompt: "Design a payment system. Current situation: Monolithic Next.js app, PostgreSQL, 100k transactions/month expected. Considerations: PCI-DSS compliance, payment failure retries, refund processing. Questions: Which payment gateway? How to manage transaction logs? How to test?"
Bad prompt: "Implement payments" (jumping to implementation without design)
Key: Provide context, constraints, and open questions
9. Decision Guide
When a task arrives, follow this decision order:
- Simple fix/search? → Yes: Use Haiku
- Architecture/design? → Yes: Use Opus
- Complex algorithm? → Yes: Use Opus
- Everything else → Sonnet (default)
Simple Rules
Conclusion
Model mix isn't just about cost savings.
It's a strategy for selecting the optimal tool for each task.
We hope this series helps you unlock the true potential of Claude Code.
Series Complete
- Context is Everything - Improve project understanding
- Automating Workflows with Hooks - Set up quality gates
- Building Team Standards with Custom Skills - Share team knowledge
- Building MCP Servers - External system integration
- Model Mix Strategy (This post) - Optimize cost and quality