[FEATURE] Programmatic Model Switching for Autonomous Agents

Resolved 💬 8 comments Opened Jan 12, 2026 by martiendejong Closed May 17, 2026

Problem Statement

Autonomous agents need the ability to programmatically switch between Claude models at runtime based on task complexity and cost optimization. Currently, agents are limited to a single model for the entire session.

Use Case Example:
An agent should be able to:

  1. Use Claude Opus for complex planning and architecture decisions
  2. Use Claude Sonnet for code reviews and medium-complexity tasks
  3. Use Claude Haiku for straightforward edits, repetitive tasks, and production code execution (cost optimization)
  4. Switch models without persisting changes to user settings
  5. Make these decisions autonomously based on task requirements

Proposed Solution

Add a mechanism for agents to switch models programmatically at runtime:

Option 1: CLI Command via Bash

claude-code /model haiku --session-only
claude-code /model opus --session-only

Agents could invoke this via Bash to change model for subsequent API calls in the current session.

Option 2: Environment Variable
Expose an environment variable that agents can set:

export CLAUDE_CODE_MODEL=haiku

Option 3: API/Function Call
Expose a direct function/API that changes the active model for the current session:

set_model('haiku', persist=False)

Why This Matters

  1. Cost Optimization - Haiku is significantly cheaper; agents should use it for most tasks
  2. Intelligent Resource Allocation - Complex tasks deserve better models, simple tasks don't
  3. Performance - Haiku is faster; agents could use it for time-sensitive operations
  4. Production Readiness - Agents need fine-grained control over model selection

Current Workaround

The only workaround is to exit and restart the session with a different model, which breaks continuity and is operationally expensive.

Priority

High - This is essential for production autonomous agent workflows where cost and performance matter.

Additional Context

The Claude Code system is being used to power self-improving agents that work autonomously across multiple repositories. These agents need intelligent model switching to:

  • Use powerful models (Opus) for architecture and complex decisions
  • Use cost-efficient models (Haiku) for execution and simple operations
  • Avoid unnecessary use of expensive models on routine tasks

This feature would unlock significant productivity and cost efficiency gains for agent-based automation.

---

Related Issues:

  • #12645 - Mid-session model switching (UI-focused)
  • #15721 - Automatic model switching for plan mode
  • #11548 - Quick model switching

View original on GitHub ↗

This issue has 8 comments on GitHub. Read the full discussion on GitHub ↗