Sprag

A model-delegation router for Claude Code and Codex

Sprag for Claude Code + Codex

Lookups, checks and test runs go to cheaper models, documents go in as compact Markdown, and the statusline shows what you spent and saved.

$ npm i -g sprag-cli
Install Sprag How it works

Sprag Pro · for teams Your whole team's AI spend and savings, on one screen A virtual office with a character for every teammate, a seat roster, and rules the team approves once. See the team features
Same requests in two terminals, priced at public list rates. Of the $4.85 saved, doc2md accounts for $4.03 and routing for $0.81 (rounded).
sprag --statusline CLI capture · example session
Claude Code statusline with routing and document accounting Tap to enlarge

Separate agent records. Shared writing and document tools. Unknown prices stay unknown.

59.1%accuracy of the tier rules in an offline LLMRouterBench replay · beats every single model in the pool, which has no Claude model
95.9%fewer tokens per document read through doc2md · 12 files, as of 2026-09-18
$188.27Claude routing estimate · 124 recorded runs, one machine, as of 2026-10-09
2coding agents · Claude Code and Codex

Install Sprag

npm i -g sprag-cli
sprag harness check

The default install registers Claude Code hooks, its statusline and the rule presets. The only question comes when you already have a statusline: show both, replace yours, or keep yours. Add --manual to sprag install to confirm each step.

How it works

It learns from the sessions you already ran

What Sprag does with a session: rules, sub-agent delegation, document conversion, Korean prose gates and telemetry WHAT GOES IN SPRAG WHAT IT DOES WITH IT YOUR SESSION 214 past sessions every command, every failure the task you just asked for "run the tests" a document you would attach q3-review.pptx Korean prose being written docs · UI strings · comments tokens this turn is spending cache · context · caps sprag matched against your own past sessions DELEGATION · A SECOND AGENT DOES THE WORK AND HANDS IT BACK a model-fitting rule matched "run the tests" → haiku ratchet-model.md · from your logs t2-runner runs it, returns the output t1-worker writes it, returns a diff the main agent reviews then it counts as done how the tier is picked · measured from your own logs where the answer lives in the repo → cheap tier · model knowledge → up does failure make noise exit codes catch it → cheap tier · silent → up per-rule track record · illustrative ×147 runs · 14% err · caps · escalate on repeat

Sprag installs once, reads the sessions you already run, and does not ask you to change how you prompt.

01

A repeat becomes a rule

The first failure is just work. On the second, Sprag surfaces it as a candidate. You approve the scope, and the rule loads at the start of every session after that.

2026-08-01 · appending to .env
→ check the trailing newline
  first (tail -c1)
02

Safe work goes to a sub-agent

When a task matches one your history shows is safe, Sprag spawns a sub-agent on a cheaper tier to do it and hands the result back for review. Anything that fails quietly stays with the main agent.

"git status"       → t2-runner
"build the app"    → t1-worker
"why is CI flaky?" → main agent
03

Cost trouble shows up early

Cache hit rate, TTL expiry, context growth and both rate-limit windows are read every turn and printed in your statusline, while you can still act on them.

⚠ Cache miss · input spike
🚨 7D 92% · run handoff

Stop paying flagship prices for work a cheaper tier already proved it can do

Sprag does not swap your model. It reads what you have already run, finds the kinds of task that never went wrong on a cheaper tier, and spawns a sub-agent for exactly those. The sub-agent returns its output to the main agent, which reviews it before anything counts as done. Every rule carries the error rate it was measured at, and the work comes straight back to the main agent the moment a rule stops holding.

TierShare (illustrative)Goes thereWhy it is safe
haiku52% lookups · status checks · test and lint runst2-runner sub-agent Exit codes and diffs make a wrong answer obviousillustrative: 0% error over 41 delegated runs
sonnet33% builds · mid-size refactors · mechanical portst1-worker sub-agent The build and the test suite verify the resultthe main model reviews the diff before it counts as done
main agent15% diagnosis · design · security · anything irreversiblenever delegated Nothing catches a wrong call here except younever delegated, however routine it looks

How a tier gets picked: the criteria every rule shares

These ship inside the generated ratchet-model.md, above the rules, so the agent applies them to every delegation. The percentages are measured, not assumed.

01 Where the answer lives

A lookup answered by the repo, a file, or command output is safe on haiku. A question answered from model knowledge is not. Measured recall: 28% on the cheap tier vs 54% one tier up.

02 Whether a mistake surfaces

If the build, the tests, or an exit code will catch a wrong answer, the gate sets the quality floor and a cheap tier is safe. Work that fails silently (diagnosis, root-cause calls, security, design) never goes down a tier.

03 Tool orchestration

A flow that feeds one tool's output into the next call never goes to haiku, whatever the call count. The accuracy gap on chained tool work measured +63pp, the largest of any task type.

04 Batching

The dominant cost of a sub-agent is loading context, not generating. Tasks that read the same files ride in one agent; only tasks that read different ground get their own.

05 Who writes, who decides

The main model fixes the design and writes a self-contained spec; a sonnet worker writes the bulk of the code. The main model reviews the diff and the test results before anything is reported done.

06 Escalation

The same error twice, an answer splitting into candidates, or a sub-agent circling: each hands the work back. re-brief and retry, then one tier up, then the main model takes over.

Measured offline on LLMRouterBench (Findings of ACL 2026): 11,696 instances across 9 datasets. The tool-chain gap is from τ²-Bench, knowledge recall from SimpleQA-class sets. Method and holdout in docs/BENCHMARK.md.

The research behind the criteria

Each criterion traces back to published routing research. Criteria 04 and 05 have no paper behind them: they come from sprag's own cost accounting.

PaperWhat it found, and how sprag uses it
When Routing CollapsesLai & Ye · 2026As budgets grow, learned routers default to the most expensive model even when a cheaper one suffices. Sprag only ever moves clearly easy work down.
RouterArenaLu et al. · 2025Routers fall short of the oracle mainly because they miss when a smaller model is enough, and difficulty is defined by how many of 42 models answered. Sprag judges difficulty by outcome too: tool errors and retries in your logs.
RouteLLMOng et al. · 2024Routes by comparing a predicted win rate against a cost threshold. Sprag recomputes its thresholds from your own last 14 days at every scan.
FrugalGPTChen, Zaharia & Zou · 2023A cascade tries the cheap model first and a check on the answer decides whether to escalate, matching GPT-4 at up to 98% lower cost. This is criterion 06.
SimpleQAWei et al. · 2024Measures short-form factual recall on its own. The 28% vs 54% gap in criterion 01 comes from this class of set.
τ²-BenchBarres et al. · 2025Agents coordinate tool calls with a user in a shared environment. The +63pp gap in criterion 03 was measured here.
LLMRouterBenchLi et al. · Findings of ACL 2026400K instances, 21 datasets, 33 models, and several routers that fail to beat a simple baseline. Sprag's offline run uses its data.
Routing and Cascading: A SurveyMoslem & Kelleher · 2026Sorts routing systems by when the decision is made, what it reads, and how it is computed.

Everything ships in one install, working from day one

$ sprag harness promote + rule 2026-09-13: npm 401 → check NPM_TOKEN first ✓ loads at the start of every session

Ratchet rules

Repeated failures become one-line rules loaded every session. Candidates are detected from your logs; you choose project or global scope.

# ratchet-model.md (illustrative) "run the tests" → haiku ×41 runs · 0% err · saved $3.05

Model fitting

Log-driven delegation rules with measured error rates and reported savings, written to ratchet-model.md.

🅷 5/5 ▰▰▰▰▰ ratchet · evidence · plan-verify structured · safe-path

Harness score

Five operating principles checked live. Skip the verify step and 🅷 4/5 says so before you report done.

🧠 91% ⏳ 4m 🚨 5H 94% cache ▰▰▰▰▰▰▰▰▰▱ · ctx 118k 💵 Sep $42 · 🔑 34% $34/$100

Token telemetry

Cache hit rate, TTL, context size, output spikes and both rate-limit windows, in the prompt every turn.

q3-review.pptx 30MB 540,429 tok → 22,610 tok ✓ −95.8% before the model reads it

doc2md

pptx, xlsx, pdf, docx and fig converted on demand, so a document costs a read instead of a context bomb.

✗ 보여집니다 (이중 피동) ✓ 보입니다 blocked at write time, by hook

Style gates

Write-time prose lint enforced by hook. Shipping today for Korean technical writing: double passives, translationese, cohesion.

Questions people ask first

Is this the same tool as claude-token-saver?

Yes. The current npm package is sprag-cli. The old claude-token-saver package is deprecated; existing configuration is preserved when you migrate.

Does it call an LLM or need an API key?

Local Claude Code and Codex log analysis needs no API key or extra model calls. Optional Codex budget and price lookups contact your configured LiteLLM endpoint with its existing credentials. Session content is not uploaded.

Will it change how I prompt?

No. It installs hooks and a statusline once, then works from what you already do. Rules, delegation, and warnings show up inside your normal sessions.

Where do the savings numbers come from?

The historical figures above come from Claude Code. Codex keeps a separate ledger for Sprag-routed child runs with matching provider prices. Both compare the same observed tokens at two rates: an estimate, not a billing measurement. Missing prices remain unknown.

Does Codex use the same statusline?

Codex gets a companion terminal panel or an inline tmux layout. Its native footer does not execute shell commands. See the Codex guide for setup, hook trust, and platform differences.

Read the docs

Install it once. Every session after gets safer and cheaper.