Route

A calibrated cascade across model backends — a cheap or self-hosted model first, escalating to a stronger or more expensive one only when needed — with either a cost guarantee or an accuracy guarantee, instead of a static “always call the expensive model” or “always call the cheap one” rule.

Request

from cli_sdk import CLIClient, OpenAIBackend, Route, Set, VLLMBackend
 
TRIAGE = Set(
    instructions="Which team should handle this ticket?",
    options={"billing": "Payments, invoicing, refunds", "technical": "Bugs, outages, integrations"},
    calibration_profile="cost-routing-v1",
)
 
with CLIClient() as client:
    result = client.evaluate(
        context={"ticket": user_ticket},
        queries={
            "team": Route(
                cascade=[
                    {"backend": VLLMBackend(model="meta-llama/Llama-3.3-70B-Instruct",
                                            base_url="https://vllm.example.com/v1"),
                     "cost_cents": 0.03},
                    {"backend": OpenAIBackend(model="gpt-4.1-2025-04-14"), "cost_cents": 0.4},
                    {"backend": "human_queue", "cost_cents": 25.0},
                ],
                calibration_profile="cost-routing-v1",
                guarantee="cost_budget",
                target_cents=0.5,
                alpha=0.10,
                task=TRIAGE,
            ),
        },
    )
 
answer = result.answers["team"]
print(answer.output)                                  # "billing", or None when it went to the human queue
print(answer.served_by, answer.escalated, answer.cost_cents)

task is the decision every stage makes, as a Set; the hosted service requires it, and output is one of its option keys. Route does not need a backend argument: its backends come from cascade. The same holds for a Judge that declares a cascade. A request whose queries all declare their own cascade can omit backend; any other query in the same request still needs one.

Each stage is {"backend": <backend>, "cost_cents": <number>}, cheapest first, at most 6 stages; {"backend": "human_queue"} may only be the last. Every stage is called through your organization’s provider connections, and a stage must be a backend object or dict: named backends are a local-mode feature, and a cascade cannot use client evidence.

Response

{
  "team": {
    "type": "route",
    "output": "billing",
    "served_by": {"backend": "vllm:meta-llama/Llama-3.3-70B-Instruct", "tier": 0,
                  "provider": "vllm", "model": "meta-llama/Llama-3.3-70B-Instruct"},
    "escalated": false,
    "cost_cents": 0.03,
    "threshold": 0.62,
    "tiers": [{"backend": "vllm:meta-llama/Llama-3.3-70B-Instruct", "confidence": 0.81, "output": "billing",
               "cost_cents": 0.03, "provider": "vllm", "model": "meta-llama/Llama-3.3-70B-Instruct"}],
    "guarantee": {
      "type": "cost_budget",
      "method": "calibrated-cascade",
      "target_cents": 0.5,
      "alpha": 0.10,
      "delta": 0.10,
      "realized_upper_bound": 0.071,
      "statement": "With 90% confidence, cost per request is at most 0.5 cents for at least 90% of requests (n=900); the escalation threshold is 0.62.",
      "calibration_profile": "cost-routing-v1",
      "calibration_n": 900,
      "profile_version": 1,
      "stats_version": "cci-sdk/0.1.1+hosted.1"
    }
  }
}

tiers lists every stage that was tried, with the confidence of its top option. A request no stage was confident about goes to the human queue (served_by: {"backend": "human_queue"}, output: null) when the cascade ends with one; otherwise the last stage answers.

Guarantee modes

guaranteeStatementCard
"cost_budget"With confidence 1−δ1-\delta over the calibration draw, P(cost per request≤target_cents)≥1−αP(\text{cost per request} \le \text{target\_cents}) \ge 1-\alphatype: "cost_budget", method calibrated-cascade, with target_cents, alpha, delta and realized_upper_bound
"accuracy"P(served answer is wrong)≤αP(\text{served answer is wrong}) \le \alpha: each of the kk stages answers only when its conformal set at level 1−α/k1-\alpha/k is a single optiontype: "risk", method conformal-cascade, with target equal to α\alpha

How cost_budget is calibrated. The policy is one escalation threshold λ\lambda: stop at the first stage whose top option has probability at least λ\lambda. Cost per request only grows with λ\lambda, so the service tests the thresholds of a fixed 101-point grid from the cheapest upward, with an exact binomial test of “more than an α\alpha fraction of requests exceed the budget”, and keeps the last threshold the test rejects. This fixed-sequence procedure keeps the overall error at δ=0.10\delta = 0.10 across the whole search. The cost of a request is the sum of cost_cents of every stage it reached, including a trailing human_queue stage. When even the cheapest policy exceeds the budget too often, the answer is heuristic and goes to the human queue.

Both modes are marginal over requests exchangeable with the calibration set. A cost budget does not cap the cost of any single request: up to an α\alpha fraction of requests can exceed target_cents. A cost_budget profile needs at least ⌈log⁡δ/log⁡(1−α)⌉\lceil \log\delta / \log(1-\alpha) \rceil examples (22 at α=0.10\alpha = 0.10), and at least as many as a coverage guarantee at the same α\alpha.

Calibrating a cascade

A Route profile is built from historical requests with the correct answer recorded, an option key of the task. The service runs every stage of the cascade on each example’s context, so it can learn the escalation threshold from how confident each stage was and whether it was right. Calibration therefore costs one scoring pass per stage and example (more at L0), and usually runs as a background job.

with CLIClient() as client:
    client.calibration_profiles.create(
        name="cost-routing-v1",
        backend=OpenAIBackend(model="gpt-4.1-2025-04-14"),
        method="calibrated-cascade",
        alpha=0.10,
    )
    client.calibration_profiles.add_examples(
        "cost-routing-v1",
        examples=[{"context": {"ticket": ticket}, "label": team} for ticket, team in historical_data],
    )

The first evaluate with the Route query binds the profile to the cascade and the task and starts calibration. A tier_outputs field on an example is accepted and stored, but the hosted service scores the stages itself and does not use it. Changing any stage’s backend is a backend change for the profile (strict fingerprints). Prices are not part of the fingerprint: the profile stores each stage’s confidence on every example, and the threshold is derived from those and the request’s cost_cents, target_cents and alpha on every request. When a provider’s price changes, update cost_cents; no recalibration or model call is needed.

See the cost routing guide for an end-to-end setup, including a spend monitor.

Answer attributes

AttributeTypeMeaning
outputAnyThe task option chosen by the stage that served the request; None from the human queue
served_bydictbackend, tier, provider and model of that stage
escalatedboolWhether the request moved past the first stage
cost_centsfloat | NoneCost of this request
raw["threshold"], raw["tiers"]The calibrated escalation threshold, and every stage tried
guarantee, is_heuristicThe guarantee card, and whether it is heuristic

Parameters

ParameterTypeDescription
cascadelist[dict]Required, 1 to 6 stages, cheapest first. Each stage is {"backend": ..., "cost_cents": ...}; "human_queue" may be the last.
calibration_profilestrRequired.
taskSetThe decision each stage makes. Required by the hosted service and by local mode.
guarantee"cost_budget" | "accuracy"Default "cost_budget".
target_centsfloatRequired for "cost_budget". Per-request cost target.
alphafloat, optionalAllowed violation rate (0.10 by default).

In local mode, Route supports guarantee="accuracy", with stages naming backends registered in LocalCLIClient(backends={...}). cost_budget is a hosted-service feature.