Skip to content

crp.qsr

Auto-generated reference for the crp.qsr subpackage.

qsr

crp.qsr

Quality-Tier-Supervised Router (SPEC-050).

RoutingExample dataclass

A single labelled routing decision.

CapabilityProfile dataclass

Capability profile for a single model in the fleet.

score_for(task_kind)

Return measured competence for task_kind (defaults to 0.5).

LearnedRouter

Route tasks to the model most likely to hit tier A/S at least cost.

Parameters:

Name Type Description Default
fleet dict[str, CapabilityProfile] | None

Mapping of model_id → CapabilityProfile. Defaults to FLEET.

None
min_train_examples int

Minimum tier-A/S examples required before the learned model is used. Defaults to 200 (SPEC-050 §2.5).

200

train(examples)

Train the classifier on tier-A/S examples.

Returns:

Type Description
bool

True if training succeeded and the router will use the learned model.

route(task)

Pick the best model for task.

Capability-aware pre-filtering excludes models whose schema_complexity_ceiling is below the task's schema depth. If a learned model is available it is used; otherwise the fleet profile with the highest competence for the task kind wins.

RoutingTask dataclass

Task description used as router input.

run_with_escalation(task, execute_fn, policy, router=None)

Execute task with failure-driven escalation.

Parameters:

Name Type Description Default
task RoutingTask

Routing task.

required
execute_fn Callable[[RoutingTask, str], dict[str, Any]]

fn(task, model_id) -> result_dict.

required
policy dict[str, Any]

Dict with keys escalate_on ({"tier_below": "B", "vr_ratio_below": 0.9}) and max_rungs.

required
router LearnedRouter | None

Optional LearnedRouter; a fresh one is created if omitted.

None

Returns:

Type Description
dict[str, Any]

The result dict from the final run, with an added escalated key.

register_profile(model_id, profile)

Add or replace a profile in the default fleet.

adapt_schema(schema, ceiling)

Flatten nested objects beyond ceiling levels into plain-language fields.

Parameters:

Name Type Description Default
schema dict[str, Any]

JSON schema dict (must be object type at root).

required
ceiling int

Maximum nesting depth the target model can handle reliably.

required

Returns:

Type Description
dict[str, Any]

A simplified schema dict.

qsr.escalation

crp.qsr.escalation

Escalation ladder driven by observed failure signals (SPEC-050 §2.3.4).

Try the smallest/cheapest model first; climb the ladder only when the observed quality tier or verification ratio falls below policy thresholds. Escalation is triggered by signals, not by a fixed retry count.

run_with_escalation(task, execute_fn, policy, router=None)

Execute task with failure-driven escalation.

Parameters:

Name Type Description Default
task RoutingTask

Routing task.

required
execute_fn Callable[[RoutingTask, str], dict[str, Any]]

fn(task, model_id) -> result_dict.

required
policy dict[str, Any]

Dict with keys escalate_on ({"tier_below": "B", "vr_ratio_below": 0.9}) and max_rungs.

required
router LearnedRouter | None

Optional LearnedRouter; a fresh one is created if omitted.

None

Returns:

Type Description
dict[str, Any]

The result dict from the final run, with an added escalated key.

qsr.gateway

crp.qsr.gateway

Gateway integration for the Quality-Tier-Supervised Router (SPEC-050).

When a request asks for learned model selection (model: crp-learned or the crp-model-selection: learned header), this module builds a RoutingTask from the request and uses LearnedRouter to pick a concrete model_id. The Gateway then dispatches to that model through the normal provider router.

resolve_model(request, headers=None, fleet=None)

Return the concrete model_id to dispatch for request.

If the request does not request learned routing, the existing request.model is returned unchanged.

Parameters:

Name Type Description Default
request Any

A Gateway ChatRequest.

required
headers dict[str, str] | None

Optional raw request headers (used to read crp-model-selection).

None
fleet dict[str, Any] | None

Optional custom fleet for LearnedRouter.

None

Returns:

Type Description
str

Model identifier string (may be unchanged).

qsr.harvest

crp.qsr.harvest

Harvest router training examples from the audit chain (SPEC-050 §2.3.1).

Each completed dispatch already writes an audit record (SPEC-011). The QSR projects those records into RoutingExample tuples - the free supervision signal that drives the learned router.

RoutingExample dataclass

A single labelled routing decision.

harvest(audit_records)

Build RoutingExample

Parameters:

Name Type Description Default
audit_records list[dict[str, Any]]

Audit records from SPEC-011.

required

Returns:

Type Description
list[RoutingExample]

Filtered list of routing examples derived from dispatch_complete records.

qsr.profiles

crp.qsr.profiles

Per-model capability profiles for heterogeneous local-SLM fleets (SPEC-050 §2.3.2).

Profiles are benchmark-derived (SQB, SPEC-026) and drive cold-start routing as well as eligibility filtering for the learned router.

CapabilityProfile dataclass

Capability profile for a single model in the fleet.

score_for(task_kind)

Return measured competence for task_kind (defaults to 0.5).

register_profile(model_id, profile)

Add or replace a profile in the default fleet.

qsr.router

crp.qsr.router

RoutingTask dataclass

Task description used as router input.

LearnedRouter

Route tasks to the model most likely to hit tier A/S at least cost.

Parameters:

Name Type Description Default
fleet dict[str, CapabilityProfile] | None

Mapping of model_id → CapabilityProfile. Defaults to FLEET.

None
min_train_examples int

Minimum tier-A/S examples required before the learned model is used. Defaults to 200 (SPEC-050 §2.5).

200

train(examples)

Train the classifier on tier-A/S examples.

Returns:

Type Description
bool

True if training succeeded and the router will use the learned model.

route(task)

Pick the best model for task.

Capability-aware pre-filtering excludes models whose schema_complexity_ceiling is below the task's schema depth. If a learned model is available it is used; otherwise the fleet profile with the highest competence for the task kind wins.

qsr.schema_adapt

crp.qsr.schema_adapt

Schema adaptation for small-model tool inputs (SPEC-050 §2.3.5).

When a tool's JSON schema is deeper than a target model's schema_complexity_ceiling, flatten nested objects into plain-language string fields before the schema enters the tool-selection window.

adapt_schema(schema, ceiling)

Flatten nested objects beyond ceiling levels into plain-language fields.

Parameters:

Name Type Description Default
schema dict[str, Any]

JSON schema dict (must be object type at root).

required
ceiling int

Maximum nesting depth the target model can handle reliably.

required

Returns:

Type Description
dict[str, Any]

A simplified schema dict.