The fallback model was the most expensive one
A one-word ticket classification was running on the flagship model at 6.7 times the price. The cause was three lines of sorting in our own router, and the tests were green the whole time.
Denys Soloviov · · 6 min read
What happened
DirectOS has a router whose whole job is to spend less on model calls. It scores each task for complexity and sends it to the cheapest model that can handle it. A support ticket that needs a one-word label should never reach a flagship model.
The week the public demo went live, I ran the demo workflows myself for the first time in production. One of them triages a support email: classify the urgency, then either page someone or draft a reply. The classification step, the one that answers with a single word, had run on Claude Opus.
| Step | Before | After the fix | Difference |
|---|---|---|---|
| Classify urgency | $0.00065 on Opus | $0.000097 on Haiku | 6.7× cheaper |
| Draft the reply | $0.0055 | $0.0028 on Sonnet | about half |
Per call these are fractions of a cent. The ratio is the point: a workflow that runs ten thousand times a day pays that 6.7× every time, and the product exists to prevent exactly this.
How routing is supposed to work
Every model in the catalog has a tier: 1 for economy models, 2 for mid-range, 3 for flagships. A prompt is classified as low, medium or high complexity, which maps to the minimum tier it needs. Among the models that meet the tier, the router picks the cheapest.
Then there are fallbacks. Providers fail: rate limits, outages, an expired key. When the chosen model errors, the router tries the next model in an ordered list, and the workflow never notices. That list is where the bug was.
Three lines of sorting
This is how the fallback list was built:
const fallbacks = pool
.filter((m) => m !== chosen)
.sort((a, b) => b.tier - a.tier || a.outputCostPerMTok - b.outputCostPerMTok);
Everything except the chosen model, strongest first. When I wrote it, the reasoning felt sound: if the first choice fails, reach for the model most likely to succeed.
But the first choice for a low-complexity task is the cheapest economy model. If it fails, “strongest first” means the very next attempt is the most expensive model in the catalog, skipping every cheap and mid-range model in between. The primary choice was optimized for cost. The fallback path silently optimized for the opposite.
Why it fired on every call
A fallback is meant to be rare. In production it was the normal path, for a boring reason: the OpenAI key on the hosted instance had no credits. Every request to it came back 429.
The router didn’t know that. For a low-complexity task it kept picking an OpenAI economy model as the cheapest capable one, got the 429, and moved to the first fallback — Opus. Each call cost one failed request plus one flagship request, and it succeeded, so nothing looked broken.
Two things kept it invisible.
- The tests checked that a fallback happened, not where it landed. The existing test made the first provider throw and asserted that a second attempt succeeded on another provider. It passed. It would have passed with any order.
- The dashboard wasn’t measuring yet. The operations page still showed placeholder numbers from the prototype, so there was no real cost-per-call figure to look wrong. That was the other thing fixed that week: every number on it is now measured.
The fix
The fallback order now mirrors the primary choice. First the other models that still meet the required tier, cheapest first, so a failure escalates one step at a time. Only after those, the weaker models, strongest first, as a last resort.
const cheapestFirst = (a: ModelProfile, b: ModelProfile) =>
a.tier - b.tier || a.outputCostPerMTok - b.outputCostPerMTok;
const fallbacks = [
// still capable: cheapest first, escalating a tier at a time
...capable.filter((m) => m !== chosen).sort(cheapestFirst),
// below the required tier: last resort, strongest first
...pool
.filter((m) => m !== chosen && !capable.includes(m))
.sort((a, b) => b.tier - a.tier || a.outputCostPerMTok - b.outputCostPerMTok),
];
And the tests now pin the order, not just the existence of a retry:
it('falls back to the next-cheapest capable model, not the flagship', () => {
const decision = router.route({ prompt: 'Say hi' });
expect(decision.model.id).toBe('local');
expect(decision.fallbacks.map((m) => m.id)).toEqual(['cheap', 'mid', 'best']);
});
it('when a low task fails on its first choice, the retry stays cheap', async () => {
// the first-choice provider throws "429 no credits"
const result = await router.chat({ prompt: 'Say hi' });
expect(result.attempts.map((a) => a.model)).toEqual(['local', 'cheap']);
});
After the deploy I ran the same workflow again on the live instance. The classification went to Claude Haiku at $0.000097 and the draft to Claude Sonnet at $0.0028.
What I took from it
- A fallback is a routing decision. It deserves the same objective as the primary choice. If the first pick optimizes for cost and the retry optimizes for “whatever works”, then under failure you are running a different product.
- Test the order, not the existence. “It recovered” is the weakest thing you can assert about a failure path. Assert which path it took.
- Failure paths only really run in production. No unit test has a key with an empty balance. The bug needed a real misconfiguration to become the default path.
- Use your own product as a stranger would. The tests were green the whole time. Running the demo and reading the cost of one step is what found it.