Cost routing
Problem
A team serves most requests from a self-hosted open-weight model and wants to pay for a frontier API call only when the cheap model is genuinely unsure. Finance wants a guarantee on the resulting cost per request, not “usually cheap”.
A static rule (“escalate below 70% confidence”) has no cost story: nobody can say how often it will escalate next month, or what that does to the bill.
Primitives
Routewithguarantee="cost_budget": a calibrated cascade with, at 90% confidence over the calibration draw, .LocalMonitoron the over-budget rate, to catch drift in the traffic mix.
The routed decision here is a support ticket’s team: each stage answers
the same Set task, and the cascade decides which stage’s answer to
trust.
Code
Calibrate the cascade
Both stages run through provider connections: a vLLM
connection for the self-hosted model (its URL must be public https on
the hosted service, or private in a self-hosted
deployment) and an OpenAI connection. The calibration set is a random
sample of historical tickets with the team a human finally assigned.
from cli_sdk import CLIClient, OpenAIBackend, Route, Set, VLLMBackend
PROFILE = "cost-routing-v1"
CHEAP = VLLMBackend(
model="meta-llama/Llama-3.3-70B-Instruct",
base_url="https://vllm.example.com/v1",
engine_version="0.11.0",
quantization="fp8",
)
STRONG = OpenAIBackend(model="gpt-4.1-2025-04-14")
TARGET_CENTS = 0.4
TEAM = Set(
instructions="Which team should handle this ticket?",
options={"billing": "Payments, invoicing, refunds",
"technical": "Bugs, outages, integrations",
"sales": "Pricing, upgrades, new accounts"},
calibration_profile=PROFILE,
)
ROUTE = Route(
cascade=[
{"backend": CHEAP, "cost_cents": 0.03},
{"backend": STRONG, "cost_cents": 0.35},
{"backend": "human_queue", "cost_cents": 30.0},
],
calibration_profile=PROFILE,
guarantee="cost_budget",
target_cents=TARGET_CENTS,
alpha=0.10,
task=TEAM,
)
with CLIClient() as client:
client.calibration_profiles.create(name=PROFILE, backend=CHEAP, method="calibrated-cascade", alpha=0.10)
client.calibration_profiles.add_examples(
PROFILE,
examples=[{"context": {"ticket": ticket}, "label": team}
for ticket, team in historical_tickets], # about 900 tickets
)
# The first evaluate binds the profile to this cascade and starts calibration:
# every stage scores every ticket, 1,800 model calls, so it runs as a background job.
client.evaluate(context={"ticket": historical_tickets[0][0]}, queries={"answer": ROUTE})
profile = wait_until_serving(client, PROFILE) # from Calibration profiles: lifecycle
print(profile.n, profile.minimum_n) # 900 22wait_until_serving is the polling helper on
Calibration profiles.
Route every request
from cli_sdk import CLIClient, EvaluateResponse, LocalMonitor, RouteAnswer
class Routed(EvaluateResponse):
answer: RouteAnswer
over_budget = LocalMonitor(type="risk", target=0.12, false_alarm_rate=0.05, profile=PROFILE)
def serve(client: CLIClient, ticket: str):
result = client.evaluate(context={"ticket": ticket}, queries={"answer": ROUTE}, response_model=Routed)
answer = result.answer
record_cost(answer.served_by, answer.cost_cents, escalated=answer.escalated)
alert = over_budget.update(answer.cost_cents is not None and answer.cost_cents > TARGET_CENTS)
if alert:
notify_finance(f"over-budget rate above 12% after {alert.n} requests")
if answer.output is None:
return enqueue_for_agent(ticket) # the human queue served it
return answer.output
with CLIClient() as client:
print(serve(client, "How do I export invoices?"))Walkthrough
Route needs no backend argument: the cascade names its own backends,
cheapest first. For each request, the self-hosted model answers first. At
its default access level (L1) one request scores all three teams from
first-token log-probabilities; pinning access_hint="exact" on the vLLM
backend uses exact label-token probabilities (L3) instead, which is a
different scoring function and needs its own calibration. When the cheap
stage’s top team clears the calibrated escalation threshold, the request
is served by vLLM (escalated=False, cost_cents 0.03). Otherwise it
goes to the OpenAI stage, and if neither is confident enough, to the human
queue, whose 30-cent cost counts toward the budget.
The threshold is learned from the calibration set: the service tries the thresholds of a fixed grid from the cheapest policy upward, with an exact binomial test of “more than 10% of requests exceed 0.4 cents”, and keeps the last one the test rejects. With 90% confidence over the calibration draw, at most 10% of requests exceed 0.4 cents. Nothing caps the cost of one particular request; the bound is on how often requests exceed the target. A cost-budget profile needs at least 22 examples at , but the threshold is only as good as the coverage of the traffic mix, so calibrate on hundreds.
The local monitor turns that bound into an alarm. Each request is a 0/1 outcome, “over budget”, and the monitor tests whether their rate has risen above 12% (a margin above the 10% the profile allows, so it reports drift rather than the scatter of one calibration). If the traffic mix shifts toward questions the cheap model cannot handle, escalations rise, the rate climbs, and the monitor fires with its false-alarm rate still at 5%.
The profile’s backend fingerprint includes the vLLM engine_version and
quantization. Upgrading the engine or re-quantizing the model changes
the cheap stage’s scores; with the default strict_fingerprint,
requests from the upgraded stage fail closed until you recalibrate with
POST .../calibrate.
What the guarantee card means
card = result.answer.guarantee
card.type # "cost_budget"
card.target_cents # 0.4
card.alpha # 0.1
card.delta # 0.1
card.realized_upper_bound # e.g. 0.071
card.describe()
# With 90% confidence, cost per request is at most 0.4 cents for at least 90% of requests (n=900); the escalation threshold is 0.62.- Over requests exchangeable with the 900 calibration tickets, at least 90% cost at most 0.4 cents, with 90% confidence over the calibration draw. Up to 10% can cost more, including the full price of the strong stage and the human queue.
realized_upper_boundis the upper confidence bound on the share of calibration tickets over budget at the chosen threshold.- The card says nothing about answer quality. To bound how often the
served answer is wrong instead, use
guarantee="accuracy", whose card is ariskcard withtargetequal toalpha. Give that query its own profile, so each profile, and each card, traces back to one query. - Prices are not part of the calibration: the profile stores each stage’s
confidence on every ticket, and the threshold is derived from those and
the request’s
cost_centson every request. When a provider changes its price, updatecost_cents; the card stays valid with no recalibration.