Sub topic 6
Cascades: try small first, escalate when needed
This chapter assumes you know basic Python, have called a hosted language model through an API, and understand per-token pricing. It also assumes you have seen a rule-based router, which picks a model before any model runs.
What a cascade is
A cascade sends every request to the small model first. It then checks how confident the small model is in its answer. If confidence is high enough, the cascade returns that answer. If not, it discards the answer and sends the same prompt to the large model.
A router decides before calling any model. A cascade decides after seeing the small model's output. This gives the cascade more information, but it costs more on the requests it escalates.
The core code
This function implements the flow in the diagram. Here, call_small returns a (text, confidence) pair and call_large returns text.
def answer(prompt, call_small, call_large, threshold=0.8):
text, confidence = call_small(prompt)
if confidence >= threshold:
return text, "small"
return call_large(prompt), "large"The function also returns which tier answered. Keep that label. You need it later for evaluation and monitoring.
Where confidence comes from
The cascade is only as good as its confidence score. Common sources include:
- Token log-probabilities from the API, such as the average probability of the generated tokens.
- A self-reported rating, where you ask the model to score its own answer. This is cheap but often over-optimistic.
- Agreement between several small-model samples. This is more reliable but multiplies the small-model cost.
- A task-specific check, such as whether the output parses as valid JSON or passes a unit test.
A task-specific check is often the best signal when one exists. It tests the answer itself and does not depend on the model's opinion of it.
Cost: the cascade always pays for the small model
A router pays for exactly one model per request. A cascade pays for the small model every time and adds the large model on escalated requests.
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 10. If 70% of requests stay on the small tier, the cascade costs 1 + 0.3 * 10 = 4.0 units per request. A router that sends the same 70% to the small model costs 0.7 + 3.0 = 3.7. The gap is exactly frac_small * cost_small. That is the small-model cost wasted on escalated requests.
The cascade still wins when it keeps more requests on the small tier than a router could. A router must guess from the prompt alone. A cascade can see that the small model handled a difficult-looking prompt well.
Latency
An escalated request waits for the small model and then the large model. Its latency is the sum of both calls. Requests answered by the small model are faster than they would be with the large model alone. So the average improves, but the slowest requests get slower. If you have a strict latency limit, check the 95th percentile, not just the mean.
Setting the threshold
The threshold controls the trade-off:
- A higher threshold escalates more requests. Quality and cost both rise.
- A lower threshold keeps more requests on the small tier. Cost falls, and so does quality if the small model is wrong on those requests.
Do not guess the value. Choose it by replaying a labeled sample, which a later chapter covers in detail.
Common errors
- Using one threshold for every task. Confidence scores behave differently for summaries, classification, and code. Calibrate per task type.
- Counting only the large-model bill. The small-model calls on escalated requests are real spend. Use the cascade formula above.
- Discarding the tier label. Without it, you cannot tell whether a quality drop comes from the small model or the large one.
- Escalating nearly everything. If most requests escalate, the cascade costs more than calling the large model directly. Check the escalation rate.
- Ignoring streaming. You cannot stream the small model's answer to the user and then replace it. Buffer the output until the confidence check passes.
Exercise
The small model costs 2 units per request and the large model costs 20. A cascade keeps 80% of requests on the small model. What is the expected cost per request? What would a router cost if it sent the same 80% to the small model?
Show answer
Cascade: 2 + (1 - 0.8) 20 = 2 + 4 = 6 units. Router: 0.8 2 + 0.2 20 = 1.6 + 4 = 5.6 units. The cascade costs 0.4 more, which is 0.8 2, the small-model cost paid on the 80% that did not need escalation... precisely, it is the small-model cost paid on the 20% of escalated requests (0.2 * 2 = 0.4).