---
name: model-routing
description: >-
  Pick the cheapest model tier that does a piece of work well, from rules in .factory/routing.json, and settle disputes about tiers with a best-of-k eval instead of opinion. Use when starting any task in a repo with .factory/, before handing work to a subagent, when a run is getting expensive, when asked "which model should do this", or when a new model ships and the routing may be stale.
license: MIT
compatibility: >
  Node.js 18+, no dependencies. Applying a tier depends on the harness: Claude Code subagent models, Codex profiles, Cursor's model picker. The eval runner needs git worktrees and a headless agent command (for example claude -p or codex exec); it spends real tokens.
metadata:
  author: software-factory
  version: "1.0"
allowed-tools: Bash(node:*) Bash(git worktree *)
title: model-routing
canonical_url: https://skilld.dev/gh/aditya-deokar/software-factory/model-routing
last_updated: 2026-10-30T17:45:51.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [scripts/lib/factory.mjs](https://skilld.dev/api/skills-raw/aditya-deokar/software-factory/model-routing/scripts/lib/factory.mjs), [scripts/lib/frontmatter.mjs](https://skilld.dev/api/skills-raw/aditya-deokar/software-factory/model-routing/scripts/lib/frontmatter.mjs), [scripts/lib/schema.mjs](https://skilld.dev/api/skills-raw/aditya-deokar/software-factory/model-routing/scripts/lib/schema.mjs), [scripts/lib/transcripts.mjs](https://skilld.dev/api/skills-raw/aditya-deokar/software-factory/model-routing/scripts/lib/transcripts.mjs), [scripts/route-eval.mjs](https://skilld.dev/api/skills-raw/aditya-deokar/software-factory/model-routing/scripts/route-eval.mjs), [scripts/route.mjs](https://skilld.dev/api/skills-raw/aditya-deokar/software-factory/model-routing/scripts/route.mjs), [scripts/schemas/routing.schema.json](https://skilld.dev/api/skills-raw/aditya-deokar/software-factory/model-routing/scripts/schemas/routing.schema.json).
>
> If the user asked to install this Skill, run `npx skilld install aditya-deokar/software-factory/model-routing`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/model-routing`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/aditya-deokar/software-factory/model-routing) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/model-routing`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/model-routing --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/model-routing --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Model routing

Running every job on the strongest model is the most common way a factory
gets too expensive to keep running. Triage, labeling, doc fixes, and small CI
repairs do not need a frontier model. Specs and ambiguous calls do. Routing
makes that decision explicit, records it, and lets data change it.

Rules name tiers (`fast`, `balanced`, `frontier`), never model IDs. Each
harness maps a tier to a model in one table, so a model release changes one
line, not every rule.

## Find the scripts

```bash
SKILL_DIR="$(dirname "$(find skills .claude/skills .agents/skills ~/.claude/skills ~/.agents/skills ~/.claude/plugins \
  -name SKILL.md -path '*model-routing*' 2>/dev/null | head -1)")"
```

## Route a task

```bash
node "$SKILL_DIR/scripts/route.mjs" --beat build --class ci-fix --size small
# tier fast, model haiku, rule chore-small
# ledger flags: --tier fast --model haiku --rule chore-small
```

Pass the printed flags to `run-ledger start`, so every run records which
rule sent it where. That is the data the loop learns from.

The first matching rule wins, and no match means `default`. A rule matches
when every key it names (`beat`, `class`, `size`) equals the task's value,
or is in its list.

## Default split

| Work | Tier |
|---|---|
| Triage, labels, dedupe, monitoring checks | fast |
| Docs, small chores, small CI fixes, mechanical sweeps | fast |
| Implementation slices, test-and-fix loops, PR follow-up, browser checks | balanced |
| Specs, architecture, ambiguity calls, final review of subagent work | frontier |
| Large or migration work | frontier |

## Applying the tier

The session you are in is usually on one model. Routing mostly decides what
you hand off:

- **Keep on the main thread:** planning, ambiguity calls, reviewing what
  subagents return, and talking to the person.
- **Hand to a cheaper subagent:** each implementation slice, the test-fix
  loop, PR babysitting, browser verification, and mechanical multi-file edits
  (one subagent per disjoint set of files).

How to set the model per harness:

| Harness | How |
|---|---|
| Claude Code | Subagent `model` field (`haiku`, `sonnet`, `opus`), or `claude --model` for headless runs |
| Codex | A profile per tier in `~/.codex/config.toml`, then `codex --profile <tier>` |
| Cursor | The model picker, or the model setting of the background agent |
| CI workflows | A repository variable per tier, read by the agent step |

When the harness cannot switch models, record the tier anyway. The ledger
then shows what routing would have saved, which is an argument for a harness
that can.

## Settle it with an eval

When someone thinks a rule is wrong ("haiku keeps botching CI fixes"),
measure instead of arguing:

1. Write 5 to 10 real, small tasks of that class into
   `.factory/evals/<class>/`, one markdown file each. The body is the prompt,
   and the frontmatter has a `check:` command that exits 0 on success:
   ```markdown
   ---
   check: npm test -- tests/parser.test.js
   setup: npm ci
   ---
   The parser drops the last row when the CSV has no trailing newline. Fix it.
   ```
   Past issues with known fixes make the best tasks.
2. Dry-run to see the plan and the cost:
   ```bash
   node "$SKILL_DIR/scripts/route-eval.mjs" --tasks .factory/evals/ci-fix \
     --tiers fast,balanced --k 3 --dry-run \
     --agent-cmd "claude -p --model {model} --permission-mode acceptEdits < {prompt_file}"
   ```
3. Run it with a cap: add `--budget-tokens 2000000`. Each attempt runs in its
   own throwaway worktree, so attempts cannot see each other's work.
4. Read the result table and the recommendation: the cheapest tier whose pass
   rate is within `--margin` (default 10 points) of the best one.

The result file lands in `.factory/evals/<date>-<class>.json`. The script
never edits `routing.json`. Changing a rule is a PR: edit the rule, add the
result file under `evidence` for that rule id, and put the table in the PR
body. The `routing-change` gate means a human merges it.

Evals cost real money. Never run them in CI on every push. Run them by hand,
or from a manual workflow, with a budget.

## Do not do these

- **Do not pin model IDs in rules.** Put them in `tiers`.
- **Do not route by vibes.** A rule change without an eval file or ledger
  numbers behind it is a guess. Say so in the PR if that is all you have.
- **Do not route the main thread's review work down.** A cheap model
  checking a cheap model's work compounds the misses.
