Skip to content

Conversation

@benzntech
Copy link
Contributor

@benzntech benzntech commented Jan 23, 2026

What This Does

When Claude uses the switchMode or newTask tools to automatically delegate work to a different mode, the CLI now automatically switches to that mode's preferred AI provider and model.

Previously, only manual /mode commands would switch the provider/model. Now automatic mode switching works the same way.

Why This Matters

If you configure modes with specific providers/models (e.g., use GPT-4 for planning, Claude for coding), the CLI will now respect those preferences even when the LLM switches modes automatically during a task.

Before: LLM switches to "planning" mode → CLI stays on current provider
After: LLM switches to "planning" mode → CLI switches to planning mode's preferred provider/model

Changes Made

File: cli/src/state/hooks/useApprovalMonitor.ts

Added a new effect that:

  • Watches for switchMode and newTask tool approvals (both auto and manual)
  • Looks up the target mode's preferredProviderId and preferredModelId
  • Switches the CLI's active provider/model to match
  • Handles errors gracefully without blocking tool execution

How to Use

  1. Configure modes with preferred providers in custom_modes.yaml:
modes:
  - name: "Code Review"
    slug: "code-review"
    preferredProviderId: "anthropic-1"
    preferredModelId: "claude-opus-4"
  - name: "Quick Tasks"
    slug: "quick-tasks"
    preferredProviderId: "openai-1"
    preferredModelId: "gpt-4o-mini"
  1. Start a task in any mode
  2. When the LLM delegates to another mode, the provider/model switches automatically
  3. Verify with /provider command

Testing

  • ✅ Build successful
  • ✅ All 2076 tests passing
  • ✅ Type checking passed
  • ✅ Linting passed

Technical Details

The implementation uses a separate useEffect hook that monitors lastAskMessage for answered tool calls. This approach:

  • Covers both auto-approval and manual approval paths
  • Avoids duplicating logic across multiple code paths
  • Uses de-duplication to prevent multiple switches for the same tool call
  • Includes memory leak prevention (cleans up old timestamps)

When the LLM uses switchMode or newTask tools (automatic delegation),
the CLI now switches the provider/model based on the target mode's
preferredProviderId/preferredModelId configuration.

Changes:
- Add useEffect in useApprovalMonitor.ts to watch for answered
  switchMode/newTask tools
- Switch provider via selectProviderAtom when preferredProviderId is set
- Switch model via updateProviderAtom when preferredModelId is set
- Handle both mode slug (switchMode) and name (newTask) lookups
- Add cleanup for modeToolHandledRef to prevent memory leaks

This completes the integration between automatic mode switching and
provider/model configuration, matching the behavior of manual /mode
command switching.
@changeset-bot
Copy link

changeset-bot bot commented Jan 23, 2026

⚠️ No Changeset found

Latest commit: bde4e22

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@benzntech benzntech changed the title Add automatic provider/model switching for mode delegation CLI: Auto-switch provider/model when LLM delegates to different modes Jan 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant