SLOPSHOPPER

Model Router

Routes each coding subtask to the cheapest Claude model and effort that can do it reliably, logs real token cost per subagent automatically, and learns routing…

panecommandtoaststatusprompttimer
v2.3.0MITupdated 2026-10-09DevstateBelgium/model-router
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · model-router
│ ┃ model-router ✕ › fix the failing auth test and add an audit log call │ ┃ This session │ ┃ No model requests yet. ⏺ Read(src/auth.ts) │ ┃ ⎿ Read 6 lines │ ┃ Last 7 days (ended sessions) ⏺ Update(src/auth.ts) │ ┃ Nothing recorded yet. ⎿ Added 2 lines, removed 1 line │ ⏺ Bash(bun test) │ ⎿ 3 pass, 1 fail │ │ ● Done. refresh now rejects expired claims and logs an audit event. │ │ ✻ Worked for 42s · done 4:20 PM │ │ › /router │ ⎿ model-router: No model requests recorded in this session yet. │ │ ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts

Draws

Pane · model-router
This session No model requests yet. Last 7 days (ended sessions) Nothing recorded yet.
README

model-router

A Claude Code plugin that routes each coding subtask to the cheapest Claude model and effort that can do it reliably. It measures what every subagent really costs and learns routing rules from verified runs.

Install (pick one)

1. From the marketplace (recommended, gets updates). At the Claude Code prompt:

/plugin install model-router --marketplace DevstateBelgium/model-router

Answer y to add the marketplace, then pick a scope. Or in two steps:

/plugin marketplace add DevstateBelgium/model-router
/plugin install model-router@model-router

2. Drop-in, zero commands. Clone this repo into ~/.claude/skills/model-router/ (Windows: %USERPROFILE%\.claude\skills\model-router\) and start a new session:

git clone https://github.com/DevstateBelgium/model-router ~/.claude/skills/model-router

Claude Code loads any folder there that has .claude-plugin/plugin.json as a plugin (model-router@skills-dir), including its agents and hooks.

3. Whole team, per repository. Commit this to the project's .claude/settings.json. Teammates get it after they trust the folder:

{
  "extraKnownMarketplaces": { "model-router": { "source": { "source": "github", "repo": "DevstateBelgium/model-router" } } },
  "enabledPlugins": { "model-router@model-router": true }
}

4. Ask Claude to install it, in any session. Say "install the plugin from https://github.com/DevstateBelgium/model-router". Claude follows INSTALL.md, which runs node scripts/install.mjs --live.

First run

The first time the plugin loads, it starts a short configuration run. In an interactive session the mod queues it automatically; in every other session the SessionStart hook tells Claude to run it on your first message. The run checks Node.js, Claude Code and gh, then asks one question: local-only, connect an existing knowledge repo, or create a new private one. Run it again any time with /model-router:setup.

Cloud sessions

A cloud session starts from a fresh clone of your repository, and Claude Code loads plugins only at session start. There are two ways to get model-router there:

  • Every session, from the start (recommended): add this line to the cloud environment's setup script. It runs before Claude Code launches and is cached with the environment, so everything (skill, agents, hooks, mod) is active from the first message: `` git clone --depth 1 https://github.com/DevstateBelgium/model-router ~/.claude/skills/model-router || true ``
  • In a session that is already running: ask Claude to install it (option 4). The installer's --live mode also writes plain copies of the skill and the five tiers. Claude Code watches ~/.claude/skills/ and ~/.claude/agents/ and picks up new files within seconds, so routing works in the same session. Only folders that existed when the session started are watched. Cloud sessions start with a skills folder but no agents folder, so there the tiers are written as forked skills (context: fork) that run as subagents with the same pinned model and effort. When even the skills folder is missing, the installer says so and Claude reads the skill file directly. Hooks, telemetry, the mod and /model-router:* commands need a plugin reload. Claude can't trigger that itself, and over a remote connection it's refused, so those parts start in the next session. The live copies delete themselves once the plugin loads.

Requirements: a recent Claude Code (tested on 2.1.292). Node.js 18+ on PATH for the hooks and scripts. Without Node.js the skill and agents still work, but automatic telemetry and push/pull are off. gh is needed only for push/pull.

How to use it

Start a task. Ask for the work as you normally would. The skill loads by itself when Claude splits multi-step work across subagents. To force it, start the request with /model-router (or /model-router:run when the mod is not loaded):

/model-router move the date helpers into utils/ and add tests

Ask "which model should do X?" to get a routing suggestion without running anything. Orchestration works best when the main session runs Claude Opus 5.5 at xhigh effort, because the orchestrator does the planning and verification.

Automatic mode. Type /model-router:start once, and every prompt you type after that is routed, without a prefix. The status line starts with auto · while it is on. /model-router:stop turns it off. It lasts for the current session only, so each new session starts with it off. Prompts that start with a slash command are left alone, and /model-router <task> keeps working as a one-off without changing the mode. The first routed prompt loads the skill; later prompts get a short reminder, so the skill text is not repeated every turn. Needs the mod.

What happens during a run. Claude splits the request into tasks, each with the files it may touch and an acceptance check. Each task goes to the cheapest tier that fits:

Task looks likeTier
Reading, searching, renames, boilerplate, running testsscout
Clear spec, 1 to 3 files, a test to passbuilder
Multi-file change, refactor, bug with an unknown causeengineer
Engineer task that failed oncesenior
Trade-off, security-sensitive logic, two failed attemptsarchitect

Independent tasks run in parallel. Claude checks every result itself (reads the diff, runs the check). A failed task is retried once with a sharper brief, then moved one tier up. Small edits that take less time to do than to brief are done directly, without a subagent.

See what it cost. Type /router for this session's cost per tier and model (needs the mod, Claude Code 2.1.287+). /model-router:stats 7 shows subagent cost over the last 7 days from telemetry, with or without the mod.

How it learns. After each verified task, Claude adds one judgment row to pending-runs.md: tier, model, effort, whether the check passed, and whether the tier was too strong or too weak. When a run teaches something new, the reply ends with a one-line Model-router: learned … note. Once five rows are pending, or one pattern repeats three times, Claude proposes routing rules in boundaries.md, marked (proposed). A rule needs at least three consistent rows, and it takes effect only after you push.

Share what it learned. With a knowledge repo configured (/model-router:setup):

  • /model-router:push sends pending rows, telemetry and proposed rules to the repo. It lists the proposed rules first, so you see what goes live.
  • /model-router:pull brings in what other machines and cloud sessions learned. The SessionStart summary tells you when the local copy is stale.

What you get

ComponentWhat it does
/model-router:run skillOrchestration rules: split work, pick a tier, verify, escalate, log. Loads when Claude delegates work.
5 tier agentsmodel-router:scout (Claude Haiku 5.5, medium), builder (Claude Sonnet 5.5, high), engineer (Claude Opus 5.5, medium), senior (Claude Opus 5.5, xhigh), architect (Claude Fable 5.1, high). Each pins its model and effort.
SessionStart hookSeeds the data dir on first run, then adds a short summary to context: active boundaries, pending rows, a stale-mirror warning, a model-check reminder.
PostToolUse + SubagentStop hooksLog every subagent: resolved model, tokens summed over the whole subagent transcript, duration, estimated USD. No model effort needed.
Mod (hooks/router.tsx)Runs inside Claude Code (needs v2.1.287+, otherwise ignored). Drops a model override on model-router:* spawns so the pinned model and effort always run. Meters every model request, orchestrator included, and shows router $… · subagents $… in the status line. Keeps per-session totals across sessions.
/routerMod command. Opens a pane with this session's cost per tier and model, plus the last 7 days. Answers instantly with no model turn, and also works in claude -p.
/model-router <task>Mod command. Short form of /model-router:run: forwards the task to the skill. Without the mod, use /model-router:run.
/model-router:start, /model-router:stopTurn automatic mode on or off for this session. The mod answers them directly, with no model turn.
/model-router:stats [days]Real cost per subagent run, from telemetry (works without the mod).
`/model-router:setup [owner/repo \local]`First-run configuration, also runnable any time: prerequisite check, then an optional private knowledge repo (created and seeded after confirmation).
scripts/install.mjsCross-platform installer. --live makes the skill and agents work in the running session, cloud sessions included.
/model-router:pull, /model-router:pushSync shared knowledge across machines and cloud sessions. Push detects conflicts instead of overwriting.

Data

State lives in ~/.claude/plugins/data/model-router-<origin>/ and survives plugin updates: mirror/ (copy of the knowledge repo), pending-runs.md (judgment rows), telemetry.jsonl (hook-written cost data), prices.json, state.json, and optional config.json.

The knowledge repo is optional. Set it in /config (model-router > Knowledge repo) or with /model-router:setup. Without one, everything runs locally.

Tests

  • bash tests/sync.test.sh runs push/pull end to end against a fake GitHub API (tests/fake-gh.mjs), so no network access is needed.
  • bash tests/install.test.sh checks the installer, the live copies, first-run state and cleanup against a fake home directory.
  • claude plugin test . runs the mod's tests (tests/router.test.ts).
  • claude plugin validate . --strict checks the manifests and lists what the mod hooks and calls.
  • Type-check the mod: load it once (claude --plugin-dir .) so Claude Code writes .claude-plugin/types/, then run tsc -p .. Both generated paths are git-ignored.
Source 2 files
hooks/router.tsx 221 lines
1import { atom, read, update } from 'claude-code'
2import type { Register } from 'claude-code'
3
4import type { RouterRow, RouterSession } from '../types'
5
6type Price = { input: number; output: number; cache_write_multiplier: number; cache_read_multiplier: number }
7type Usage = { model: string; input_tokens: number; output_tokens: number; cache_read_input_tokens: number; cache_creation_input_tokens: number }
8
9const PANE = 'model-router'
10const HISTORY_KEY = 'history'
11const HISTORY_MAX = 200
12const SETUP_KEY = 'setupStartedAt'
13export const FIRST_RUN_PROMPT =
14  'model-router was just installed. Run its first-run configuration now by invoking the model-router:setup skill, then tell me in one line what was set up.'
15const totals = atom({ plugin: 'model-router', key: 'totals' } as const, {})
16const agents = atom({ plugin: 'model-router', key: 'agents' } as const, {})
17const overrides = atom({ plugin: 'model-router', key: 'overrides' } as const, 0)
18const auto = atom({ plugin: 'model-router', key: 'auto' } as const, { on: false, loaded: false })
19
20// Automatic mode adds one hidden note to each typed prompt; the full skill loads once, later prompts get a short reminder.
21export const AUTO_LOAD =
22  'model-router automatic mode is on: invoke the model-router:run skill with the Skill tool and handle this request by its rules.'
23export const AUTO_REMIND =
24  'model-router automatic mode is on: follow the model-router:run instructions already loaded. Load them again with the Skill tool if they are no longer in context (for example after a compact).'
25const AUTO_ORIGINS = new Set(['composer', 'bridge', 'sdk'])
26
27let prices: Record<string, Price> = {}
28
29export function priceFor(table: Record<string, Price>, model: string): Price | undefined {
30  const key = Object.keys(table).find(k => model.startsWith(k))
31  return key ? table[key] : undefined
32}
33
34export function costOf(p: Price | undefined, u: Usage): number {
35  if (!p) return 0
36  return (
37    (u.input_tokens * p.input +
38      u.cache_creation_input_tokens * p.input * p.cache_write_multiplier +
39      u.cache_read_input_tokens * p.input * p.cache_read_multiplier +
40      u.output_tokens * p.output) /
41    1_000_000
42  )
43}
44
45export function addUsage(rows: Record<string, RouterRow>, who: string, u: Usage, p: Price | undefined) {
46  const key = `${who}|${u.model}`
47  const row: RouterRow = rows[key] ?? { who, model: u.model, requests: 0, input: 0, output: 0, cacheRead: 0, cacheWrite: 0, usd: 0, priced: true }
48  return {
49    ...rows,
50    [key]: {
51      ...row,
52      requests: row.requests + 1,
53      input: row.input + u.input_tokens,
54      output: row.output + u.output_tokens,
55      cacheRead: row.cacheRead + u.cache_read_input_tokens,
56      cacheWrite: row.cacheWrite + u.cache_creation_input_tokens,
57      usd: row.usd + costOf(p, u),
58      priced: row.priced && p !== undefined,
59    },
60  }
61}
62
63const tierName = (type: string) => type.replace(/^model-router:/, '')
64const money = (n: number) => `$${n < 10 ? n.toFixed(3) : n.toFixed(2)}`
65const kTok = (n: number) => (n >= 1000 ? `${(n / 1000).toFixed(1)}k` : String(n))
66
67export function summaryLine(rows: RouterRow[], autoOn = false): string {
68  const total = rows.reduce((s, r) => s + r.usd, 0)
69  const sub = rows.filter(r => r.who !== 'orchestrator').reduce((s, r) => s + r.usd, 0)
70  const spawned = new Set(rows.filter(r => r.who !== 'orchestrator').map(r => r.who)).size
71  return `${autoOn ? 'auto · ' : ''}router ${money(total)} · subagents ${money(sub)}${spawned ? ` (${spawned} tier${spawned > 1 ? 's' : ''})` : ''}`
72}
73
74export function table(rows: RouterRow[]): string {
75  if (!rows.length) return 'No model requests recorded in this session yet.'
76  const lines = ['| who | model | requests | input tok | output tok | est. $ |', '|---|---|---|---|---|---|']
77  for (const r of [...rows].sort((a, b) => b.usd - a.usd)) {
78    const input = r.input + r.cacheRead + r.cacheWrite
79    lines.push(`| ${r.who} | ${r.model} | ${r.requests} | ${kTok(input)} | ${kTok(r.output)} | ${r.priced ? money(r.usd) : 'n/a'} |`)
80  }
81  lines.push('', `${summaryLine(rows)}. List prices; cache reads and writes included.`)
82  return lines.join('\n')
83}
84
85export const register: Register = on => {
86  on('session.start', async ($, e, next) => {
87    try {
88      prices = JSON.parse(await $.fs.read(`${$.plugin.root}/seed/prices.json`)).models ?? {}
89    } catch {
90      prices = {}
91    }
92    // Automatic mode lasts one session.
93    await update($, auto, () => ({ on: false, loaded: false }))
94    try {
95      await $.command.register({ name: 'router', description: 'model-router: live cost per tier for this session and the last 7 days' })
96      await $.command.register({ name: 'model-router', description: 'model-router: route a task across the tiers (same as /model-router:run)', argumentHint: '<task>' })
97    } catch {}
98    // First load in an interactive session (install, enable, or first start): run the configuration once.
99    if (e.isInteractive && !(await $.store.get(SETUP_KEY))) {
100      await $.store.set(SETUP_KEY, new Date(await $.clock.now()).toISOString())
101      // session.start is awaited before the first prompt, so the prompt is queued from a timer.
102      $.clock.after(500, () => {
103        void $.prompt.submit({ text: FIRST_RUN_PROMPT })
104      })
105    }
106    return next(e)
107  })
108
109  // A model override would silently replace the tier's pinned model and evidence.
110  on('agent.spawn', async ($, e, next) => {
111    const pinned = e.subagentType.startsWith('model-router:') && e.model !== undefined
112    if (pinned) {
113      await update($, overrides, n => (n ?? 0) + 1)
114      $.ui.toast(`model-router: ignored model "${e.model}" for ${e.subagentType}; the tier's pinned model runs`)
115    }
116    const res = await next(pinned ? { ...e, model: undefined } : e)
117    if (res.agentId) {
118      const id = res.agentId
119      await update($, agents, m => ({ ...(m ?? {}), [id]: tierName(e.subagentType) }))
120    }
121    return res
122  })
123
124  on('turn.step', async function* ($, e, next) {
125    const res = yield* next(e)
126    if (res.usage) {
127      let who = 'orchestrator'
128      if (e.agentId) {
129        const known = (await read($, agents))[e.agentId]
130        who = known ?? tierName((await $.agent.list()).find(a => a.id === e.agentId)?.type ?? 'subagent')
131      }
132      const usage = res.usage
133      const rows = await update($, totals, t => addUsage(t ?? {}, who, usage, priceFor(prices, usage.model)))
134      $.ui.status(summaryLine(Object.values(rows), (await read($, auto)).on))
135    }
136    return res
137  })
138
139  on('session.end', async ($, e, next) => {
140    const rows = Object.values(await read($, totals))
141    if (rows.length) {
142      const history = ((await $.store.get(HISTORY_KEY)) as RouterSession[] | undefined) ?? []
143      await $.store.set(HISTORY_KEY, [...history, { endedAt: new Date(await $.clock.now()).toISOString(), rows }].slice(-HISTORY_MAX))
144    }
145    return next(e)
146  })
147
148  on('command.run', { command: 'router' }, async $ => {
149    const rows = Object.values(await read($, totals))
150    await $.ui.open({ id: PANE, title: 'model-router' })
151    return { text: table(rows) }
152  })
153
154  // Plugin skills are always namespaced, so the short name forwards to the skill.
155  // $.command.run rejects inside the hook this command waits on, so it is queued from a timer.
156  on('command.run', { command: 'model-router' }, async ($, e) => {
157    $.clock.after(0, () => {
158      void $.command.run({ command: 'model-router:run', args: e.args }).catch(() => {})
159    })
160    return {}
161  })
162
163  // /model-router:start and :stop are plugin skills (mod command names cannot hold a colon); answering here runs no model turn.
164  on('command.run', { command: 'model-router:start' }, async $ => {
165    await update($, auto, () => ({ on: true, loaded: false }))
166    $.ui.status(summaryLine(Object.values(await read($, totals)), true))
167    return { text: 'Automatic mode on for this session. Every prompt you type is routed across the tiers. /model-router:stop turns it off.' }
168  })
169
170  on('command.run', { command: 'model-router:stop' }, async $ => {
171    await update($, auto, () => ({ on: false, loaded: false }))
172    $.ui.status(summaryLine(Object.values(await read($, totals))))
173    return { text: 'Automatic mode off. /model-router <task> still routes a single task.' }
174  })
175
176  on('prompt.submit', async ($, e, next) => {
177    const mode = await read($, auto)
178    if (!mode.on || !AUTO_ORIGINS.has(e.origin.kind) || e.text.trimStart().startsWith('/')) return next(e)
179    await update($, auto, () => ({ on: true, loaded: true }))
180    return next({ ...e, context: [...(e.context ?? []), mode.loaded ? AUTO_REMIND : AUTO_LOAD] })
181  })
182
183  on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
184    const { Box, Text } = $.ui.resolve(e)
185    const rows = Object.values(await read($, totals)).sort((a, b) => b.usd - a.usd)
186    const skipped = await read($, overrides)
187    const history = ((await $.store.get(HISTORY_KEY)) as RouterSession[] | undefined) ?? []
188    const weekAgo = (await $.clock.now()) - 7 * 86_400_000
189    const week = new Map<string, number>()
190    for (const s of history.filter(s => Date.parse(s.endedAt) >= weekAgo))
191      for (const r of s.rows) week.set(r.who, (week.get(r.who) ?? 0) + r.usd)
192    const weekRows = [...week].sort((a, b) => b[1] - a[1])
193    const width = Math.max(30, e.props.bodyColumns ?? 60)
194    const cell = (s: string, n: number) => (s.length > n ? s.slice(0, n - 1) + '…' : s.padEnd(n))
195    const modelCol = Math.max(10, width - 12 - 5 - 8 - 10 - 4)
196
197    return (
198      <Box flexDirection="column">
199        <Text bold>This session</Text>
200        {rows.length === 0 && <Text dimColor>No model requests yet.</Text>}
201        {rows.length > 0 && <Text dimColor>{cell('who', 12)} {cell('model', modelCol)} {cell('req', 5)} {cell('out', 8)} est. $</Text>}
202        {rows.map(r => (
203          <Text>
204            {cell(r.who, 12)} {cell(r.model, modelCol)} {cell(String(r.requests), 5)} {cell(kTok(r.output), 8)} {r.priced ? money(r.usd) : 'n/a'}
205          </Text>
206        ))}
207        {rows.length > 0 && <Text color="cyan">{summaryLine(rows)}</Text>}
208        {skipped > 0 && <Text color="yellow">Model overrides ignored on pinned tiers: {skipped}</Text>}
209        <Text> </Text>
210        <Text bold>Last 7 days (ended sessions)</Text>
211        {weekRows.length === 0 && <Text dimColor>Nothing recorded yet.</Text>}
212        {weekRows.map(([who, usd]) => (
213          <Text>
214            {cell(who, 12)} {money(usd)}
215          </Text>
216        ))}
217      </Box>
218    )
219  })
220}
221
types/index.d.ts 28 lines
1export type RouterRow = {
2  who: string;
3  model: string;
4  requests: number;
5  input: number;
6  output: number;
7  cacheRead: number;
8  cacheWrite: number;
9  usd: number;
10  priced: boolean;
11};
12
13export type RouterSession = {
14  endedAt: string;
15  rows: RouterRow[];
16};
17
18declare module 'claude-code' {
19  interface PluginState {
20    'model-router': {
21      totals: Record<string, RouterRow>;
22      agents: Record<string, string>;
23      overrides: number;
24      auto: { on: boolean; loaded: boolean };
25    };
26  }
27}
28