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.
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 featuresnpm i -g sprag-cli
One command sets up Codex too when it is installed, with the rule presets registered globally. Then trust the Sprag hooks in Codex /hooks; Codex requires that step. Codex only: npm i -g sprag-cli --ignore-scripts then sprag install --agent codex.
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.
The harness score rides in the line: one point for each of the five principles the session is holding to.
Conversion savings are a lifetime total with a per-format breakdown, and it takes the top row whenever it has saved more than routing has.
This one has no chip. The check runs as the text is written, and a violation stops the session on that file instead of waiting to be read off a status line.
Cache hit rate, the expiry countdown, context use and both rate-limit windows are read every turn, and a warning chip moves to the front of the line when one of them needs you.
Sprag installs once, reads the sessions you already run, and does not ask you to change how you prompt.
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)
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
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
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.
| Tier | Share (illustrative) | Goes there | Why it is safe |
|---|---|---|---|
| haiku | 52% | 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 |
| sonnet | 33% | 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 agent | 15% | diagnosis · design · security · anything irreversiblenever delegated | Nothing catches a wrong call here except younever delegated, however routine it looks |
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.
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.
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.
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.
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.
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.
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.
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.
| Paper | What it found, and how sprag uses it |
|---|---|
| When Routing CollapsesLai & Ye · 2026 | As 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. · 2025 | Routers 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. · 2024 | Routes 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 · 2023 | A 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. · 2024 | Measures short-form factual recall on its own. The 28% vs 54% gap in criterion 01 comes from this class of set. |
| τ²-BenchBarres et al. · 2025 | Agents 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 2026 | 400K 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 · 2026 | Sorts routing systems by when the decision is made, what it reads, and how it is computed. |
$ sprag harness promote
+ rule 2026-09-13: npm 401 → check NPM_TOKEN first
✓ loads at the start of every sessionRepeated 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.05Log-driven delegation rules with measured error rates and reported savings, written to ratchet-model.md.
🅷 5/5 ▰▰▰▰▰
ratchet · evidence · plan-verify
structured · safe-pathFive 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/$100Cache 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 itpptx, xlsx, pdf, docx and fig converted on demand, so a document costs a read instead of a context bomb.
✗ 보여집니다 (이중 피동)
✓ 보입니다
blocked at write time, by hookWrite-time prose lint enforced by hook. Shipping today for Korean technical writing: double passives, translationese, cohesion.
Yes. The current npm package is sprag-cli. The old claude-token-saver package is deprecated; existing configuration is preserved when you migrate.
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.
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.
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.
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.