Ops & Systems

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.

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

AttributeValue
SpeedFastest
CostCheapest
StrengthSimple tasks, fast responses
WeaknessComplex reasoning limitations

Suitable tasks:

  • Writing test code
  • Fixing type errors
  • Fixing lint errors
  • Simple code generation
  • Codebase search
  • Documentation/comments

Claude 3.5 Sonnet

AttributeValue
SpeedFast
CostMedium
StrengthBalanced performance
WeaknessExtremely complex tasks

Suitable tasks:

  • New feature implementation
  • Bug fixes
  • Code refactoring
  • API endpoint development
  • Component development
  • Debugging

Claude 3.5 Opus

AttributeValue
SpeedModerate
CostHighest
StrengthComplex reasoning, creativity
WeaknessCost, speed

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

bash
# Set default model
claude config set model sonnet

# Switch during session
/model haiku
/model sonnet
/model opus

Configuration File

`~/.claude/settings.json`

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

  1. Analyze logs with Haiku: /model haiku → "Analyze this error log"
  2. Fix bug with Sonnet: /model sonnet → "Fix the bug based on the analysis"
  3. Add tests with Haiku: /model haiku → "Add tests for the fixed part"

New Feature Development Flow

  1. Design with Opus: /model opus → "Design a payment system. Consider: PCI-DSS, idempotency, retries"
  2. Implement with Sonnet: /model sonnet → "Implement PaymentService according to the design"
  3. Test with Haiku: /model haiku → "Write tests for PaymentService"

Refactoring Flow

  1. Strategy with Opus: /model opus → "Create a strategy for refactoring this legacy code"
  2. Execute step-by-step with Sonnet: /model sonnet → "Step 1: Clean up dependencies" → "Step 2: Extract functions" → "Step 3: Add tests"
  3. Clean up with Haiku: /model haiku → "Fix lint errors and format"

6. Cost Optimization Strategy

Cost Comparison (Example)

TaskHaikuSonnetOpus
10 tests$0.01$0.05$0.20
1 feature$0.05$0.15$0.50
Architecture$0.10$0.30$1.00

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`

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:

  1. Simple fix/search? → Yes: Use Haiku
  2. Architecture/design? → Yes: Use Opus
  3. Complex algorithm? → Yes: Use Opus
  4. Everything else → Sonnet (default)

Simple Rules

Task TypeModelReason
`*.test.*` filesHaikuPattern repetition
Lint/type errorsHaikuSimple fixes
Code searchHaikuFast response
New featuresSonnetBalance
Bug fixesSonnetAnalysis needed
RefactoringSonnetContext understanding
ArchitectureOpusComplex reasoning
Security reviewOpusDeep analysis
Performance optOpusTradeoffs

Conclusion

Model mix isn't just about cost savings.

It's a strategy for selecting the optimal tool for each task.

PrincipleApplication
Simple tasks fastHaiku
General tasks balancedSonnet
Complex tasks carefullyOpus

We hope this series helps you unlock the true potential of Claude Code.

Series Complete

  1. Context is Everything - Improve project understanding
  2. Automating Workflows with Hooks - Set up quality gates
  3. Building Team Standards with Custom Skills - Share team knowledge
  4. Building MCP Servers - External system integration
  5. Model Mix Strategy (This post) - Optimize cost and quality