nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Feature] Smart Auto-Routing (Simple vs Strong Models)

Open akramcodez opened this issue 3 months ago • 1 comments

Description

Implement a cascading model strategy that auto-routes trivial or tool-only turns (simple reads, formatting checks) to a small/fast/cheap model, escalating to the user's "main" strong model only when task complexity warrants it.

Use Case

Saves cost and reduces latency on easy tasks (like view_file) without sacrificing quality on hard reasoning tasks.

Proposed Solution

Create source/ai-sdk-client/smart-router.ts with a classifier based on prompt length, code-block count, etc. Add a /smartroute command to toggle this behavior.

Alternatives Considered

Forcing users to manually swap models (which creates friction).

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] This feature aligns with the project's goals (local-first AI assistance)

akramcodez avatar Aug 18 '26 06:08 akramcodez

@akramcodez @will-lamerton I'd like to take this. #908 was closed for inactivity on Oct 2 and I can't see anyone on it since. I've read both reviews on #908, and my plan is built around them:

Routing (strong by default)

  • A turn goes to the simple model only if the whole message is trivial: short, no imperative verb, no file path or code, no tool intent. A trivial word inside a longer request ("check", "find", "list", "ok", "no") doesn't count.
  • Confirmations ("yes", "ok, do it") and follow-ups ("no, that broke the build") always go to the strong model, because they run the full tool loop on the whole history.
  • If the classifier can't decide, it picks strong.

Model selection

  • No fallback to availableModels[0]. The simple model comes from config or /smartroute model <name>. Auto-pick only considers chat models, and if none qualifies, routing stays off with a message saying why.
  • The simple model is checked before use: it must support tools when the turn has tools, and it must have a usable context window for the current history. If it fails, the turn goes to strong.

State (no mutating the shared client)

  • LLMClient.chat() takes the turn's model per call, so setModel isn't touched and there's nothing to restore.
  • Everything that depends on the model for that turn uses the routed model: the system prompt, getAvailableToolNames, the toolsDisabled native/XML choice, buildResponseUsageBounded pricing, and the context size.
  • Each response shows which model answered when routing changed it.

Scope and tests

  • TUI only. Plain, ACP, subagents and the daemon don't change, and the PR description will say so.
  • Tests:
    • your realistic prompts from #908, as regression cases that must go to strong
    • option tests where the option actually flips the result
    • the /smartroute subcommands
    • preferences persistence
    • a turn-level test that the prompt, tools, toolsDisabled and pricing all use the routed model
  • Plus docs/features/commands.md and a changeset.

Would you rather I start from @atulya-srivastava's branch and credit them, or start fresh? I'll wait for assignment before writing code.

macjayz avatar Nov 05 '26 15:11 macjayz