Design, implementation and evaluation
Cost-Quality LLM Router
An OpenAI-compatible gateway that sends each request to the cheapest model that can still answer it, and measures the trade instead of assuming it.
- Cost cut at 96.9% quality retention (95% bootstrap CI, n = 187)
- 17.0%
- Expected calibration error of the routing probability
- 0.024
- Median routing overhead added per request
- 20.9 ms
The problem
Sending every request to the strongest model is the safe default and the expensive one. Routing each prompt to a cheap model sounds like a win until you have to answer the follow-up: how much quality did the saving cost, and was the measurement trustworthy? Quality scores from a model grading itself are the easy answer and the weak one.
The goal was a router that behaves like a drop-in OpenAI-compatible endpoint and reports a real, auditable cost-quality trade across a 12.5x price gap.
Approach
- OpenAI-compatible gateway. FastAPI with bearer auth, a 1 MB body cap and overwritten client
modelfields, so callers keep their existing SDK and only change the base URL. - Shared feature extractor. MiniLM embeddings (PyTorch or ONNX int8 with an LRU cache) plus bounded rule flags for code, math and structural cues, capped at 20k characters.
- Calibrated classifier. A
HistGradientBoostingmodel (hgb_both) predictsP(cheap ok); sigmoid calibration brings ECE to 0.024. Ablations cover logistic regression, kNN and a RouteLLM-style embedding router. - Selection discipline. Stratified 60/20/20 splits per domain, τ tuned on validation and frozen before the test split is touched.
- Guardrails and fallback. Hard rules send tool, image and very long requests straight to the strong model; errors, empty responses and truncations fall back automatically, with an uncertain-band check around τ.
- Telemetry. SQLite
request_logandshadow_logtables,/stats, a Streamlit dashboard and a shadow-drift report. - 85 tests, one skipped, covering graders, features, train-serve parity, gateway security and the ONNX export.
- 01OpenAI-compatible chat request
- 02Hard rules: tools, images, long prompts
- 03Embeddings plus bounded rule features
- 04Calibrated P(cheap ok) against τ = 0.737
- 05Cheap answer, with fallback on failure
- 06Telemetry, shadow log, cost accounting
Results
Held-out test split, n = 187, τ tuned on validation.
| Target | Method | Routed cheap | Retention (95% CI) | Savings (95% CI) |
|---|---|---|---|---|
| 95% | ML router | 13.4% | 96.9% [93.3, 100.0] | 17.0% [7.3, 28.3] |
| 95% | Length heuristic | 7.0% | 99.4% [97.5, 101.3] | 15.3% [4.9, 27.0] |
| 95% | Oracle bound | 62.0% | 104.3% [101.2, 108.1] | 54.5% [44.7, 63.6] |
| 99% | ML router | 1.1% | 99.4% [98.1, 100.0] | 2.4% [−0.1, 7.1] |
Anchors: always-strong is 87.2% accuracy at $0.000238/request; always-cheap is 52.9% at $0.000019. Human-judge validation on 40 turns gives 95.0% agreement, Cohen's κ = 0.828.
- science_qa32.6%
34.6% routed cheap · 96.0% quality retained
- chat28%
7.7% routed cheap · 104.5% retained
- docqa25.1%
27.3% routed cheap · 84.2% retained
- knowledge19.5%
13.9% routed cheap · 96.7% retained
- code−0.1%
Nothing routed cheap · 100.0% retained
- math−0.1%
Nothing routed cheap · 100.0% retained
The router learns the useful shape without being told: it protects math and code entirely and harvests savings on retrieval-style questions.
What went wrong
The first result was leakage. The initial evaluation reported a saving that was too good to be true because near-duplicate prompts sat on both sides of the split. Rebuilding the split as stratified by task type and difficulty removed the leak and moved the headline from implausible to defensible. That is why the project reports bootstrap intervals and a kappa next to every percentage.
The margin over a tuned heuristic is not yet separable. At the 95% target the ML router beats the length heuristic by +1.68 points, but the paired bootstrap interval is [−8.01, +10.32] with P(ML better) = 0.65. At n = 187 that is a positive but inconclusive result, and the README says so. Expanding the benchmark split is the next step, not a redesign.
Limits. Maximum savings are bounded by the real price ratio between the two models. chat and docqa labels depend on an LLM judge, validated on only 40 human-graded turns. The classifier scores the latest user message, so multi-turn state is invisible, and multimodal or tool-calling requests always take the strong path. Cascade mode escalates on errors, empty responses and truncation; logprob- or verifier-based escalation is future work.