Model Router · Documentation · Previous: Questions · Next: Glossary
# Architecture
Model Router changes two fields of an agent call, `model` and `effort`, and by default tells the main session to hand work to agents. Claude names both fields from a table the plugin gives it; the plugin holds them to the user's cap and floor, the allowlist and model health, fills in an effort nobody chose, gives a call that names no model a level from its agent's tools, raises a call to a rule's level, and runs a call marked "Redo:" one step higher. A Stop check asks for a check after unchecked edits, and for a review of an agent's work that no check can test. With a person at the prompt, the mod also shows a status line, answers `/model-router`, and offers a stronger main-session setup once.
## Two paths, one router

Both paths run one launcher, `scripts/model-router.sh`, and one procedure, `spawn.decide` then `spawn.record`. The mod, `hooks/router.ts` (under `modules` in `hooks/hooks.json`), is the main path, and calls the runtime's `mod-*` commands:
| Event | What the mod does |
| :- | :- |
| `tool.call` (`Agent`) | Keeps the call's `effort` (`agent.spawn` has none) and writes the mark; adds the spawn's `note` to the result's `context` |
| `tool.call` (a work tool) | Under `delegation` at `on` or `strict`, in the main conversation, adds up the characters Read, Grep, Glob, Edit, Write, NotebookEdit, Bash, PowerShell, WebFetch and WebSearch calls sent and got back, and an `Agent` call for the built-in `web-fetch` agent, one call counting for the runtime's `each` at most. Each time the total passes a further multiple of the `after` the runtime sent, it adds the `delegate` note to the result's `context`; with a `deny` text, it refuses the call once the total is `after` or more, adding the runtime's `fetch` text when the call is a page read. Both need `canStartAgent` (`Agent` in `$.tool.list()` and no `deny` from `$.tool.check`), and each sends `mod-delegate` |
| `tool.describe` | Appends `rules` to the Agent tool's description and `workflow_rules` to the Workflow tool's, from the latest `mod-start` answer |
| `session.start` | Sends the settings layers (`mod-start`), which the runtime stores; it clears day-old marks and answers the session-start note. If `isInteractive`, registers `/model-router`, refreshes the status line and runs the session check |
| `session.end` | At `/clear`, forgets the session id the layers went to and, if interactive, drops the session's switch and check (`mod-end`). Any other end sends nothing to the runtime, since the same session can follow it |
| `command.run` (`model-router`) | Answers `/model-router`, display only: the state or a switch (`mod-status`), or `check on` and `check off` (`$.config.set`) |
| `config.set` (`model-router.*`) | Refuses an option change while a main-loop turn runs or an agent whose effort the mod set is running: the change reloads the module, which forgets those efforts |
| `agent.spawn` | Sends the call, the layers, `parent_model`, `kind` and the session's effort (`mod-spawn`); passes the answer's `model` on. Marks a spawn a later mod moved to another family. Passes forks and resumed runs untouched. Sets the work total back to 0 once a spawn the main conversation made has started an agent, a resumed run included. A fork, a spawn with a parent agent, the `web-fetch` agent, a spawn another plugin made, and one that is refused or fails reset nothing (`delegates`, `begun`). No work call is refused on the total while a spawn the main conversation made is under way, whatever started it (`counted`), and that call's own spawn also ends the hold its Agent call put on (`holds`) |
| `turn.start` | On the main loop, sets the work total back to 0, notes that a turn started, and sends the layers again when the session id changed, as at `/clear`. Gives an agent whose first request beats its spawn's return that spawn's effort, if its prompt's waiting spawns agree |
| `turn.step` | Sets a routed agent's effort on each request that carries one (a workflow agent's only when higher); records the main loop's |
| `turn.complete` | Sends a subagent's tokens, cost estimate, and the model and effort its last request carried (`mod-usage`), with a `subscription` flag only when the usage read shows a plan window; a read that fails or comes back with no plan windows still sends the figures. On an interactive main loop, refreshes the status line |
The settings hooks:
| Hook | Command | What it does |
| :- | :- | :- |
| `SessionStart` | `session-start` | Records the session's model; writes the session-start note. As a session opens (not at `/clear` or compaction), a `systemMessage` names a subagent model variable that is set |
| `PostModelSwitch` | `model-switch` | Records the new model |
| `UserPromptSubmit` | `user-prompt` | Says where a call runs with the `model` the person's own prompt names, as in "use opus" (`intake.is_user_prompt`, `request.named_model`); gives the main conversation any waiting line about files an agent's shell changed, and, beside the prompt that carries a background agent's report, says no check has run on agents' edits |
| `PreToolUse` (`Agent`) | `pre-agent` | The fallback (`fallback.route`): rewrites the call through `updatedInput`, with a line saying what it set |
| `PreToolUse` (`SubagentHandback`) | `handing-back` | Notes the project files an agent's shell changed, for the main conversation, where the report goes back through that tool; the call goes through as sent |
| `PostToolUse` (`Agent`) | `post-agent` | Records the result (done record, health, ledger); asks for a check of a run that switched models mid-run; beside a foreground agent's result, tells which project files its shell changed, and that no check has run on agents' edits |
| `PostToolUseFailure` (`Agent`) | `agent-failed` | Strikes a model error; says what a re-issued call runs on |
| `SubagentStop` | `agent-stopped` | Finishes a background run that stopped with an answer; counts a check the subagent ran, and a read by an agent whose name says it reviews; notes a project file the subagent's shell changed as an agent's edit, and keeps a line about such files for the main conversation |
| `PostToolUse` (`Edit`, `Write`, `NotebookEdit`) | `post-edit` | Notes an edit under the project, and whether an agent made it |
| `Stop` | `stop` | The Stop check |
### Which path routes a call
While the runtime answers (the latest `mod-start`, `mod-spawn` or `mod-usage` got a reply, if only `{"ok": true}`), the mod's `tool.call` hook writes an empty file, `mod-calls/`. Plugin `PreToolUse` hooks run after the mod; `mod.routes` takes the mark and stands the fallback down.
| Case | Routed by |
| :- | :- |
| `Agent` call, mark written | The mod, at spawn |
| `Agent` call, no mark (no reply yet, a failed one, a failed write, no mod) | The fallback; the mod passes its spawn untouched |
| `Agent` call, no mark, after an empty reply (no Python) | Neither. With routing on, the mod holds a named model to `max_tier` and `min_tier` by family (`heldTo`) |
| A marked call whose `mod-spawn` fails twice | Neither; the mod holds the cap and the floor as above |
| A spawn with no `Agent` call, such as a workflow stage | The mod |
| A fork or a resumed run | Neither |
A mod round trip times out after 8 seconds, a settings hook after 10 (`UserPromptSubmit` after 5). `cli.main` catches what a handler raises, writes its type and place to stderr, and exits 1: Claude Code shows a hook error notice, and the mod counts a failed round trip. Hook input that doesn't parse reads as empty; with no readable `tool_input`, `pre-agent` leaves the call alone and `mod-spawn` answers nothing, as with no Python. The launcher runs Python with `-I`, exits 0 with no Python 3.9 or later, and 1, never 2, on other hook failures. Each mod hook's `.catch` passes the event on.
## In an interactive session
None of this runs unless `session.start` reports `isInteractive`. The loader takes `$` only as calls, so each mods API call sits in a `try`, and a surface without one loses only that feature.
- **Status line.** `report.status` builds it from the options, the switch and the session's spawn rows: `auto · mod · cap opus · 14 calls · 5 changed · 2 redos`, or `off`. A floor shows as `floor sonnet` after the cap. The second field is `fallback` when the latest spawn went that way, `(this session)` marks a switched mode, and a zero count of changed calls or redos is left out. The mod pins it with `$.ui.status`, one refresh at a time, folding any asked for meanwhile into one more.
- **Switch.** `/model-router on` or `off` sends `routing` to `mod-status`. `settings.switch` keeps it in the session's `switches.json` entry (none when it matches `routing_mode`), and `settings.resolve` reads it for every snapshot with a session id, so both paths decide alike. The mod keeps the answer for a call whose round trip fails.
- **Session check.** Runs after `session.start` returns, when `session_check` and routing are on and `$.session.turns()` is 0. `mod-check` sends `$.session.model()`, whether managed settings own `session_check` (`$.config.list()`), and a `--effort` flag read from the session's command line with `ps`. `session.check` marks the session checked first, so the reload a `$.config.set` causes asks nothing. It asks only when the session's model at its estimated effort matches no `session` setup in `config/models.json`, or is older than its family's `newest`, and offers each setup the allowlist and `maxEffortLevel` leave, then a way to stay. Each choice carries its label, its next question (`then`), what it does (`model`, `effort`, `check`) and the line to show after (`done`). The mod asks with `$.ui.ask` only before the first main-loop turn and acts only on a label it sent: `$.command.run` for `model`, `$.session.model()` to confirm the family, then `effort`. The session's model is read before the first command and after each one (`apply` in `hooks/router.ts`). A pick with no model command always runs `/effort`. When a pick has a model command and a read before or after it failed, `/effort` is not run and one line says so (`EFFORT_SKIPPED`). A failed read after the last command shows one line that the change could not be confirmed (`UNCONFIRMED`); `$.config.set` for `check`.
- **Effort estimate.** `session.effort_on`: `CLAUDE_CODE_EFFORT_LEVEL` (`auto` or `unset`: the API's default for the family, or Claude Code's default under a `maxEffortLevel` cap), the `--effort` flag (`ultracode` reads as `xhigh`), `Allowlist.saved_effort` over the layers in `allowlist.PRECEDENCE` order (a model's `modelSettings` level before its file's top-level `effortLevel`, the user file's top-level one skipped), then `config.DEFAULT_EFFORT`, held to `maxEffortLevel`. After each main-loop turn, `session.observe` records the model and effort its latest request carried.
## Modules
In `scripts/modelrouter/`, standard library only:
| Module | Owns |
| :- | :- |
| `cli`, `hooks` | The command line; one handler per hook command; the session-start note (`hooks._rules`) |
| `mod`, `fallback` | The mod's commands and mark; the fallback's rewrite, repeat guard and deny |
| `spawn`, `router` | The shared procedure and the run a redo redoes; `decide`, from facts alone |
| `ladder`, `registry`, `config` | Levels and rungs; the `Registry` (`select`, `available`, `clamp`, `family`); building it from the config files |
| `settings` | Options, environment facts, the session's routing switch, and each hook's `Snapshot` |
| `allowlist`, `health` | `availableModels`, `deniedModels`, `maxEffortLevel`; model errors and substitutions |
| `calls` | Routes, outcomes, the redo mark, tasks, done records |
| `agents`, `session` | Agent definitions and whether one can only read (`agents.read_only`); the conversation's model and transcripts, and the session check |
| `intake`, `request`, `gates` | Whether a prompt is the person's; the model it names; the Stop check |
| `ledger`, `report`, `store` | Ledger rows; skill tables, `--json` and the status line; state files and locks |
The agents `scout` and `verifier` pin Haiku at `medium`, `implementer` Haiku at `high`, and `architect` and `reviewer` Opus at `high`. None of them has the Agent tool: four list their tools, and `implementer` drops it with `disallowedTools`, since the Agent tool's description carries the table, and under `delegation` at `strict` (the default) or `on` the delegation paragraph, and an agent that reads it hands its brief on. Models are entries in `config/models.json`; the mod repeats the family names in `TIERS`. A model's `twins` are other names it is offered under (Fable's is Mythos). `Registry.family` and the mod's `NAMES` (twins and `aliases`) place a twin's ID in its family for the cap and the ladder; the allowlist and the health table match it by its own name (`family(twins=False)`).
## The ladder

| Level | `needs` | Model | Effort |
| :- | -: | :- | :- |
| lookup | 1 | Haiku | medium |
| standard | 1 | Haiku | high |
| complex | 3 | Opus | high |
| hardest | 3 | Opus | high |
| frontier | 4 | Fable | high |
A level runs on the cheapest usable model whose strength meets its `needs`, else the strongest usable one (`Registry.select`). A model stands for the lowest level it is the model of (`ladder.rung`), or, if it is no level's model, the highest level it is strong enough for. A named effort lifts it on the same model: Haiku at `high` or above is standard, and Opus above `high` is hardest. No level picks Sonnet, so a call that names Sonnet stands for standard, and Sonnet also runs as the redo of a Haiku run. Hardest is reached by a rule that sets the level, by Opus named above `high`, or by a redo the cap holds on Opus.
`complex` has `needs` 3 because, in a round of 24 runs on a review task with 10 seeded defects, the complex level on Sonnet at `high` solved 0 of 6 and on Opus at `high` 4 of 6 (one-sided Fisher p 0.030). Opus costs more per run: $0.65 against $0.28 at the mean. Under `max_tier: sonnet`, a judgment call that names a model and an effort is held to Sonnet at the named effort, and one that names no effort runs on Sonnet at `xhigh`.
The router's effort is the level's own, one step more (at most `xhigh`) on a model weaker than the level needs (`ladder.effort_on`).
## The decision
`spawn.decide` gathers the facts (the agent's definition, the parent's model, the usable models, the session's effort, and for a redo the run it redoes) and calls `router.decide`, which reads no state. Neither writes. `spawn.record` then writes the ledger row and, unless the call is denied or a workflow stage, the route.
`agents.find` looks in the project's `.claude/agents` folders, nearest first and up to the repository root, then in the user's agents folder, then at the built-in names. For a `plugin:name` type it looks in that plugin's install folder. A folder is walked 10 deep and 20,000 entries at most, the first 500 markdown files of it are read, and each file only to its first 24,000 bytes.
A decision is `keep`, `rewrite` or `deny` (the guard's), with a model, effort, level, source, `was` (the untouched model) and reason. A fork, routing `off`, an allowlist that leaves no registry model open, a user rule's `route: false`, or a call's `model` outside the registry such as `inherit` is a keep. Otherwise the first source below that names a model wins.
### Order of authority
| # | Source | The router |
| -: | :- | :- |
| 1 | `CLAUDE_CODE_SUBAGENT_MODEL_FORCE` | Sets effort only, none when the call names a model. With only this set, the router rates the built-in Explore as the model `CLAUDE_CODE_SUBAGENT_MODEL` names, or the conversation's when it names none. Claude Code may run Explore on another model, which the router can't see |
| 2 | `CLAUDE_CODE_WORKFLOW_SUBAGENT_MODEL`, for a workflow agent whose call names no model | Sets effort only |
| 3 | The call's `model` | Keeps it |
| 4 | The user's rule `model` | Applies it |
| 5 | A project's rule `model` | Applies it only when not weaker than what the call runs on without it, and not a model the project added; else `held` says why |
| 6 | The agent's own `model` | Keeps it; for `inherit`, the parent's model, which outranks row 7. With no definition found, or a `model` outside the registry, leaves the call alone |
| 7 | `CLAUDE_CODE_SUBAGENT_MODEL` | Leaves the call alone. Unforced, it doesn't move the built-in Explore and Plan, which go to row 8 |
| 8 | Nothing | The level the agent's tools put it at: `lookup` when its `tools` hold none of Edit, Write, NotebookEdit, Bash, PowerShell and Monitor, or its `disallowedTools` remove them all, as for the built-in Explore and Plan; `standard` otherwise. A list `agents._list` can't read with confidence counts as every tool, or as removing none |
Then, for rows 3 to 6 and 8 (steps 4 to 6 on rows 1 and 2 too):
1. **Level floor.** A rule's `level` raises a call that stands below it to that level and its model. On row 8 either file's floor raises the tools' level. On an inherited parent's model the user's floor applies wherever the call stands below it, and a project's only when its model is stronger than the parent's, or, with the parent's unknown, when no registry model is stronger (`router._floor_lifts`).
2. **Teammate.** A teammate naming nothing starts no lower than standard: its row 8 level is at least standard, and an inherited parent's model weaker than standard needs moves to standard's model.
3. **Cap and floor.** `Registry.clamp` keeps a usable model (inside `max_tier`, no weaker than `min_tier`, permitted, healthy), else takes the next stronger usable one, else the strongest. A model the floor raises keeps the level and effort it had, so a lookup raised from Haiku to Sonnet runs at `medium`. `Registry.available` drops the health filter before it leaves nothing usable. With no usable model at or above the floor, the strongest one left stands in, and the reason says the floor could not be held. The floor is ignored when `max_tier` is weaker (`Settings.floor_ignored`), and, as under a cap, only models whose strength the user vouches for count. `router._moved` words the reason, as in "raised to the floor the user set (sonnet)". An exact ID the call or the agent names inside `max_tier` and `min_tier`, which the lists let run though they don't open its family (a Mythos ID, say), makes that family usable for the call (`spawn._usable`). A parent's model it moves runs at the new model's level.
4. **Effort.** If nobody chose it (the call, the agent, `CLAUDE_CODE_EFFORT_LEVEL`, or `CLAUDE_CODE_SUBAGENT_EFFORT`, which skips teammates), the level's effort.
5. **Session floor.** On a main-conversation call on the session's own model, an effort the router sets is at least the session's (`router._session_floor`): `session_effort` from the mod, `effort.level` from the hook.
6. **`maxEffortLevel`.** The lowest cap any settings source sets for that model, a `modelSettings` entry replacing its source's top-level cap (`router.capped`). None without the mod's layers.
The same model with no effort to set is a keep. Where the untouched model is unknown (a nested call on the fallback, the built-in Explore and Plan), row 8 still names a model, and an agent that inherits is left alone unless a floor lifts it.
A full ID or `[1m]` variant the call or the agent names is kept while its family is, if the lists permit it and it is healthy (`spawn.exact`). On a provider whose alias runs an older model (`older_aliases`), the mod sends the family's `newest` ID unless `ANTHROPIC_DEFAULT__MODEL`, `modelOverrides`, the allowlist or health rules it out (`Registry.newest_id`). Where no newest ID goes (always on the fallback, which sends aliases only), the router's own picks (a level, a floor, a redo's next model) move one family up (`Registry.past_lag`), unless `ANTHROPIC_DEFAULT__MODEL` pins that family. Mantle alone has a row of its own (`sonnet` is Sonnet 5, `haiku` is Haiku 4.5) but is in `registry.ALIAS_ONLY`: no newest ID goes out and no pick moves (`Snapshot.lagging` is empty), since it serves only some versions. Mantle with another provider is `unknown`, and only aliases are sent.
`explain`, behind `/model-router:route`, runs `spawn.decide` with no session and writes nothing.
## Redo escalation

| Step | How |
| :- | :- |
| Mark | A `description` starting "Redo:", any case (`calls.marked_redo`). The same call sent unmarked is a new call. A workflow stage is never a redo |
| Task | A hash of the agent type and the description without the mark (`calls.task`) |
| Record | Each route in `routes.json` holds the effort the run gets, in Claude Code's order (`CLAUDE_CODE_EFFORT_LEVEL`, the call's as sent, the agent's, `CLAUDE_CODE_SUBAGENT_EFFORT`, the session's on its own model), else `medium` (`router.ran_at`); on the mod, what the run's last request carried replaces it (`calls.observed`). A foreground result with no model swap makes the run its task's latest in `done.json`; a background one waits in `launched.json` for `SubagentStop` with an answer |
| Find | The task's latest run this session, at most 30 minutes old, from any request (`calls.last_run`) |
| Run as named | If the call's unmarked decision asks for more than that run (a stronger model, or more effort on a model that took one), it runs as decided (`spawn._asks_more`) |
| Climb | Otherwise it climbs from that run, or with none on record from its unmarked run (`on_record: false`) |
The climb (`router._redo`):
1. Below `high` effort: the same model, one step up.
2. Else the next stronger usable model, never a project's, one effort level below the run it redoes and never below `medium` (`router._below`).
3. Else the same model one more step, at most `max`.
A step `maxEffortLevel` holds counts as none. Under an effort variable the router takes no effort step, so the redo takes the next model; under `CLAUDE_CODE_SUBAGENT_EFFORT`, a redo left on its model keeps the effort it redoes. A nested run with no effort on record skips step 1, and a run whose requests carried no effort gets no effort step. The call's model, the agent's, a rule's model and `CLAUDE_CODE_SUBAGENT_MODEL` don't hold a redo back. Under the force pin a redo gets one effort step, none past `max`. The session floor and `maxEffortLevel` apply after, and `escalated_from` names the redone run's level.
With no stronger usable model and no effort to gain, the redo runs as the run it redoes did, and `no_step` is set. Claude then reads one sentence, such as "This redo ran on opus at max, the strongest model and the most effort usable here, the same as the run it redoes." (`spawn.redo_note`): `additionalContext` on the fallback, the `tool.call` result's `context` on the mod.
A redo is left alone wherever a plain call is. It misses its record after a skipped write, 30 minutes, 100 newer tasks, or a day idle, and never changes later calls. A run that swapped models mid-run (`modelsUsed`) finishes nothing and isn't health evidence. The ledger keeps at most 5 of its model ids, the first and the last among them, so the note to Claude names the first model and the last.
## The session-start note
`hooks._rules` writes it while routing is `auto`, as facts, since the Agent tool sets `effort` only when instructions ask. It says the plugin keeps the `model` and `effort` a call names within the cap (within the user's limits, under `min_tier`) and fills in `effort` when a call names only a model, then "Here every Agent call names both, by the work": haiku at medium for finding, reading and listing; haiku at high for writing and fixing code, figures and documents to a brief that settles the approach; opus at high for a judgment call with an open approach or no check to run (a design, a review, a rewrite). Sonnet and Fable have no row. Under `min_tier` each row names the model it runs on, so with `min_tier: sonnet` the rows read sonnet at medium, sonnet at high and opus at high. The note ends with where a call naming neither runs, and how to mark a redo.
With `delegation` at `strict` (the default) or `on` (at `off` the note is the table alone), and no force pin, a paragraph (`hooks._DELEGATE`) comes first. It says this session coordinates and agents do the work, hands to an agent whatever takes more than two tool calls, says an agent's report is a claim, so work no check ran on gets a review by a second agent before it counts, and counts a workflow stage or a teammate as an agent. `strict` adds a sentence that the next call is refused once the session has moved a lot of text itself, until an agent starts.
Each pairing is a level's model and effort from the registry (`ladder.model_for`, `ladder.effort`), so a rules file that moves a level moves the note. The force pin, either effort variable, `max_tier`, `min_tier` and `CLAUDE_CODE_SUBAGENT_MODEL` each trim or reword it to what still applies.
While routing is `auto`, `mod-start` answers the same text as `rules`, and `workflow_rules` (`hooks._workflow_rules`), the table worded for a workflow script's `agent()` calls; under the force pin, where a script can't set a model, there is none. Each tool's description keeps the latest text it got. The Workflow tool's text has no delegation paragraph.
`mod-start` also answers `delegate` (`hooks._delegate`) with `after`, `each` (the most one call counts for, half of `after`), a `note` and, under `strict`, a `deny`, a `lifted` and a `fetch`, unless routing is `off`, `delegation` is `off`, the force pin is set, or the session runs as an agent (`hooks._delegating`). With routing `off` it answers only `{"ok": true}`, which tells the mod the runtime ran. The mod sends the agent the session runs as with `mod-start` and `mod-check` (`runsAs` in `hooks/router.ts`, from `$.session.info().agentType`), and the `SessionStart` hook reads it from `agent_type`. Such a session, a teammate in a pane of its own among them, gets the table without the delegation paragraph and no session check (`mod.check`). The mod keeps the total in memory and the runtime holds none. It counts a call only when the main conversation made it: a subagent's call carries an agent ID, and another mod's own `$.tool.call` carries that mod's name in `next.origin`. An agent the main conversation starts sets the total back to 0, a resumed run included, and so does each new turn of the main conversation. A fork, the `web-fetch` agent, an agent a subagent starts, an agent another plugin starts, and a start that is refused or fails don't (`delegates` and `begun` in `hooks/router.ts`). No work call is refused on the total from the moment the main conversation's own Agent call begins until the first of two events: that call's own spawn begins, or that call settles (`holding`). The spawn that carries the call's `tool_use_id` ends the hold, and so does a spawn that carries no id and belongs to no workflow run, which ends the oldest hold; a spawn of another call or of a workflow run ends none, and a hold ends once (`counted`). No work call is refused either while a spawn the main conversation made is under way, whatever started it. So a foreground agent's whole run does not hold the refusals off. `mod-delegate` (`hooks.mod_delegate`) takes `kind`, `reminder`, `deny` or `lifted`, and writes one `gate` ledger row of kind `delegate`, `delegate-deny` or `delegate-lifted`. The threshold is 8,000 characters: in the eval runs on an Opus session, a session that handed the work to agents moved at most 4,419 characters itself between agents, a one-line fix done in the main conversation moved 631 to 1,177, and a seven-change release done there moved 35,000 or more.
## Model health and the allowlist
Model health lives in `state.json` (`health.py`), one table per provider: `health` for the Anthropic API and `health.` for a session on another one, so a model one provider can't serve stays usable on the next. `/model-router:models reset` clears every table.
- A model error is a failed `Agent` call whose error text has `not_found_error`, `model_not_found` or `claude_code_version_too_old`, and isn't an interrupt or an unknown agent type. A substitution is a `resolvedModel` in another family than asked, outside the force pin, a swapped run, or the untouched model.
- Neither counts for a spawn a later mod moved to another family (the mod compares the model it sent with the one the engine's entry in `next.trace` was asked for, and writes `mod-calls/.moved` for `post-agent` and `agent-failed` to take), or for a fallback call left on the parent's model, which the router only guessed.
- Two of either route the model around for a day. A run as asked clears the count; until then, one more after the day routes it around again. The cause kept is the error's type identifier, never its display text, which can quote a model ID a repository's settings chose. A full ID the router sent (a newest ID, or an exact ID kept on a rewrite) is routed around after one model error or provider refusal (`health.REFUSED`), and its alias runs instead. An exact ID on a call left as it is strikes nothing. `/model-router:models reset` clears the table.
- `Registry.available` drops the health filter when it would leave nothing usable; it never drops the allowlist's.
The allowlist (`allowlist.py`):
- The mod reads each settings source at session start, at the first main-loop turn after the session id changes, as after `/clear`, `/resume` or `/branch`, and at each spawn it routes, and sends only the keys routing reads, kept per session in `allowlist.json`; a source whose read fails keeps what it last sent. A read waits at most 2 seconds; a source that missed that is read without waiting from then on, and its answer goes with a later command. Without the layers no list applies, and Claude Code enforces its own.
- A policy `availableModels` applies alone, else the sources' lists join; `deniedModels` and `availableModelsMatch` count only from policy. A policy `availableModels` or `deniedModels` that isn't a list, or a policy deny list over 200 entries or with an entry over 200 characters, leaves no model open. An allow list keeps its first 200 entries and drops any entry over 200 characters. A deny entry blocks a family only when it names it with no version.
- A `[1m]` suffix is dropped from an `availableModels` entry and from the requested ID before matching, on every provider, as Claude Code does. A `deniedModels` entry catches a request with and without the suffix, and one written with the suffix blocks that variant only.
## The Stop check
`gates.before_stop` asks once per batch of edits when `post-edit` noted an Edit, Write or NotebookEdit on a file under `cwd` or `CLAUDE_PROJECT_DIR` (`gates.note_edit`, which also records whether an agent made it), and no later tool call in the transcript ran a check (`gates.is_check`); a subagent's transcript counts at `SubagentStop` (`gates.agent_finished`). A heredoc in the main conversation's own commands that writes a project file is its edit too, and so is a PowerShell here-string that `Set-Content`, `Add-Content`, `Out-File`, `Tee-Object` or a redirect writes to one, read from the transcript at `Stop` (`gates._heredoc_writes`). In that here-string form a path given as `-Path:notes.md` is read, and a relative target follows an earlier move in the command: `Set-Location`, `sl`, `chdir`, `cd`, `Push-Location` or `pushd`, with the folder bare or as `-Path:dir` or `-LiteralPath:dir`. A `Push-Location` with no path stays where it is, and `Pop-Location` or `popd` goes back to the folder before the last push. `~`, `~/path` and `~\path` are the home folder, a `~name` in a move leaves a relative target counted as a project write, and so does a `~name` as the target when no move comes before it, `-Verbose` and `-Debug` are switches that take no value, `tee` as the first word of a statement is `Tee-Object`, and a `>` inside quotes is no redirect. A `(` or a backtick anywhere in the writer's statement, as in `Set-Content notes.md -Value (Get-Date)`, hides its path, so the write counts as a project write.
In the Bash form only a `cd` on the heredoc's own line moves where it writes, so `cd /tmp && cat > notes.md < a.md < a.md <`, `>>`, `1>`, `1>>`, `&>`, `&>>`, `>|`, `>&file` or `>& file`, and `tee` writes as one does; `2>`, `>&2`, `1>&2`, `2>&1` and `>&-` are not writes. The lines a heredoc feeds to a program are not read for redirects, unless the program is a shell (`bash </subagents/agent-.jsonl`). It counts the agent's shell work there as well (`gates.handing_back`). One scan covers an agent's shell calls: after a scan at a hand-back, or one that led to a line, `edits.json` keeps the agent's ID with when its latest shell call started and what the scan found (`scanned`, the latest 20). A later hook for that agent acts on what is kept, and scans again only after another shell call, or while one runs in the background. A line is kept once per agent: `edits.json` keeps the IDs of those agents (`asked`, the latest 100). None is kept when the call has no agent ID or when that write fails. What the report says is not read, since a report can name a file and call it untouched. The line shows at most 8 names in backticks, and only plain ones. Each line writes a `gate` row of kind `report`.
The ask is `additionalContext` on `Stop` naming the work's own check (tests, `model-router:verifier`, a recompute, a reread), plus a `gate` ledger row. When an agent made the unchecked edits, it says the agent's report is the only word on them and, for work with no check to run, asks for a review by `model-router:reviewer`, briefed with the request and the files. It adds that a compare against the agent's report leaves the files unread. That ask is 862 characters, and the one for the main conversation's own edits 516 (`gates.unverified_edit`). It is silent when routing isn't `auto`, `verify_gate` is off, `stop_hook_active` is set, background tasks run, or the transcript can't be read.
`gates.unchecked_at_return` says it early, in one line of 273 characters, when an agent's work comes back: `post-agent` adds it to a foreground agent's result, and `user-prompt` to a background agent's notification. It needs unchecked edits an agent made and no background run still waiting in `launched.json`. `edits.json` keeps when it was last said (`early_at`), so a batch of edits draws it once. It writes a `gate` row of kind `verify-early` and changes nothing the Stop check does: a turn that ends with the edits unchecked still gets the ask. The early line lets a session start the review before the turn ends.
## State
The data folder is `${CLAUDE_PLUGIN_DATA}`, else `plugins/data/model-router-local` under `CLAUDE_CONFIG_DIR` or `~/.claude`: mode 0700, and 0600 for the runtime's files. That config folder also holds the user's agents, your override file and the plugin install records. A hook run with `CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=1` has no `CLAUDE_CONFIG_DIR`, since Claude Code removes it and keeps `CLAUDE_PLUGIN_DATA`, so then the folder is read from `CLAUDE_PLUGIN_DATA` when that is an absolute path ending in `plugins/data/` (`store.claude_home`).
| File | Holds |
| :- | :- |
| `state.json` | Model health, a table per provider |
| `allowlist.json` | Per session: settings layers |
| `sessions`, `routes`, `pending`, `launched`, `done`, `edits` (`.json`) | Per session: its model, routes, waiting fallback rewrites, waiting background runs, finished runs, edit and check times, whether an agent made the unchecked edits, the latest file-tool writes, the agents whose shell's files were already noted, the lines waiting for the main conversation, when the early line was last said and the agents a disk scan already covers |
| `switches.json` | Per session: the `/model-router` switch, whether the session check ran, its estimate and the setup the latest main-loop request carried. Dropped at `/clear`, and by a write after a day unwritten |
| `locks.json` | Per file: lock waits, waits on another session, skipped writes |
| `.flock`, `mod-calls/` | OS lock files; the mod's marks |
| `ledger.jsonl` | One row per spawn, result, model error, Stop ask, early line about unchecked agent edits, line about files an agent's shell changed, delegation reminder, refusal or end of refusals and subagent turn |
Per-session files keep 200 sessions and 100 records each, `routes`, `launched`, `done` and `switches` drop a session by a write once it is a day unwritten (a read does not check age), and `edits` after 30 days. A session start removes from the data folder a temp file a killed hook left more than an hour ago and the state files only earlier releases wrote, and, once a day, from the launcher's private bytecode folder the `.pyc` files kept for a source folder that is gone (`store.sweep`). The ledger drops rows past 30 days once its oldest is a day past that, so an age trim runs about once a day. Past 4 MiB it drops each running usage row a later one for the same agent replaces, then its older half if the rest still fills 2 MiB.
Writes are atomic. Each read-modify-write and ledger append holds an OS lock (`flock`, or `msvcrt.locking` on Windows) on the file's `.flock` (`store.locked`), since agents started together run their hooks at once; only the counts in `locks.json` are written without one. The OS frees a dead holder's lock. A held lock is `BlockingIOError` from `flock` and `PermissionError` (`EACCES`) from `msvcrt.locking`; any other lock error means the filesystem can't lock, and the write goes ahead unlocked.
| Case | What happens |
| :- | :- |
| Lock busy | Retried for up to one second; then the write is skipped and counted, and the hook goes on with what it read |
| A write fails (a full disk, an I/O error) | Skipped, and the hook goes on with its normal answer. A `/model-router` routing switch that could not be saved says so, and `/model-router:models reset` says `state.json` was left as is |
| `state.json` corrupt | Moved aside as `state.json.corrupt-` (three kept); routing goes on with it empty; `/model-router:models` lists it |
| Newer `schema` in `state.json` or `locks.json` | Read, never written |
| `locks.json` corrupt | Its counts start over at the next one |
| Per-session file corrupt | Read as empty, replaced on the next write |
| Ledger line that doesn't parse (bad bytes, nesting past the parser's limit) | Skipped |
## Trust
The user's `~/.claude/model-router.json` is trusted in full. A project's `.claude/model-router.json` only raises (`config.build`):
- A level `needs` or `effort` below the shipped or user value is dropped, and a named model keeps the level it has without the file. A `needs` of 0 or below means any model will do, and a level never needs less than the level under it.
- Its rule `level` is a level floor, its rule `model` follows row 5, and `route: false` is ignored. A user rule setting an agent's level or model voids both of the project's.
- A model it adds is never a pick, a clamp stand-in or a redo's next model, is ignored under a cap or a floor, and may not overlap a trusted name or a shipped twin.
An override file's `routing_mode` is dropped: only `/config` sets it. Bad types and non-finite numbers are dropped, and `/model-router:models` lists what was dropped or held back. An override file that doesn't parse, nests past the parser's limit, is over 4 MiB (64 KiB for a project's file) or isn't a regular file says nothing (`store.load_dict`).
## Compatibility
`state.json`, `locks.json` (`store.SCHEMA`), `config/models.json` and override files (`config.CONFIG_SCHEMA`) carry a top-level `schema` of 1, which a file without one also is. An override with a newer `schema` is read as far as understood. `models --json` prints `model-router.models/3` and `usage --json` prints `model-router.usage/2`, the data the tables use (`report.models_data`, `report.usage_data`); a new key keeps the version, and removing, renaming or redefining one bumps it. Options follow the plugin's version. Removing or renaming a key or option, changing its type or meaning, or accepting fewer values bumps the version.
## What the plugin never does
- **Network calls.** The runtime imports no network module; the mod uses only the engine's own APIs.
- **Reading a brief to pick a model.** A call's prompt is only hashed into its key (`calls.identify`) and matched to the agent's first turn. The person's prompt is read only for a model it names.
- **Changing more than `model` and `effort`** of an agent call. The main conversation's model and effort change only through Claude Code's own `/model` and `/effort`, run for a setup the person picks in the session check.
- **Writing settings**, but for `session_check`, through `$.config.set`, from `/model-router check on` or `off` or the session check's "Don't ask again". A plugin's own `$.config.set` skips its `config.set` hook, so `check on` and `off` run the same refusal themselves.
- **Approving a call.** No hook returns `permissionDecision: "allow"`.
- **Refusing a tool call**, but for one case. Under `delegation: strict` (the default), the mod refuses a work call of the main conversation once the total reaches the threshold with no agent started, and only while `canStartAgent` holds, checked again once the wait ends, so a call that waited while an agent started or an Agent call failed runs. It says why and tells Claude to hand the work to an agent. An Agent call of the main conversation that comes back refused or errored with no agent started, while the refusal stands, turns the refusal and the reminder off until an agent starts or a new turn begins. So does the start of a 6th round of refusals with no agent started (`ROUNDS` is 5), whatever the cause: a round is the refusals one request of the main conversation brings (counted in the `turn.step` hook), at most `BATCH` (8) of them, so the refusals end even if no request is seen, and an agent that starts or a new main turn resets the count. When the refusals end either way, the mod owes Claude the `lifted` text ("the refusals are off now, so tool calls run here") and adds it to the next result of the main conversation's own call, once each time they end: on a new line after a `deny` text, or in the `context` of an answer or an error. An agent that starts first, or a new main turn, drops it, and `mod-delegate` writes one `delegate-lifted` row when the refusals end.
- **Blocking a spawn**, but for one case. A fallback rewrite waits in `pending.json`. If the same call comes back 5 seconds to 30 minutes later with no result, the rewrite didn't run, so it passes through untouched and can't loop; if that would run it above `max_tier`, or below `min_tier` for a raise that didn't take, it is denied, with the model to use (`fallback._guard`).
- **Blocking a session.** Hooks never exit 2 and return no `decision: "block"`. The Stop check's ask is `additionalContext`, which gives Claude one more turn, and `agent-stopped` answers `SubagentStop` with nothing, so no agent is kept running.