← Engineering blog

Is your model router costing you more than it saves?

What real routing looks like, and how a cache setting we hadn't accounted for broke ours.

Ours was, in one place. We hadn't accounted for it.

When we wrote up how we got 5x the work from our AI subscriptions, the part people asked about was routing. The usual picture is simple: send the easy steps to a cheaper model and keep the hard ones on the best one. Pick a model, and the work gets done.

That picture loses money in two ways, and we have run into both. The cheaper model may not do the job, so you pay again for the retry and the rework. And the cheaper route may break your prompt cache, so you pay more for the same work than you did before you started saving. Routing that works means the evidence holds, the cache stays intact, and you don't end up paying more than you save. Each of those has a lot of small ways to go wrong. This post is what we do about it, and one time it went wrong for us.

Two ways routing loses money

What we do

Three habits, and none of them is clever.

A step moves only where there's evidence. Our routing decides based on evidence: a combination of public benchmarks, public evidence and our own measured evidence, and our own evidence takes precedence. Where we have no recorded evidence that a cheaper model does the job, the step stays on the model your agent asked for.

We measure the plain tool on the same tasks. We run the same tasks through plain Claude Code and through Claude Code behind Agent OS, and we compare what each one cost and what each one delivered. A saving that comes with worse work isn't a saving, and a saving that only shows up in the router's own accounting isn't one either.

We check the cache the way we check the answer. Prompt-cache reads are the cheapest tokens in the system, and a cache write costs more than a plain input token. So for every arm of every comparison we look at cache reads and cache writes, not just the total.

How a cache lifetime broke our routing

In one of those comparisons, the two arms behaved differently in a way nobody had designed. The plain Claude Code arm switched from the 1-hour cache to the 5-minute cache after its first call. The Agent OS arm never did: it kept writing the 1-hour cache for the whole run. At Anthropic's published prices, a 5-minute cache write costs 1.25x the base input price and a 1-hour write costs 2x. The routing that was supposed to make the work cheaper was making one line item dearer.

Cache write prices relative to base input

We went to the client. Claude Code chooses the cache lifetime on every request. When the previous response carries rate-limit headers showing the account has moved into overage, it drops from the 1-hour cache to the 5-minute cache and stops sending the extended-lifetime header. We read that behaviour from the client itself, and Anthropic's own documentation now states the rule: once you go over your plan's limit and draw on usage credits, Claude Code drops the main conversation to the 5-minute cache, and you can choose the lifetime yourself with the promptCacheTtl setting. See How Claude Code uses prompt caching. And our proxy wasn't passing those headers back, on either the buffered path or the streaming path. The client behind Agent OS never learned the account was in overage, so it carried on writing the pricier cache. The proxy never rewrote the cache lifetime. It hid the information the client needed to choose the cheaper one.

We confirmed it with a live flip against a local recorder: with the overage headers present, the client moved from 1 hour to 5 minutes; without them, it stayed on 1 hour. Then we reproduced the whole path:

Path What the client did
Direct to the provider 1 hour, then 5 minutes
Through the old proxy stayed on 1 hour (the bug)
Through the fixed proxy 1 hour, then 5 minutes, same as direct

Before and after: the proxy passing the header on

The fix has two parts. The proxy now relays the provider's rate-limit headers, a fixed allow-list of named headers with each value checked against a strict format so nothing else gets through, so the client sees what it would see talking directly. And the relay is cleared and refilled from each response, so a locally refused request or a retry can never leave a stale header behind. The test that matters is the plain one: through the proxy, the cache lifetime must match a direct call. It's in the suite now.

That was one cause

Here is what we took from it. Most people know a cache can break. Fewer people realise how many different things can break it, and that most of them are changes nobody on your side made.

A prompt cache matches a prefix: the tools, then the system prompt, then the conversation so far. Anything that changes earlier in that prefix changes everything after it. So the list of things that can break a cache is long, and it has three kinds of owners.

A change early in the prompt loses the cache after it

What changes What it does to the cache Who changes it
A rate-limit header the client reads The client changes the cache lifetime (1 hour to 5 minutes, or back) The provider and the client. We hit this.
A new block at the front of the system prompt The part that gets cached now includes something different every session, so the first call is cold The client. We hit this too: Claude Code began sending a per-session billing block first, and our proxy, which makes sure Claude Code's own identity prompt leads the system prompt, added another copy in front of it. That pulled the per-session block into the cached part, so every session's first call started cold. The proxy now passes that layout through untouched.
A tool's name, description or parameters Changing any of them invalidates the whole cache: tools, system prompt and conversation Your tools, your agent, or a plugin
The model a step runs on We treat a switch as starting cold, so our router only switches when the saving pays for re-writing the cache The router. This one is routing's own bill.
Request settings Changing tool choice, or adding or removing images, invalidates the conversation part of the cache. Changing thinking or effort settings does too, and on some models more. Toggling web search, citations or speed settings invalidates the system prompt and the conversation Your agent or your client
A timestamp, a request id or a working-directory path early in the prompt The prefix is different every time, so nothing before it can be reused. The documentation advises keeping varying content like timestamps after the cached part Your prompts and your tools
Time The cache expires after its lifetime, 5 minutes by default, with a 1-hour option at additional cost The clock
Parallel requests A cache entry only becomes available after the first response starts, so the documentation advises waiting for the first response before sending the rest. Requests started together can't read each other's cache Your agent

We have been bitten by the first two. The others are documented, and a router that switches models, trims context or rewrites prompts touches several of them by its nature. That's why we don't treat the cache as a feature to tick off. It's a property we re-check, because the list keeps moving: a client update can add a row, and a new provider feature can add another.

Why this is a full-time job

If you're running your own routing, every row above is yours to watch, across every client and every provider you use. Header semantics, prompt layouts and cache behaviour change with client updates, and the savings from routing are only as good as the cache underneath them.

At Ekamyth, watching this is our job. We compare Agent OS against the plain tool, and we fix what costs more. The case above was ours, found that way and fixed.

If you're doing it yourself

  • Log cache read and cache write tokens per run, separately, and compare the plain tool with the routed one on the same tasks.
  • Check the cache lifetime a client actually uses through your proxy against a direct call. Make it a test.
  • Treat a model switch as a cost: price in re-writing the cache before you switch.
  • Re-run the comparison when the client updates.
  • Don't count a saving until the work comes out the same.

We're not publishing a dollar or percentage figure for this incident. A real before-and-after needs subscription-account traffic, and our API-key test runs never see these headers. When we have it, we will show it.

Ekamyth Agent OS

Put your agents to work.

Start using Agent OS