← All work

Applied ML, LLM Infrastructure

CR

9.2%
Of the bill saved at 97.1% quality, on 205 held-out prompts
91%
Quality weighted by task size, since the misses are big tasks
+5 pts
Quality vs random routing at the same model mix (risk threshold)
How CR routes a turn, in a minute and a half. Made with Manim; the router outputs and benchmark numbers are real.

Pipeline

  1. 01

    Read

    A fine-tuned Laya model answers two questions about the turn: how hard is it, and how long will a complete answer be

  2. 02

    Estimate

    A small regression turns predicted answer length and prompt length into the turn's expected Opus cost

  3. 03

    Route

    A risk threshold or a cost-aware score picks Haiku, Sonnet, or Opus for the quality target you set

  4. 04

    Run

    Claude Code runs the turn headless on that model and resumes the same session, so context carries across switches

What it is

CR is Claude Code with a router in front. You run it in a project directory and it works like Claude Code: it reads and edits files and runs commands. The difference is that every turn first goes to a small self-hosted model that decides whether Haiku, Sonnet, or Opus should answer it. Haiku costs a fortieth of Opus per output token and Sonnet half, so every turn that doesn't need Opus saves money or, on a Pro/Max plan, usage limit.

You don't pick a model; you pick a quality target, such as 95% of answers as good as Opus, and CR uses the routing setting that met it on a benchmark. Each turn prints which model it used, why, what it cost, and what Opus would have cost.

The router

The router is Laya, an open 0.4B-parameter ModernBERT encoder that answers typed questions with probabilities instead of generating text. It runs on CPU on an always-free Oracle Cloud Ampere A1 instance, bound to localhost and reached over an SSH tunnel, so a routing decision costs nothing but about 2.7 seconds.

Out of the box it couldn't tell easy prompts from hard ones; zero-shot, its routing did no better than random. So I built training data from 1,500 prompts drawn from OpenAssistant, Dolly, and Arena-Hard. Opus acted as a teacher and estimated which model each prompt needed, recalibrated against real judge verdicts, and about 300 of the prompts were also answered by all three models and judged directly. Haiku answered nearly all 1,500 to supply answer-length labels: Haiku and Opus answer lengths correlate at 0.90, and Haiku is far cheaper to run. I fine-tuned on a Kaggle T4 with both questions at once.

Two routing rules

The first rule is a risk threshold: send the turn to the cheapest model whose estimated chance of falling short of Opus is below ε. One number trades quality for savings, and each router keeps a stored curve of what every ε achieved on the benchmark, so a quality target maps straight to a setting.

That saves turns, but most of a bill sits in a few long answers; on the first benchmark the five most expensive prompts were 44% of the total cost. The second rule weighs dollars instead. A regression turns the predicted answer length into an expected Opus cost, and each model gets a score: what it would save on this turn, minus λ times its chance of falling short. Cheaper models get risked where answers are expensive, which is where the money is. It is also where they most often fall short, which the benchmark below measures.

Benchmark

Every benchmark prompt was answered once by all three models, and an Opus judge compared the Haiku and Sonnet answers with Opus's in both orders. If the two orders disagree it counts as a tie, and a win or tie counts as "as good as Opus". Everything is cached, so any routing policy can be replayed offline without new calls. Settings are chosen on a 155-prompt tune split and reported on a 205-prompt test split.

Counting prompts hides where the losses land, so the table also weights each prompt by what it costs on Opus. The cost-aware rule saves by sending the biggest jobs to Haiku, and the bigger the job, the more often Haiku's answer loses to Opus's: 41% of the time on the cheapest quarter of prompts, 94% on the most expensive. At 93.7% quality it saves 28% of the bill, but 25 of those 28 points come from the 13 prompts judged worse, which together are a quarter of the work. The risk threshold's misses are mostly small prompts, so its weighted quality stays high, but it saves less.

I quote the cost-aware rule at 97.1% quality: 9.2% of the bill saved, 91.1% weighted. A loss means the judge preferred Opus's answer, not that the cheaper answer was wrong.

Cost-aware, 97.1% quality (91.1% weighted)
9.2% of the bill saved
Cost-aware, 95.6% quality (77.5% weighted)
24.8% saved
Cost-aware, 93.7% quality (74.5% weighted)
28.0% saved
Risk threshold, 95.6% quality (96.4% weighted)
5.0% of the bill, 11.5% per prompt
vs random routing at the same mix
Risk threshold: +5 quality points. Cost-aware: about the same quality, 2.4× the savings
Length signal
Predicted length vs log Opus cost: r = 0.77 (prompt length alone: 0.25)
Router cost
$0 per call, self-hosted; about 2.7 s per turn

Running on a subscription

CR doesn't call the API. Each turn runs the official Claude Code CLI headless under your own Pro/Max login with the routed model, resuming the same session, so the conversation keeps its context when the model changes. API keys are stripped from its environment so it can't silently bill an API account, and it never reads the subscription's login token.

Costs are reported in API-equivalent dollars, a proxy for how much of the usage limit each model uses. Claude Code reports a running total for a resumed session, so per-turn cost is the difference between totals. An early version of the cost line missed this and overstated later turns.

  • Thanks and sign-offs skip the router and go to Haiku, since they ask for no work. "ok" and "go ahead" are still routed, because after a plan they mean "do it".
  • Short follow-ups such as "yes, do it" are routed together with the request they answer, so a one-line reply to a hard task doesn't drop to Haiku.
  • If the router is unreachable, the turn goes to Opus and says so. An outage costs money, never quality.

Honest limits

The benchmark shows the idea works; it doesn't settle every number.

  • Opus judges answers from its own family, which likely favors Opus, so savings probably err low rather than high.
  • 205 test prompts is enough to rank routing rules, not to pin exact percentages. At a 95% target, the tune split couldn't separate two settings that saved 9% and 25% on test, so CR keeps the safer one.
  • In an agent session every turn re-sends the whole context, about 38K tokens even for a one-word reply, so turns cost about the same and the length signal helps less. The interactive CLI defaults to the risk threshold; the cost-aware rule is for long-answer work.
  • Switching models loses the prompt cache, because the new model re-reads the full context. The router doesn't account for that yet.
  • The difficulty signal is cautious: even "What is the capital of France?" gets only a 42% chance of simple, so at a 95% target easy turns go to Sonnet rather than Haiku.
Source on GitHub ↗︎

Python · Laya · ModernBERT · Claude Code · Kaggle T4 · OCI