mixle.task.economics module¶
Cost comparisons for local distillation, cascades, and teacher-only serving.
Every routing choice has a cost model. This module combines a measured
escalation rate (mixle.task.calibrate.CalibratedTaskModel.escalation_rate(),
the empirical p_escalate) with unit costs:
frontier-only – pay
c_frontierfor every request, forever.local-only – distill once (
n_labelteacher calls + training), then payc_localper request.cascade – run the low-cost local model first, escalate only the ambiguous fraction: per request
c_local + p_escalate * c_frontier.
p_escalate is a measured selection rate, and alpha buys only MARGINAL prediction-set
coverage under exchangeability – it is not a bound on the error of the locally-answered
slice, because answering locally conditions on the set being a singleton. Treat answered-slice
risk as a measurement, or certify it separately with
mixle.task.calibrate.CalibratedTaskModel.calibrate_selective(); both statements fail
silently under distribution shift.
break_even_volume() is the request count at which a distilled route
recovers its one-time setup cost. recommend_route() picks the lowest-cost
route at a given volume, optionally constrained by a maximum tolerated
escalation rate, and reports the savings.
- class CostModel(c_frontier, c_local=0.0, c_label=0.0, train_cost=0.0)[source]
Bases:
objectUnit costs in any consistent currency.
- cascade_cost_per_request(cost, p_escalate)[source]
Expected per-request cost of the cascade: always run local, escalate the
p_escalatefraction.
- break_even_volume(cost, n_label, *, p_escalate=0.0)[source]
Requests after which a distilled route undercuts frontier-only (
infif it never does).Setup is amortized against the per-request saving
c_frontier - per_request(route). Withp_escalate=0this is the local-only break-even; pass the model’s escalation rate for the cascade break-even.
- class RoutePlan(route, volume, per_request, total, savings_vs_frontier, p_escalate, break_even, options)[source]
Bases:
objectCosted route comparison for a fixed request volume.
- recommend_route(cost, *, volume, n_label, p_escalate, max_escalation=None, local_only_certified=False)[source]
Pick the lowest-cost route over
volumerequests.local_onlyis never inferred from an escalation preference: forcing ambiguous requests through a local model changes serving semantics. It is offered only whenlocal_only_certified=Trueand the measured escalation probability is exactly zero.
- select_alpha_for_cost(model, cal_texts, cal_labels, probe_texts, cost, *, certification_texts, certification_labels, volume, n_label, alphas=(0.01, 0.05, 0.1, 0.15, 0.2, 0.3))[source]
Select
alphafrom aCostModeltarget.The sweep connects
recommend_route()to the calibration step so threshold selection reflects both model behavior and the caller’s cost assumptions.modelis anything with theCalibratedTaskModelshape: a mutablealphaattribute,calibrate(texts, labels), andescalation_rate(texts). For each candidate inalphas, this recalibratesmodelon policy-selection calibration data and measures its realized escalation rate onprobe_texts, then scores that escalation rate withrecommend_route()overvolumerequests. The winner is the alpha whose recommended route is lowest-cost overall; only after selection is complete ismodelcalibrated at that alpha on the independentcertification_texts/certification_labels. Thus the coverage-bearing threshold never participates in policy selection. Returns(best_alpha, best_plan, plan_by_alpha)so the full sweep remains auditable; the plans are policy-selection cost estimates, while the model’s final threshold is the independent certification result.The in-place recalibration is all-or-nothing (MXR-080-1895): if any step raises,
model’s attributes are restored to what they were on entry before the exception propagates, so a failed sweep can never leave a serving model carrying a threshold fitted on policy-selection rows. See_attribute_snapshot()for what “restored” does and does not cover.- Parameters:
- Return type: