Sub topic 8
Evaluating a Routing Policy and Choosing a Threshold
Prerequisites
This chapter assumes you can read basic Python and know how a cascade works: the small model answers first, and the request escalates to the large model when the small model's confidence is below a threshold. It also assumes you understand per-token pricing, so that "cost per request" is a number you can estimate. The goal here is to measure a routing policy and pick its threshold with evidence, not by guessing.
What you are measuring
A routing policy is a rule plus its parameters. For a cascade, the parameter is the confidence threshold. Four numbers describe how a policy behaves:
| Metric | Meaning |
|---|---|
| Accuracy | Fraction of requests whose final answer is judged correct |
| Escalation rate | Fraction of requests that reach the large model |
| Expected cost | Average spend per request, including the small model call on every request |
| Expected latency | Average time to the final answer, including both calls for escalated requests |
Changing the threshold moves the policy along a curve. A low threshold keeps most traffic on the small model, which is cheap and less accurate. A high threshold escalates most traffic, which is accurate and expensive. No threshold is best in general. You choose one by stating a constraint, for example "the cheapest policy whose accuracy is within two points of always using the large model."
Two baselines frame every comparison:
- Always small: the lowest cost and accuracy you can get.
- Always large: the highest cost, and usually the highest accuracy.
A policy is only worth running if it lands meaningfully better than a straight line between these two.
Build a replay set
You cannot evaluate a threshold from production logs alone. In production, a request that the small model answered confidently never saw the large model, so you do not know whether the large model would have done better. To get both answers, build a replay set:
- Sample requests from real traffic. Cover the mix you expect, including rare but important request types.
- Run both models on every sampled request, whatever the router would have chosen.
- Record the small model's confidence score.
- Grade both answers with the same grader. This can be exact match, unit tests, human review, or a judge model.
Each record then has three fields: confidence, small_correct, and large_correct. This is the input format the sweep function expects. The cost is one-time: you pay for the large model on a few hundred requests that the router would otherwise have kept on the small model.
Sweep thresholds
The sweep replays the sample at each threshold. A record with confidence at or above the threshold keeps the small model's grade. Any other record takes the large model's grade and counts as escalated.
def sweep(records, thresholds):
# Each record: {"confidence": float, "small_correct": bool, "large_correct": bool}
results = []
for t in thresholds:
correct = 0
escalated = 0
for r in records:
if r["confidence"] >= t:
correct += int(r["small_correct"])
else:
escalated += 1
correct += int(r["large_correct"])
n = len(records)
results.append((t, correct / n, escalated / n))
return resultsHere is the sweep on a toy sample of ten records, with a cost model added. Ten records is far too few for a real decision. It is small enough to check by hand.
def expected_cost_cascade(frac_answered_by_small, cost_small, cost_large):
# Every request pays for the small model; escalated requests also pay for the large one.
return cost_small + (1 - frac_answered_by_small) * cost_large
# (confidence, small_correct, large_correct)
ROWS = [
(0.95, True, True), (0.92, True, True), (0.90, True, True),
(0.85, True, True), (0.82, False, True), (0.75, True, True),
(0.60, False, True), (0.55, False, False), (0.40, False, True),
(0.30, False, False),
]
RECORDS = [
{"confidence": c, "small_correct": s, "large_correct": l}
for c, s, l in ROWS
]
COST_SMALL = 0.0002 # dollars per request
COST_LARGE = 0.005
for t, acc, esc in sweep(RECORDS, [0.0, 0.5, 0.7, 0.8, 0.9, 1.01]):
cost = expected_cost_cascade(1 - esc, COST_SMALL, COST_LARGE)
print(f"threshold {t:4.2f} accuracy {acc:.2f} escalated {esc:.2f} cost ${cost:.5f}")
large_only = sum(r["large_correct"] for r in RECORDS) / len(RECORDS)
print(f"always large: accuracy {large_only:.2f} cost ${COST_LARGE:.5f}")Output:
threshold 0.00 accuracy 0.50 escalated 0.00 cost $0.00020
threshold 0.50 accuracy 0.60 escalated 0.20 cost $0.00120
threshold 0.70 accuracy 0.70 escalated 0.40 cost $0.00220
threshold 0.80 accuracy 0.70 escalated 0.50 cost $0.00270
threshold 0.90 accuracy 0.80 escalated 0.70 cost $0.00370
threshold 1.01 accuracy 0.80 escalated 1.00 cost $0.00520
always large: accuracy 0.80 cost $0.00500The threshold of 1.01 is a deliberate trick. No confidence can reach it, so every request escalates. The sweep therefore includes the "escalate everything" end of the curve.
Reading the results
Three patterns in this output appear in real sweeps.
Plateaus. Thresholds 0.7 and 0.8 both reach 0.70 accuracy, but 0.8 escalates more and costs more. Raising the threshold only helps if it sends wrong small answers to the large model. Between 0.7 and 0.8 it only moved a correct answer (the record at 0.75) to the large model. On a plateau, pick the lowest threshold.
A cascade can cost more than the large model alone. At 1.01, the cascade costs $0.0052 against $0.005 for always large. Every request paid for the small model and then paid for the large one. A cascade tuned to escalate nearly everything is strictly worse than skipping the small model. If your chosen threshold escalates more than roughly 80% of traffic, check whether the cascade is worth keeping.
Accuracy can match the large model before escalation hits 100%. At 0.9, the policy matches the large model's 0.80 while escalating 70% of requests. The large model is wrong on two records, and the small model is also wrong on both, so escalating them gains nothing. When the small model is right on a request where the large model is wrong, the cascade can even beat always-large. That is rare, but it happens.
Choose a threshold from a constraint
Turn the business requirement into a rule and let code pick the threshold:
def pick_threshold(rows, min_accuracy):
# rows: (threshold, accuracy, escalation_rate) tuples from sweep()
ok = [r for r in rows if r[1] >= min_accuracy]
if not ok:
return None
# Lowest escalation rate means lowest cost; ties go to the lower threshold.
return min(ok, key=lambda r: (r[2], r[0]))
rows = sweep(RECORDS, [0.0, 0.5, 0.7, 0.8, 0.9, 1.01])
print(pick_threshold(rows, 0.70)) # (0.7, 0.7, 0.4)
print(pick_threshold(rows, 0.80)) # (0.9, 0.8, 0.7)
print(pick_threshold(rows, 0.95)) # NoneNone is a real result. It means no threshold meets the quality floor, so the problem is the models or the grader, not the threshold.
How much to trust the numbers
Accuracy measured on n records has a standard error of about the square root of p(1-p)/n. For p = 0.8:
| Sample size | Standard error | Rough 95% range |
|---|---|---|
| 10 | 0.126 | plus or minus 0.25 |
| 100 | 0.040 | plus or minus 0.08 |
| 400 | 0.020 | plus or minus 0.04 |
Differences between adjacent thresholds are often one or two points, so a sample of 100 cannot separate them. Two practical consequences follow:
- Use at least a few hundred records, and more if you need to detect a one-point difference.
- Compare thresholds on the same records, as the sweep does. Because the records are shared, the comparison is paired and less noisy than the table above suggests.
To avoid fooling yourself, split the sample. Choose the threshold on the tuning half and report accuracy and cost from the holdout half. If you tune and report on the same records, the reported numbers are optimistic, because the chosen threshold partly fits that sample's noise.
Evaluating a rule-based router
The same replay set evaluates a rule-based router, because the router decision picks which grade to use. A router pays for one model per request, so its cost formula differs from the cascade's.
def evaluate_router(records, route):
# Each record also needs a "prompt" field. route(prompt) returns "small" or "large".
correct = 0
sent_small = 0
for r in records:
if route(r["prompt"]) == "small":
sent_small += 1
correct += int(r["small_correct"])
else:
correct += int(r["large_correct"])
n = len(records)
return correct / n, sent_small / n
def expected_cost_router(frac_sent_to_small, cost_small, cost_large):
# A router pays for exactly one model per request.
return frac_sent_to_small * cost_small + (1 - frac_sent_to_small) * cost_large
sample = [
{"prompt": "What is 2+2?", "small_correct": True, "large_correct": True},
{"prompt": "Prove that sqrt(2) is irrational.", "small_correct": False, "large_correct": True},
]
route = lambda p: "large" if "prove" in p.lower() else "small"
acc, frac_small = evaluate_router(sample, route)
print(acc, frac_small) # 1.0 0.5
print(expected_cost_router(frac_small, 0.0002, 0.005)) # 0.0026A router has no threshold to sweep, but you can sweep its parameters, such as the length cutoff. The method is the same.
Edge cases
Tied or clumped confidence scores. Some confidence methods return only a few distinct values, such as 0.9 or 1.0 from a model asked to state its own confidence. Then many records share a score and the curve moves in jumps. Sweep over the distinct observed values instead of an even grid.
Calibration drift. A threshold of 0.8 means whatever the scoring method makes it mean. If you change the small model, the prompt, or the scoring method, the old threshold no longer applies. Re-run the sweep after any such change.
Segments hide problems. An overall accuracy of 0.85 can hide a request type, such as code generation, where the cascade performs poorly. Compute accuracy and escalation per segment, and check that the worst segment meets your floor.
Latency is not the cost curve. Escalated requests wait for the small model and then the large one, so their latency is the sum of both. Expected latency is the small model's latency plus the escalation rate times the large model's latency. Track the slowest requests, because users notice those, not the average.
A weak grader limits everything. If the judge model disagrees with humans on 15% of grades, differences of one or two points between thresholds are noise. Spot-check a sample of grades by hand.
Stale samples. A replay set built last quarter reflects last quarter's traffic. When the request mix changes, refresh the sample.
The evaluation workflow
Common errors
- Evaluating only on requests the router kept on the small model. Production logs show only the answers that were actually served, so you cannot measure escalation benefit from them. Build the replay set with both models.
- Reporting accuracy without cost. A threshold that matches the large model's accuracy but escalates 95% of traffic saves almost nothing.
- Forgetting the small model's cost in the cascade. The cascade cost formula adds the small call to every request. Using only
(1 - frac) * cost_largeunderestimates spend. - Picking the highest-accuracy threshold. Accuracy rises with the threshold, so this always picks the most expensive option. Choose by constraint.
- Tuning and reporting on the same sample. Hold out records.
- Comparing thresholds across different samples. Use the same records for every threshold so the comparison is paired.
Exercise
Using the sweep table above, your quality floor is 0.75 accuracy. Which threshold do you choose, what does it cost per request, and what fraction of the always-large cost ($0.005) does it save?
Show answer
Only thresholds 0.9 and 1.01 reach 0.75 or more (both give 0.80). The cheaper one is 0.9, at $0.0037 per request. The saving is (0.005 - 0.0037) / 0.005 = 0.26, so 26% less than always using the large model.
Exercise
At threshold 1.01 the cascade costs $0.0052 per request, more than always-large at $0.005. Explain why, and give the formula that shows it.
Show answer
At 1.01 every request escalates, so every request pays for both models. The cascade formula gives costsmall + (1 - 0) * costlarge = 0.0002 + 0.005 = 0.0052. The small call is pure overhead when nothing is kept on the small model.
Exercise
You tune a threshold on 300 labeled records and report 0.84 accuracy on those same records. Why might production accuracy be lower, and what should you do?
Show answer
Choosing the threshold that looks best on a sample partly fits that sample's noise, so the reported number is optimistic. Split the records, pick the threshold on the tuning part, and report accuracy from the holdout part. Also check that the sample matches the production request mix.