A ticket-tagging feature can call a small model for a fraction of a cent or a large one for several cents. A cascade calls the small model on every query and escalates to the large one only when the small model's confidence is low. Choose the escalation threshold on validation data so the cascade reaches a target accuracy as cheaply as possible, then route live traffic with it.
No model is called. Each validation record is a dict {"conf": c, "cheap_ok": bool, "strong_ok": bool}: the cheap model's confidence on that query, and whether each model's answer was correct. At threshold a query is escalated when conf < t (strictly). An escalated query pays for both calls and uses the strong model's answer; any other query pays for the cheap call only and uses the cheap answer.
Implement three functions. cheap_cost and strong_cost are the prices of one call, with strong_cost > 0.
cascade_metrics(records, threshold, cheap_cost, strong_cost): return {"accuracy": a, "escalation_rate": e, "cost": k} where is the share of records answered correctly, the share escalated and the expected cost per query. Round each to 4 decimals. With no records, all three are 0.0.choose_threshold(records, candidates, cheap_cost, strong_cost, target_accuracy): among the candidate thresholds whose unrounded accuracy is >= target_accuracy, the one with the lowest cost per query; if several share the lowest cost, the highest of them. Return cascade_metrics for it with "threshold": t added, or None when no candidate reaches the target (always the case with no records or no candidates).route(queries, threshold, cheap_cost, strong_cost): queries is a list of (query_id, conf). Return {"cheap": ids answered by the cheap model, "strong": ids escalated, "cost": total cost of the batch}, ids in input order and the cost rounded to 4 decimals.