Sub topic 8
Evaluating a routing policy and choosing a threshold
This chapter assumes you can write basic Python and call a hosted model through an API. It also assumes you know what per-token pricing is and how a cascade escalates from a small model to a large one. Here you will measure a routing policy on labeled data and pick a confidence threshold with evidence instead of a guess.
What you are measuring
A routing policy trades quality against cost. To judge one, you need three numbers for each setting you try:
- Accuracy: the share of requests whose final answer is correct.
- Escalation rate: the share of requests sent to the large model.
- Cost per request: computed from the escalation rate and the per-call prices.
You also need two reference points. Always-small is cheap but lower in quality. Always-large is the highest in quality and the most expensive. A good policy lands between them, close to always-large on accuracy and close to always-small on cost.
Build a labeled sample
Replaying a policy needs a sample of real requests. For each one, run both models and record three things:
- The small model's confidence score.
- Whether the small model's answer was correct.
- Whether the large model's answer was correct.
"Correct" comes from human labels, a reference answer, or a grader you trust. Use requests that look like production traffic. If the sample is mostly easy requests, the small model will look better than it is.
Keep a separate set for the final check. If you tune the threshold and report accuracy on the same records, the result is optimistic.
Sweep the threshold
The sweep function replays the sample at several thresholds. For each threshold, a record counts as answered by the small model when its confidence meets the threshold. Otherwise it is escalated, and the large model's correctness is used.
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 resultsEach result is a tuple of (threshold, accuracy, escalation_rate). A higher threshold sends more requests to the large model. Accuracy rises and cost rises with it.
Turn escalation into cost
Cost per request depends on the policy's structure. A cascade always pays for the small model, then pays for the large model on escalated requests. A router pays for only one model.
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
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_largeSuppose the small model costs 1 unit per request and the large model costs 20. If 70% of requests stay with the small model, the cascade costs 1 + 0.3 * 20 = 7 units. Always-large costs 20. The cascade is cheaper, but the 1 unit it pays on every request means it can never go below the small model's cost.
Compare that with a router that sends 70% to the small model. It costs 0.7 * 1 + 0.3 * 20 = 6.7 units. The router is cheaper by the small model's price on the escalated share, but it cannot recover when it sends a hard request to the small model.
Choose the threshold
Run the sweep and look at the results together. The table below shows an example of the output for one sample.
| Threshold | Accuracy | Escalation rate | Cascade cost |
|---|---|---|---|
| 0.5 | 0.84 | 0.15 | 4.0 |
| 0.7 | 0.89 | 0.30 | 7.0 |
| 0.9 | 0.92 | 0.60 | 13.0 |
Pick the cheapest threshold that meets your quality target. If the requirement is at least 0.88 accuracy, 0.7 is the choice here. Going to 0.9 adds 0.03 accuracy and nearly doubles the cost. Whether that is worth it is a business decision, so state the trade-off in numbers when you present it.
Look for a knee in the curve, where accuracy stops improving much while cost keeps climbing. Use a fine grid of thresholds near that point.
Common errors
- Tuning and testing on the same data. Hold out records for the final number.
- Uncalibrated confidence. A score of 0.8 from one model may not mean 80% correct. The sweep measures what the score actually delivers, so rely on it instead of the raw number.
- Ignoring latency. An escalated request waits for two calls. Measure the tail latency too, not just the average.
- A small sample. With a few dozen records, a difference of 0.01 in accuracy is noise. Collect enough records that the gaps you care about exceed the random variation.
- Stale data. Traffic changes. Re-run the sweep when the mix of requests shifts.
Exercise
Using the example in this chapter (small model costs 1, large model costs 20), a cascade has a threshold that escalates 40% of requests. What is the expected cost per request, and how does it compare with a router that sends 60% of requests to the small model?
Show answer
The cascade answers 60% with the small model, so its cost is 1 + 0.4 20 = 9 units. The router costs 0.6 1 + 0.4 * 20 = 8.6 units. The router is cheaper by 0.4 units, which is the small model's cost on the escalated 40%. The cascade pays that extra amount in return for the chance to correct low-confidence small-model answers.
Exercise
A sweep shows accuracy of 0.90 at threshold 0.8 and 0.91 at threshold 0.9, but escalation rises from 25% to 55%. Which threshold would you choose, and what would you check before deciding?
Show answer
Threshold 0.8 is the better default, because 0.01 more accuracy costs 30 more points of escalation. Before deciding, check the sample size to see whether 0.01 is larger than random noise, confirm the numbers on held-out records, and compare against your quality target.