Skip to content
AgenticAIWiki
  1. Learn
  2. Part 13
  3. 71. Cost at scale
  4. 71.2

Lesson 71.2 · 2 of 6 · 4 min read · Reviewed 24 Nov 2026

Model routing for cost

By the end, you can

  • Route requests to different models by difficulty.
  • Check routing doesn't hurt quality.

Chapter 63 introduced confidence routingRouting: A workflow pattern that classifies an input and sends it to a specialised prompt, model or handler.. At scale, routing is one of the biggest cost levers.

Routing strategies

Strategy How Good for
Static by task Classification goes to the small model; long reasoning to the large one Clear task types
Rules on input Long documents or many tools go to the larger model Simple and predictable
Classifier router A cheap model labels difficulty first Mixed traffic
Cascade Try the small model; escalate if unsure or a check fails When most requests are easy
request --> small model --> passes checks? --> done
                         \-> unsure or fails --> larger model --> done

Guard quality

  • Evaluate each route separately. The overall average can hide a route that fails often.
  • For cascadesModel routing: Sending each request to the cheapest model that can handle it well, often using a classifier., the “unsure” signal must be reliable: schema validation, a confidence field checked against calibrationCalibration: Checking that a grader’s scores agree with expert human judgement, and adjusting it until they do. data, or a second check.
  • Watch for drift. A route that was fine last month may degrade as inputs change (67.4).

Count the full cost

A cascade pays for the small call even when it escalatesEscalation: Handing a case from an agent to a person, with the context needed to continue.. If 60% of requests escalate, the cascade may cost more than going straight to the larger model.

Key points

  • Many requests are easy; send them to a smaller, cheaper model and save the larger model for hard ones.
  • Route by rules (task type, input size), by a small classifier, or by escalating when the small model is unsure.
  • Evaluate each route on its own slice of traffic; a router is only as good as its worst route.
Hands-on labRuns in the Playground

Route requests and score each route

Route requests by simple rules and check every route against the quality bar, not just the average.

You will have: A router with a per-route eval table, a second rule set that fixes the weak route, and the cascade break-even.

Steps

  1. Run the starter: overall accuracy is 91%, which looks fine.
  2. Print accuracy per route and flag routes below the 0.9 bar.
  3. Change the rule for the weak route and compare quality and cost.
  4. Work out the escalation rate where a cascade costs the same as always using the large model.

Done when

  • extract->small is flagged at 67%.
  • Rules v2 have no weak routes and still cost less than all large (80 against 110).
  • The cascade breaks even at 60% escalation.

Open in the Playground No API key needed.

Starter code (Python)
# Route requests by rules, then evaluate each route on its own slice, not only on average.
SMALL, LARGE = 2, 5  # illustrative credits per request
BAR = 0.9            # quality bar every route must meet

# (kind, input tokens, small model correct?, large model correct?) from a labelled eval set
REQUESTS = (
    [("classify", 800, True, True)] * 10
    + [("extract", 1500, True, True)] * 4 + [("extract", 1500, False, True)] * 2
    + [("reason", 3000, False, True)] * 3 + [("reason", 3000, True, True)]
    + [("classify", 12000, False, True)] * 2
)


def route(kind, tokens, rules):
    if tokens > 8000:  # long inputs always go to the larger model
        return "large"
    return rules.get(kind, "large")


def evaluate(rules):
    per_route, cost = {}, 0
    for kind, tokens, small_ok, large_ok in REQUESTS:
        r = route(kind, tokens, rules)
        ok = small_ok if r == "small" else large_ok
        cost += SMALL if r == "small" else LARGE
        key = f"{kind}->{r}"
        hit, n = per_route.get(key, (0, 0))
        per_route[key] = (hit + ok, n + 1)
    total_ok = sum(h for h, _ in per_route.values())
    print(f"overall accuracy {total_ok / len(REQUESTS):.0%}, cost {cost} credits (all large: {LARGE * len(REQUESTS)})")
    weak = []
    # TODO 1: print each route's hits/n and accuracy, and add routes below BAR to weak
    return weak


print("rules v1")
weak = evaluate({"classify": "small", "extract": "small", "reason": "large"})
print("weak routes:", ", ".join(weak) or "none")
print("rules v2")
# TODO 2: change the rule for the weak route and compare quality and cost
weak = evaluate({"classify": "small", "extract": "small", "reason": "large"})
print("weak routes:", ", ".join(weak) or "none")

# A cascade always pays for the small call; escalations pay for both.
break_even = 0.0  # TODO 3: solve SMALL + e * LARGE == LARGE for e
print(f"cascade breaks even with always-large at {break_even:.0%} escalation")
Show a solution
# Route requests by rules, then evaluate each route on its own slice, not only on average.
SMALL, LARGE = 2, 5  # illustrative credits per request
BAR = 0.9            # quality bar every route must meet

# (kind, input tokens, small model correct?, large model correct?) from a labelled eval set
REQUESTS = (
    [("classify", 800, True, True)] * 10
    + [("extract", 1500, True, True)] * 4 + [("extract", 1500, False, True)] * 2
    + [("reason", 3000, False, True)] * 3 + [("reason", 3000, True, True)]
    + [("classify", 12000, False, True)] * 2
)


def route(kind, tokens, rules):
    if tokens > 8000:  # long inputs always go to the larger model
        return "large"
    return rules.get(kind, "large")


def evaluate(rules):
    per_route, cost = {}, 0
    for kind, tokens, small_ok, large_ok in REQUESTS:
        r = route(kind, tokens, rules)
        ok = small_ok if r == "small" else large_ok
        cost += SMALL if r == "small" else LARGE
        key = f"{kind}->{r}"
        hit, n = per_route.get(key, (0, 0))
        per_route[key] = (hit + ok, n + 1)
    total_ok = sum(h for h, _ in per_route.values())
    print(f"overall accuracy {total_ok / len(REQUESTS):.0%}, cost {cost} credits (all large: {LARGE * len(REQUESTS)})")
    weak = []
    for key, (hit, n) in sorted(per_route.items()):
        flag = "" if hit / n >= BAR else "  BELOW BAR"
        if flag:
            weak.append(key)
        print(f"  {key:<16} {hit}/{n} {hit / n:.0%}{flag}")
    return weak


print("rules v1")
weak = evaluate({"classify": "small", "extract": "small", "reason": "large"})
print("weak routes:", ", ".join(weak) or "none")
print("rules v2")
weak = evaluate({"classify": "small", "extract": "large", "reason": "large"})
print("weak routes:", ", ".join(weak) or "none")

# A cascade always pays for the small call; escalations pay for both.
break_even = (LARGE - SMALL) / LARGE
print(f"cascade breaks even with always-large at {break_even:.0%} escalation")
Quick checkYour app uses the largest model for every request, including simple yes/no classification. What's a good first step?
How sure are you?
Pick an answer and how sure you are