feat(agent): smart model routing (pre-turn task-difficulty router) - #20327
Closed
crogers2287 wants to merge 1 commit into
Closed
crogers2287 wants to merge 1 commit into
crogers2287 wants to merge 1 commit into
Conversation
Adds a deterministic, turn-scoped router that picks the right model before each user turn starts. Local-first mode escalates hard tasks to a smart cloud model; smart-primary mode demotes simple tasks to a cheap local model. Auto-detects mode from config and primary provider. The decision module (agent/smart_model_routing.py) is pure and tested in isolation. The runtime swap in AIAgent mirrors the existing fallback client/api_mode/cache bookkeeping and reuses _restore_primary_runtime for turn-scoped restoration. When smart_model is omitted, the first fallback_providers entry is used, so a single config can drive both provider failover and difficulty escalation. Disabled by default and on bad config; existing fallback semantics are preserved. Tests: 48 passing (39 unit + 9 runtime). No HTTP calls, no local model endpoints touched. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Contributor
This was referenced May 7, 2026
Collaborator
|
Closing as superseded by #12732. Triage notes (medium confidence): Thanks for the contribution — the underlying problem this PR addresses has been resolved by the linked PR on current main. If you believe this was closed in error, please comment and we'll reopen. (Bulk-closed during a CLI PR triage sweep.) |
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a deterministic, turn-scoped router that picks the right model before each user turn starts, complementing the existing fallback chain (which handles provider failure mid-turn). Two modes, auto-detected from config:
When
smart_modelis omitted, the firstfallback_providersentry is used as the smart target so a single config can drive both provider failover and difficulty escalation.Design
agent/smart_model_routing.py— heuristic classifier (length / keywords / phrases / code-fence / traceback / multi-question), mode resolution,decide_route(). No network, no SDK imports.AIAgent._maybe_apply_smart_routing()runs at the top ofrun_conversation()after_restore_primary_runtime(). Inline runtime swap mirrors the bookkeeping in_try_activate_fallback(resolve client, determine api_mode, refresh prompt-cache + context engine), and sets_fallback_activated=Trueso the next turn restores primary the same way fallback does.smart_model_routingconfig block with the same provider/model/base_url/api_key shape as fallback entries. Disabled by default; bad config logs and stays on primary.cli.py(both AIAgent construction sites) andgateway/run.py(_load_smart_model_routing()+ both construction sites).Config
```yaml
smart_model_routing:
enabled: true
mode: auto # auto | local-first | smart-primary
max_simple_chars: 160
max_simple_words: 28
cheap_model:
provider: custom
model: fast-local-model
base_url: http://fred:9069/v1
api_key: local
smart_model:
provider: openrouter # if omitted, fallback_providers[0] is used
model: anthropic/claude-sonnet-4
extra_hard_keywords: [permitting, rezoning]
```
Mode auto-detection (when `mode: auto` or unset):
Test plan
Docs
Risks / follow-ups
🤖 Generated with Claude Code