Post-mortem · UncommonRoute

When Mid-Trajectory Model Routing Loses Money Under Prompt Caching

Anjie Yang · · 5 min read

Earlier this year I built UncommonRoute (opens in new tab), a local OpenAI/Anthropic proxy (700+ stars) that scores request difficulty locally in under a millisecond (using structural features and character n-grams) and routes coding-agent calls across model tiers. On TwinRouterBench (opens in new tab)’s live SWE-bench Verified slice (100 tasks: 82 Django, 18 Astropy), a sister router built with frozen bge-small-en-v1.5 embeddings and logistic regression solved 75/100 vs. Opus 74/100 while cutting OpenRouter spend (billed across input, cache read, cache write, and output) from $54.73 to $25.66 (−53%).

The repo is still live under CommonstackAI, though I stepped back from maintaining it. In the 30-turn cost model below, frequent switching costs $1.14 with a warm cache and $1.81 after two late cache misses. Staying on the frontier model costs $1.58. Switching once from frontier to small costs $0.79 under the same assumptions.

Why short eval runs and long coding sessions diverge

That $54.73 Opus benchmark total averages $0.55 per task on short, bounded issue runs (and 75 vs. 74 on a 100-task two-repo slice is not broad proof of quality parity). Real sessions in Claude Code, Cursor, Codex, or OpenClaw behave differently. A typical debugging or feature loop runs 30 to 60 turns, carrying a ~12k-token static prefix (instructions, tool schemas, repo rules) plus appended file reads, diffs, and test output up to 60k–85k+ tokens.

Prompt caches are scoped per model. Sending an easy turn from Frontier Model A to a 5× cheaper Model B does not erase Model A’s KV cache if you come back within its TTL with an intact prefix. What it does immediately is force a cold prefill of the whole context on Model B, followed by catch-up cache writes when Model A resumes over the turns Model B produced.

Where single-turn switching stops saving

Take a standard 5× tier gap: a frontier model at $5 / $25 per million input/output tokens ($0.50 cached read, $5.00 implicit 1.0× write or $6.25 explicit 1.25× write) and a small model at $1 / $5 ($0.10 cached read, $1.00 implicit or $1.25 explicit write). With 2,500 new input tokens and 500 output tokens per turn, a cold small-model turn stops beating a warm frontier turn once accumulated context passes:

  • 30,000 tokens when both models bill 1.25× explicit cache writes (Anthropic-style);
  • 40,000 tokens when both models use 1.0× implicit caching with no write surcharge (OpenAI/Gemini-style);
  • 46,250 tokens in a mixed setup (1.25× explicit frontier vs. 1.0× implicit small).

Comparing both apples-to-apples setups at 60,000 accumulated context tokens (2,500 new input, 500 output):

Turn scenario at 60k prefix (2.5k new, 500 out) Cost vs. Warm
Warm Frontier, explicit 1.25× write (60k read + 2.5k write) $0.058 1.0×
Cold Small, explicit 1.25× write (62.5k write) $0.081 +39%
Warm Frontier, implicit 1.0× cache (60k read + 2.5k input) $0.055 1.0×
Cold Small, implicit 1.0× input (62.5k input) $0.065 +18%
Subsequent warm Small turn (explicit 1.25× write) $0.012 −80%
Cold Frontier return after TTL expiry or prefix rewrite $0.403 6.9×

Past 30k–40k tokens, the first turn routed to a 5× cheaper model already costs 18–39% more than staying on the warm frontier model. You only come out ahead if the session stays on the small model for several turns in a row without hurting quality.

30-turn model: ping-pong vs. one-way switching

To keep every number easy to check, model a 30-turn session under explicit 1.25× write pricing: 12,000-token static prefix, +2,500 tokens added per turn (ending at 84,500 tokens; 1.4475M cumulative input tokens), and 500 output tokens per turn. Every routed schedule runs the same 12 Frontier turns and 18 Small turns, changing only how it switches:

  • Ping-pong (18 switches): Frontier on turns 1–2, 5–6, 9, 12, 15, 18, 21, 24, 27, 30 and Small on the other 18 turns, tested with 0 misses (warm TTL), 2 late Frontier TTL misses (turns 18 and 27 at 54.5k and 77k context), and all-cold misses (broken prefix translation on every switch).
  • One-way switch (1 transition): Frontier on turns 1–12, then Small on turns 13–30 with no jump back.
  • $3.99 Ping-pong (all-cold)
  • $1.81 Ping-pong (2 misses)
  • $1.58 Sticky cached
  • $1.14 Ping-pong (warm TTL)
  • $0.79 One-way switch
$0 $1 $2 $3 $4 Turn 1 Turn 10 Turn 20 Turn 30
30-turn model (12k base, +2.5k tokens/turn, 500 output tokens/turn, explicit 1.25× cache write). Sticky uncached frontier ($7.61) sits off-scale above $4.50 and is included in the table below.
30-turn schedule (routed runs: 12 Frontier + 18 Small) Total vs. Sticky
Sticky Frontier (uncached baseline) $7.61 4.8×
Ping-pong, 18 switches (cold miss every switch) $3.99 +152%
Ping-pong, 18 switches (2 Frontier misses at 54.5k & 77k) $1.81 +14%
Sticky Frontier (prompt cached) $1.58 1.0×
Ping-pong, 18 switches (0 misses, warm TTL) $1.14 −28%
One-way switch (12 Frontier to 18 Small, 1 transition) $0.79 −50%

Two things jump out when you read all six lines together. With frequent ping-pong switching, warm-TTL caching saves $0.44 over sticky caching ($1.14 vs. $1.58 on rounded totals; $0.45 unrounded), yet just two cold frontier returns at 54.5k and 77k tokens push total spend to $1.81 (+14%) before counting any retry loop from a weaker edit. Meanwhile, the exact same 12F/18S split arranged as a single one-way transition drops cost to $0.79 (−50%) because neither model pays repeated catch-up writes.

What shifts the break-even in 2026

  • Thinking and reasoning tokens: The model above holds output at 500 tokens/turn. When a frontier model emits 2,500 thinking + output tokens per turn ($0.0625 vs. $0.0125 on the small tier), the single-turn break-even climbs from 30k–40k to 83k tokens (explicit 1.25×) or 120k tokens (implicit 1.0×). Output-heavy reasoning turns are far more forgiving to route than short tool calls.
  • 1-hour cache TTLs: Extended 1-hour retention makes idle TTL expiry during slow test runs much rarer than 5-minute TTLs. It still does not remove the cold prefill on the small tier or prefix misses when a cross-provider proxy rewrites thinking blocks or tool-call IDs.

Where routing still works well

Step-level classifiers still solve a real evaluation problem inside TwinRouterBench (opens in new tab), and routing pays off cleanly whenever it respects cache boundaries:

  • Turn-0 routing: pick the model once before warming the KV cache, then stay sticky.
  • Isolated subagent forks: send bounded jobs (repo search, log summarization, test triage) to a small model with a fresh, short context so there is no 60k prefix to re-prefill.
  • One-way phase switches: as the $0.79 line shows, switching once across phases (frontier planning to small mechanical edits, or a cheap draft escalating once to a frontier model if checks fail) keeps cache locality intact.