pi-smart-router1.5.0
Auto-model router middleware for the pi.dev coding agent
Untested on omp
This is a pi package. No one has reported whether it works on omp yet. If you try it, add your result below.
Reports 0
No reports yet.
Readme
pi-smart-router
Auto-model router middleware for the pi coding agent.
v1.0 — SemVer stable (honest defaults). From the
1.0.0release on, the public API and documented operator surface follow semantic versioning: breaking changes land only in a new major version, each accompanied by a migration guide — start with docs/migration-v1.md. 1.0 means stable API + honest calibration floors + gates that catch Sept-class failures — not a claim that cheap-tier routing is behaviorally proven. #95 / #110 remain open for verifier-graded recalibration. The published release always mirrorspackage.json(npm).
pi-smart-router intercepts every LLM inference request and dynamically routes it to the optimal execution engine — balancing cost, capability, latency, and time-to-first-token (TTFT) — without requiring you to manually pick a model for each turn.
| pi-smart-router is | pi-smart-router is not |
|---|---|
| A pi extension that auto-selects the best model per request | A replacement for pi or your LLM provider |
| A three-tier router: local, economical cloud, frontier cloud | A post-generation output judger (FrugalGPT-style) |
| Cache-aware with session pinning to preserve prompt-cache economics | A turn-by-turn model switcher that shatters provider caching |
| Registry-driven in pi (no YAML copy for normal use) | An RL-trained router requiring agent trace datasets |
How it works
request → hardware probe → loop escalation → turn envelope → typed judgment* → context-fit gate
→ low-intensity tier gate → session pin → deterministic triage
→ local zero-tier → triage cloud fallback → HyDRA embedding matcher
→ safe cloud default → context overflow fallback
The pipeline runs 12 stages sequentially with early exit — the moment any stage reaches a routing decision, subsequent stages are skipped. Every decision includes the stage name, reason code, candidates considered, estimated cost, and routing latency for full observability.
* typed_judgment is an additional opt-in, non-deciding stage (default off) that runs between turn envelope and the context-fit gate when enabled — it records judge answers and never selects a model itself. Its #188 consumers can use those answers for planning classification, tier hints, and local safety gates. See Typed judgment (opt-in).
| Stage | Budget | What it does |
|---|---|---|
| Hardware Probe | — | Checks platform/RAM/battery to gate local inference |
| Loop Escalation | — | Detects repeated identical tool failures; escalates session to frontier |
| Turn Envelope | <2ms | Classifies turn type: tool_result, planning, subagent, main_loop |
| Typed Judgment (opt-in) | ~385–990ms per judged user turn when enabled; 0 otherwise | Non-deciding (default off): asks a Jev-compatible judge six questions and records the answers; downstream #188 consumers may use them for planning, tier hints, and local gates |
| Context-Fit Gate | — | Filters fleet to models whose context window fits estimated input tokens |
| Low-Intensity Gate | — | Structural tier hint, cluster match, and P(success) expected-cost scoring |
| Session Pin | <1ms | Returns pinned model if session has one; breaks pin on compaction or overflow |
| Deterministic Triage | <5ms | Aho-Corasick keyword scan + cyclomatic complexity analysis |
| Local Zero-Tier | <15ms | Pings LM Studio + Ollama in parallel; routes locally when eligible |
| Triage Cloud Fallback | <2ms | Trivial prompts not claimed locally route to the first healthy economical-cloud model |
| HyDRA Matcher | 80-120ms | ONNX embeddings, 3D requirement projection, shortfall gate, multi-objective scoring |
| Safe Cloud Default | — | First healthy economical-cloud model (context-fit aware) |
| Context Overflow Fallback | — | Escalates to largest-fit model when economical tiers cannot fit |
Research lineage
pi-smart-router builds on ideas from several production and research routing systems:
- Adopted: GitHub Copilot HyDRA (shortfall matching decoupled from model identities), Zero-Tier local edge-cache pattern, Weave Router session pinning and multi-objective selection
- Rejected: FrugalGPT sequential cascading (tail latency), RouteLLM matrix factorization (confounder vulnerability), turn-by-turn dynamic routing (cache destruction)
See docs/PRD.md for full architectural justification, docs/deep-research.md for the research survey, docs/routing-roadmap.md for the prioritized quality backlog, docs/gemini-research.md for the second-source agent-router report, and docs/research/README.md for research provenance.
Prerequisites
| Dependency | Required | Notes |
|---|---|---|
| Node.js >= 22.19.0 | Yes | ES module package; matches package.json engines.node and CI (workflows pin Node 22.19.0) |
Engine floor:
package.jsondeclaresengines.node >= 22.19.0. If you (or your environment) enableengine-strict=truein.npmrc, installs on Node < 22.19.0 fail withEBADENGINE; on a supported Node (>= 22.19.0),npm cicompletes with noEBADENGINEwarnings. | pi coding agent | Yes | Extension host | | macOS Apple Silicon | MVP | Primary supported platform | | Linux (x64/arm64) | Experimental | Probe logic supported; not validated on real hardware | | Windows (x64/arm64) | Experimental | Probe logic supported; not validated on real hardware | | LM Studio or Ollama | Optional | Required for zero-tier local routing | | Authenticated cloud providers in pi | Recommended | Anthropic, OpenAI, Google, etc. |
Install
Security: Pi packages run with full system access. Extensions execute arbitrary code. Review source before installing third-party packages (pi packages docs).
Via pi (recommended)
Install from npm / pi.dev/packages:
pi install npm:pi-smart-router
pi --list-models | grep smart-router
Project-local install (writes to .pi/settings.json):
pi install -l npm:pi-smart-router
Then in pi:
/model smart-router/auto
/smart-router status
First run: pi install runs npm install for package dependencies (better-sqlite3 compiles natively). The first routed request downloads HyDRA ONNX weights to .pi-smart-router/models/ under your state directory.
Via npm (library API)
npm install pi-smart-router
Use createRouter() / createRouterFromFleet() for programmatic integration without the pi extension. That path is the catalog / routing-core composition root (not full extension wiring). See Library vs extension and Optional: YAML fleet (library API).
From source (contributors)
git clone https://github.com/beettlle/pi-smart-router.git
cd pi-smart-router
npm install
The repo ships a project-local pi extension at .pi/extensions/smart-router/. pi auto-discovers it when you run pi from the repo root (after the project is trusted — see Develop from clone).
Quick start
After installing via pi install npm:pi-smart-router (or from clone — see below):
- Authenticate providers (
/login) and enable models in your scoped list if you use one (/scoped-models) /model smart-router/auto— every turn runs through the routing pipeline/smart-router status,/smart-router history, or/smart-router stats— inspect routing decisions and window aggregates
Set SMART_ROUTER_LOG_ROUTING=1 before starting pi to print each routing decision to stderr (see Environment variables).
Use with pi
Detailed steps for the operator path above.
Installed via npm
Global install (pi install npm:pi-smart-router) registers the extension from ~/.pi/agent/settings.json. No project /trust prompt is required for npm-installed extensions — start pi from any directory.
After auth or model list changes, restart pi or run /reload.
Develop from clone
Requires Pi ≥ 0.85.1 (pi.minPiVersion). The extension loads TypeScript from this repo via pi’s loader — no npm run build needed for dogfooding.
Recommended (works from any cwd): install the clone as a path package and remove any published npm copy. npm and path packages have different identities; leaving both installed can double-load and you may keep running a stale npm tarball.
# From anywhere — remove published package if present
pi remove npm:pi-smart-router
# Prefer an absolute path (relative paths are resolved from the install cwd first).
# Pi stores a path relative to ~/.pi/agent/ in settings (same pattern as other local packages).
pi install /path/to/pi-smart-router
# Pi does not run npm install for path packages
cd /path/to/pi-smart-router && npm install
pi list # must show this clone path, not ~/.pi/agent/npm/.../pi-smart-router
pi --list-models | grep smart-router
Alternative (repo-root discovery only): with the npm package removed, start pi with cwd at this repo root. Project-local extensions under .pi/extensions/ load only after the project is trusted — without trust, smart-router never appears in /scoped-models or /model.
On first run, pi prompts you to trust the project when it detects .pi/extensions/. Accept the prompt.
Later or missed prompt: run /trust inside pi to save a trust decision for this directory (or its parent) to ~/.pi/agent/trust.json. Trust on a parent folder (for example ~/Documents/github) applies to this repo as well. After /trust, restart pi — the current session is not reloaded automatically.
Verify the extension loaded:
pi --list-models | grep smart-router
You should see smart-router auto. If the line is missing:
- Confirm
pi listpoints at this clone (path package) or that cwd is the repo root (discovery mode). - Confirm no
npm:pi-smart-routerentry remains in settings while developing from clone. - Confirm the project is trusted if relying on
.pi/extensions/discovery (/trust, or check~/.pi/agent/trust.json). - Restart pi or run
/reloadafter trusting or changing packages.
Non-interactive one-shot checks can pass --approve to trust project-local resources for that run only.
Select the auto model
Switch to the smart-router provider (from any directory after npm install, or from repo root when developing from clone):
/model smart-router/auto
If you use scoped models (/scoped-models or enabledModels in settings), enable smart-router/auto there first — when a scoped list is active, /model only resolves models in that list.
This registers smart-router as a custom provider with a single auto model. Every inference request runs through the routing pipeline and delegates to the selected underlying provider's streaming API.
cursor/auto vs smart-router/auto
pi exposes two different auto models. They are easy to confuse but play different roles:
| Model | Provider | Role |
|---|---|---|
smart-router/auto |
smart-router (this extension) |
Runs the routing pipeline on every turn and delegates to whichever underlying model HyDRA selects |
cursor/auto |
cursor (pi registry) |
Cursor's opaque auto model — direct inference target when selected; Cursor picks the backend model |
Recommended dogfood setup: use /model smart-router/auto so routing, pinning, and telemetry stay active. Enable cursor/auto (and other Cursor models such as composer-latest) in your scoped fleet so the router can select them when appropriate — for example on planning turns or when the Gemini tool-history guard excludes unrepairable Google replay state.
When to pin /model cursor/auto directly (bypass the router):
- You want Cursor's opaque auto selection on every turn with no routing overhead
- You are debugging Cursor SDK auth or delegation outside the router
- You need a stable, non-routed session for comparison with routed behavior
When to use smart-router/auto:
- You want cost/capability-aware model selection across your full authenticated fleet
- You rely on session pinning, failover, or
/smart-router status/history/statstelemetry - Tool-heavy sessions with Gemini economical models work via in-repo replay repair (cross-provider included); the tool-history guard reroutes to non-Google models such as
cursor/autofor unrepairable replay state (see pi-smart-router#85, pi-smart-router#158)
Cursor models (cursor/*, composer-*, and the opaque fleet id default) map to frontier-cloud tier in pi-model-mapper.ts so HyDRA can score them against Gemini and Claude instead of treating them as unknown economical models (pi-smart-router#40, pi-smart-router#70). Related: pi-smart-router#23 (turn envelope / pin order), pi-smart-router#37 (Gemini thought_signature errors).
Cursor subscription quota vs API cost
Cursor models bill against your Cursor Pro subscription quota, not per-token API rates. The mapper sets fallback_cost_per_1m: 0 (no API billing) and a separate quota_cost_per_1m virtual rate used only for frugality scoring and telemetry (SP-096). Economical API models (e.g. gemini-flash-lite) can outscore composer-latest on routine main_loop turns when capabilities are sufficient.
Quota-sensitive fleet hygiene: if you are near Cursor usage limits, exclude composer-latest (and other heavy Cursor frontier models) from your pi scoped fleet enable-list. Leave economical API models enabled so turn envelope and HyDRA prefer paid API tiers over subscription quota. The opaque id default is mapped to frontier tier — do not rely on it as an economical fallback.
Operator commands
| Command | Purpose |
|---|---|
/smart-router |
Same as status (default when no subcommand is given) |
/smart-router status |
Show fleet mode, fleet size, pricing freshness/staleness, and the last routing decision (stage, tier, selected model, latency) |
/smart-router history |
Show recent routing telemetry from SQLite (default limit; optional numeric limit, e.g. /smart-router history 20). Displays the concrete delegated model id (never bare virtual auto) |
/smart-router stats |
Privacy-safe session/window aggregates from routing telemetry: count, mean cost/latency with cost_basis labeling (actual vs estimated, SP-241), planning_delegate vs direct share, local vs cloud when distinguishable, and role cost breakdown. Optional vs-always-frontier savings when frontier fleet prices exist (omitted otherwise). The JSON snapshot (buildStatsSnapshot, automation surface) additionally carries warm rolling cost_calibration actual/estimate buckets (SP-242). Optional numeric limit, e.g. /smart-router stats 50 |
/smart-router mode scoped |
Route only among pi's enabled model patterns (default) |
/smart-router mode all |
Route among all authenticated models in the registry |
/smart-router pricing refresh |
Manually fetch LiteLLM pricing from LITELLM_PRICING_URL, persist to SQLite, and rebuild the fleet with updated rates |
/smart-router export dataset [--limit N] |
Export opt-in routing dataset as JSONL (requires SMART_ROUTER_DATASET=1) |
/smart-router export telemetry-contrib [--limit N] [--embeddings] |
Export privacy-safe community telemetry JSON for calibration contributions (--embeddings opts in to captured 384-dim embedding rows) |
/smart-router feedback good|bad |
Label the last auto-routed request outcome (requires SMART_ROUTER_DATASET=1) |
/smart-router unpin |
Clear the current session pin (in-memory and SQLite) so the next request runs the full routing pipeline |
/smart-router plan [--json] |
Read-only local placement report: encoder resident status, local model warm/cold, RAM/disk constraints, cold vs warm TPS, and bottleneck guess. --json prints the schema-stable report for automation |
/smart-router doctor |
Read-only local readiness checklist (✓/✗) with bottleneck guess and recommendation — validate placement without starting a route |
Fleet mode persists in the session. Use scoped to respect your /model enable-list; use all when you want the router to consider every provider you have logged into.
Local placement plan / doctor (read-only, #116)
/smart-router plan and /smart-router doctor report local placement readiness without mutating anything — no route, pin, or gate is touched. Inspired by Colibrì's coli plan / coli doctor.
The report covers:
- Encoder — whether HyDRA ONNX artifacts are resident in the cache (
.pi-smart-router/models/) or will download on first route - Local model warm/cold — LM Studio / Ollama reachability and whether a model is actually loaded (
warm) vs reachable-but-unloaded (cold-start expected) - Hardware — platform/arch, total/free RAM, hardware-probe verdict (
full_local/classification_only/disabled), battery state - Disk — free GiB; flagged constrained below 2 GiB
- Throughput (cold vs warm TPS) — see formula below
- Bottleneck guess — one of
none,unsupported-platform,battery,memory,disk,no-local-runtime,cold-start,cold-throughput,warm-throughput, with a rationale string - Recommendation —
local-ready,local-warmup-needed, orlocal-unavailable
/smart-router plan --json prints the same report as schema-stable JSON (schemaVersion: 1, kind: "smart-router-placement-plan"; all keys always present, unknowns are null) for automation.
Cold vs warm TPS formula. Throughput samples are tagged by phase: warm samples measure steady-state generation after model load; cold samples include cold-start load cost and never count toward viability. warmMedianTps = median(tps where phase='warm'); viability is warmSamples > 0 AND warmMedianTps >= threshold (default 25 tok/s). Cold-only windows fail closed: when only cold samples exist, local viability is false by policy (requireWarmSamples: true) — cold-start cost must not masquerade as steady-state throughput.
Quality-preserving resource policy. Under RAM/disk/battery pressure the router prefers "local unavailable / escalate safely" to a cloud default over silently weakening encoder fidelity (no quantization flips, no cheaper cascades, no FrugalGPT-style downgrade chains). plan/doctor surface this policy verbatim in the report's policy block.
After typing /smart-router (with a trailing space), press TAB to see subcommands. Continue TAB-completing after mode or pricing for sub-options (scoped/all, refresh).
5. Verify
npm run verify:ci
Concurrency contract
RouterPipeline.route() calls on a single router instance are single-flight: concurrent calls are serialized internally (SP-230, #141). Each exclusive route owns one per-route RoutingContext; overlapping executions are queued rather than interleaved — each queued call waits at most one routing latency. This applies to createRouter() / createRouterFromFleet() handles: a shared router.dispatch is safe to call concurrently, and serialization does not change routing policy outcomes. For parallel routing throughput, create separate router instances.
Library vs extension
pi-smart-router ships in two shapes, and they are not feature-identical (#153):
| Path | What you get |
|---|---|
Pi extension (pi install npm:pi-smart-router, or project-local .pi/extensions/smart-router/ when developing from clone) — supported product composition root |
The full product — the routing pipeline plus the stream-level behaviors below, with hardware probe + store-backed telemetry wired via createDispatchOptions() in .pi/extensions/smart-router/fleet-bootstrap.ts |
npm library (createRouter() / createRouterFromFleet() / GatewayDispatch) — catalog / embedder composition root |
The routing core — the 12-stage pipeline, fleet mapping, gateway health/failover selection, and constructor defaults for costEstimator + localRuntime only. Hardware probe stays disabled and telemetry is opt-in unless you pass those ports. Stream-level behaviors are stubbed or left to your embedder loop |
Extension-only capabilities
These behaviors run in .pi/extensions/smart-router/ and have no equivalent in the library API:
| Capability | Extension implementation | Library status |
|---|---|---|
| Planning delegate spawn — cache-preserving ephemeral frontier sub-call on planning turns, with observation injection and bounded timeouts (SP-144, SP-213 / #71, #120) | .pi/extensions/smart-router/planning-delegate.ts — compressed-context sub-call via streamSimple; falls back to direct frontier with a documented fallback_reason |
The pipeline still emits planning_delegate decisions with delegate model and compressed limits, but nothing spawns the delegate — an embedder must implement the sub-call itself or accept direct-frontier routing |
| Stream failover loop — live provider-error failover across candidate models with user-facing notices (atomic state machine, #33) | .pi/extensions/smart-router/route-and-delegate.ts (~L377–595) — retries stream delegation across alternates, emits failover notices, ends in SP-226 fail-open safe default |
GatewayDispatch.selectFailover() only selects an alternate model; no stream retry loop runs. The embedder owns iterating over failures and re-dispatching |
| Output headroom escalation — exclude failover candidates whose context window cannot fit input plus the required output floor (SP-108) | route-and-delegate.ts + .pi/extensions/smart-router/delegation-runtime.ts — per-attempt computeOutputHeadroom checks; headroomExcludedModelIds accumulate across the failover loop |
src/domain/delegation/output-headroom.ts ships the helper, but no library caller wires it into GatewayDispatch.dispatch() — the embedder must apply it per attempt |
Cursor quota handling — subscription-quota exhaustion detection and failover to cursor/auto or economical API models with cursor_quota_exhausted telemetry (SP-097 / #70) |
The extension stream loop catches quota errors from live streams and drives selectFailover reactively |
Detection and failover selection exist in src/infrastructure/gateway/gateway-dispatch.ts (isCursorQuotaExhaustedError, per-model quota tracking), but the reactive trigger lives in the extension's stream loop — library dispatch alone does not observe provider stream errors |
The middleware is a lifecycle stub, not a router
createPiRouterMiddleware() / RouterHandle.register() (src/api/middleware/pi-router-middleware.ts) registers lifecycle hooks only — compaction flags and model_select overrides consumed when building the next routing request. It does not intercept LLM streams, route requests, or delegate inference. The production stream path lives in the pi extension (route-and-delegate.ts, stream-delegation.ts, delegate-stream.ts), not in the npm-exported middleware. Treat middleware as a flag registrar; routing happens through your call to router.dispatch.dispatch() or the extension's stream path.
Recommended integration path
- pi users: install the extension (
pi install npm:pi-smart-router). It is the supported composition root — everything in the table above works out of the box, including failover, delegate spawn, headroom escalation, quota reaction, hardware probe, and store-backed routing telemetry. - npm embedders: you get the routing core (12-stage pipeline, fleet mapping, gateway health tracking, failover selection). Bare
createRouter()does not wire hardware probe or telemetry by default — passGatewayDispatchOptionsoncreateRouterFromFleet(fleet, options)when you need those ports. Plan to implement your own stream delegation, failover iteration, headroom checks, and planning-delegate spawn around the decisions the pipeline returns — or track #149 (extension public facade), the migration plan for exposing the extension's stream/delegation surface as supported library API so this gap closes over time. Until #149 lands, the extension modules also importsrc/**internals directly, so deep imports intosrc/are not a stable API. See docs/extension-package-boundary.md for the facade vs internal-API boundary, the deep-import lint guard, and the extension coverage gate. See also Composition roots in the 1.0 migration guide.
pi extension path (full product) npm library path (routing core)
──────────────────────────────── ───────────────────────────────
pi (host agent) your host application
└─ .pi/extensions/smart-router/ └─ createRouter() / createRouterFromFleet()
├─ routing pipeline (src/) ════════ ├─ routing pipeline (src/) ← shared core
├─ stream failover loop ✗ ├─ GatewayDispatch: health tracking,
├─ planning delegate spawn ✗ │ failover selection only
├─ output headroom escalation ✗ ├─ lifecycle middleware (stub: hooks only)
└─ cursor quota failover ✗ └─ embedder implements: stream loop,
delegate spawn, headroom checks, quota reaction
✗ = capability exists only on the extension path today; #149 is the plan to close the gap.
Fleet behavior
When you use smart-router/auto, the extension does not read config/models.yaml. Instead:
- Discover —
modelRegistry.getAvailable()returns authenticated models from pi. - Scope — In
scopedmode, filter to patterns from pi settings (getEnabledModels()). Inallmode, use the full registry. - Map —
src/config/pi-model-mapper.tsmaps each pi model to aModelProfile(tier, capabilities, pricing) using provider and model-id patterns. - Route —
createRouterFromFleet()runs the 12-stage pipeline on each request. - Delegate — The extension resolves the chosen model in the registry and forwards the stream via pi-ai's built-in provider APIs.
Unknown models receive conservative economical-cloud defaults. Local providers (lmstudio, ollama) map to zero-tier. Cursor provider models (cursor/*, composer-*, opaque id default) map to frontier-cloud with explicit capability defaults (SP-086, SP-098). v1.4.0 fleet coverage: Kimi K3 / K3.1 Pro ids (k3, k3.1-pro, kimi-k3, …) map to frontier-cloud via pattern rules; k3.1-pro is benchmark-grounded when leaderboard scores exist (aliases cover highspeed / provider-prefixed forms). Gemini 4 carbon and hydrogen match the existing Gemini 4+ frontier rule like argon and stay on pattern_default in provenance.ungrounded_fleet_ids until public scores appear. Benchmark-grounded capability vectors (including multi-fleet github-copilot/*, Gemini, and Anthropic dogfood IDs, aliased family-by-family) are documented in the capability profile coverage report (#108 / #124).
To refresh after auth or settings changes, restart pi or /reload extensions.
Optional: YAML fleet (library API)
For programmatic integration without the pi extension, load a static fleet catalog from YAML and route via GatewayDispatch.dispatch().
This path is catalog-oriented: createRouter() loads models.yaml and constructs GatewayDispatch with library defaults (costEstimator + localRuntime). It does not call the extension's createDispatchOptions() — so hardware probe remains disabled and no store-backed telemetryEmitter is attached unless you pass those options yourself. Prefer the pi extension when you want the full product composition root.
cp config/models.yaml.example ./config/models.yaml
# Edit config/models.yaml — at least one model per tier
import { createRouter, createRouterFromFleet } from 'pi-smart-router';
const router = createRouter({ modelsPath: './config/models.yaml' });
router.register(piExtensionHooks); // lifecycle only: compaction + model override
const decision = await router.dispatch.dispatch(routingRequest);
// Embedder forwards inference to decision.selected_model_id
// Optional: wire probe/telemetry yourself (same options bag as GatewayDispatch)
// createRouterFromFleet(fleet, { systemInfoProvider, hardwareConfig, telemetryEmitter, ... })
Embedder integration paths
| Path | When to use | Routing | Lifecycle hooks |
|---|---|---|---|
| Pi extension (recommended) | Running inside pi | pi install npm:pi-smart-router (or project-local .pi/extensions/smart-router/ when developing from clone) registers smart-router/auto and delegates streams |
Extension calls router.register(); compaction/model overrides wired automatically |
| Library API | Custom host, tests, or non-pi embedders | Your code calls router.dispatch.dispatch() (or wraps the pipeline) |
Call router.register(hooks) to wire compaction and model_select events |
The library createPiRouterMiddleware() / RouterHandle.register() registers lifecycle hooks only — not routing, context capture, or before_provider_request. Do not expect middleware to intercept LLM streams; that is the extension's streamSimple path or your embedder's dispatch loop.
createRouter() returns a RouterHandle:
| Property | Type | Purpose |
|---|---|---|
middleware |
PiRouterMiddleware |
Lifecycle hook registrar (register, lifecycleHookState) |
dispatch |
GatewayDispatch |
Gateway with circuit breaker, failover, rate limiting |
fleet |
readonly ModelProfile[] |
Loaded fleet catalog |
register |
(hooks) => void |
Alias for middleware.register — attach pi lifecycle hooks |
You can also pass a pre-built fleet:
import { createRouterFromFleet } from 'pi-smart-router';
const router = createRouterFromFleet(myFleetProfiles);
Example fleet entry:
models:
- id: local-gemma-4-7b
tier: zero-tier
provider: lmstudio
endpoint: http://localhost:1234/v1
capabilities:
reasoning: 0.3
code_gen: 0.6
tool_use: 0.1
pricing:
registry_key: local/free
fallback_cost_per_1m: 0.0
- id: claude-3.5-haiku
tier: economical-cloud
provider: anthropic
# ...
- id: claude-3.5-sonnet
tier: frontier-cloud
provider: anthropic
# ...
Tiers: zero-tier, economical-cloud, frontier-cloud. See config/models.yaml.example.
Routing cluster catalog (library API)
Reference prompts grouped by tier bias for semantic cluster matching (SP-099). Operators tune clusters in YAML without code changes. Precomputed centroids live in config/routing-centroids.json (SP-114); when that file is absent, centroids are computed at load time as the mean embedding of each cluster's reference prompts.
cp config/routing-clusters.yaml.example ./config/routing-clusters.yaml
cp config/routing-centroids.json.example ./config/routing-centroids.json
# Edit reference_prompts, min_similarity, and min_margin per cluster
# Regenerate centroids after catalog changes:
npm run routing:bootstrap-centroids
The bootstrap script embeds each reference prompt via the HyDRA MiniLM ONNX pipeline (384-dim), mean-pools to centroid vectors, and writes config/routing-centroids.json with { cluster_id, tier_bias, centroid, reference_count } per cluster. ONNX artifacts cache under .pi-smart-router/models/ on first run.
import { loadRoutingClusters } from 'pi-smart-router';
const catalog = await loadRoutingClusters({
filePath: './config/routing-clusters.yaml',
embedder: myTextEmbedder, // shared ONNX embedder (SP-100)
});
// Reason codes: cluster_${id} — e.g. cluster_low_stakes_general
// createClusterMatcher (cluster-matcher module) prefers routing-centroids.json when present.
Cluster IDs are stable reason-code prefixes (cluster_low_stakes_general, cluster_architecture, etc.). See config/routing-clusters.yaml.example.
Configuration
Environment variables
| Variable | Default | Purpose |
|---|---|---|
ROUTER_STATE_DB_PATH |
./.pi-smart-router/state.db |
Override SQLite state store location (telemetry, pricing catalog, session data) |
SMART_ROUTER_LOG_ROUTING |
(unset) | Set to 1 to log each routing decision to stderr as JSON (debugging dogfood sessions). Canonical payload builder (buildRoutingDecisionLogPayload) includes top-level stage, reason_code, low_intensity_score, tier_hint, local_eligible_reason, and cluster_id (plus nested cluster_summary / features). The pi extension’s live stderr logger is still a slim subset — see LOG_ROUTING field checklist |
SMART_ROUTER_DATASET |
(unset) | Set to 1 to opt in to privacy-safe routing dataset capture (metadata and feature fields only; 30-day / 10k-row retention). Prompt text, messages, and tool arguments are never stored. Required for outcome labels and P(success) training export. See #8. |
SMART_ROUTER_DATASET_FINGERPRINT |
(unset) | Set to 1 (requires SMART_ROUTER_DATASET=1) to store an install-local HMAC-SHA256 fingerprint of each normalized prompt for duplicate detection within this install. The install pepper lives in .pi-smart-router/.dataset-key (gitignored) and is never exported. Warning: short or common prompts are vulnerable to offline rainbow-table guessing; use only when you accept that tradeoff. See #10. |
SMART_ROUTER_DATASET_EMBEDDINGS |
(unset) | Set to 1 (requires SMART_ROUTER_DATASET=1) to capture the raw 384-dim HyDRA encoder embedding on each dataset row (derived dense vector of the metadata-prefixed routing input — never prompt text). Embeddings stay local until you additionally export with --embeddings; they exist so the hydra_projection ≥100-row training floor can ever be met. See #170. |
MODELS_YAML_PATH |
./config/models.yaml |
Fleet catalog path (library API only) |
SMART_ROUTER_PLANNING_TURN_BUFFER |
2 |
SAAR planning buffer: frontier planning turns allowed before hard-lock (v0.2.0 Continuity) |
SMART_ROUTER_PLANNING_DELEGATE_ENABLED |
true |
Enable cache-preserving planning delegate (#71) |
SMART_ROUTER_PLANNING_DELEGATE_MAX_MESSAGES |
12 |
Compressed-context message cap for frontier sub-call |
SMART_ROUTER_PLANNING_DELEGATE_MAX_TOKENS |
16384 |
Compressed-context token cap for frontier sub-call |
SMART_ROUTER_PLANNING_DELEGATE_EXCLUDE_EXECUTION_HISTORY |
true |
Exclude tool execution history from delegate payload |
SMART_ROUTER_PLANNING_DELEGATE_GLOBAL_TIMEOUT_MS |
120000 |
Global cap (ms) on the whole planning-delegate stage per planning turn — bounds fan-out wall-clock so a stalled worker cannot hang TTFT (#120) |
SMART_ROUTER_PLANNING_DELEGATE_SUB_CALL_TIMEOUT_MS |
30000 |
Per-call cap (ms) on each delegate sub-call worker; on expiry the worker is cancelled/abandoned and routing falls back to direct frontier with planning_delegate_timeout (#120) |
SMART_ROUTER_ADAPTIVE_REASONING_ENABLED |
true |
Master switch for the adaptive thinking-level policy (#166); false passes the session thinking level through unchanged |
SMART_ROUTER_ADAPTIVE_REASONING_MIN_LEVEL |
(unset) | Floor on policy-derived thinking levels (minimal|low|medium|high|xhigh|max) — see Adaptive reasoning |
SMART_ROUTER_ADAPTIVE_REASONING_MAX_LEVEL |
(unset) | Ceiling on policy-derived thinking levels (incl. turn-class upgrades) — see Adaptive reasoning |
SMART_ROUTER_PREFIX_CACHE_WEIGHT |
0.20 |
SAAR weight on warm prefix value in cache breakeven math (0–1; #73) |
SMART_ROUTER_IDLE_TIMEOUT_SECONDS |
300 |
SAAR idle seconds before pin reopens for full re-route |
SMART_ROUTER_SWITCH_THRESHOLD |
0.5 |
SAAR switch score gate (0–1) for tier upgrades during hard-lock |
ROUTER_SAFE_DEFAULT_TIER |
economical-cloud |
Fallback tier on any routing failure |
LITELLM_PRICING_URL |
— | LiteLLM pricing JSON source |
LOG_ROUTING field checklist
When SMART_ROUTER_LOG_ROUTING=1, prefer the canonical payload from buildRoutingDecisionLogPayload (library / tests). Checklist for #99:
| Field | In payload builder? | Notes |
|---|---|---|
stage |
Yes (top-level) | Pipeline stage that decided |
reason_code |
Yes (top-level) | Machine-readable reason |
low_intensity_score |
Yes (top-level + cluster_summary) |
Null when low-intensity stage did not run |
tier_hint |
Yes (top-level + cluster_summary) |
Null when no tier hint |
local_eligible_reason |
Yes (top-level + features) |
Null when local_zero did not evaluate eligibility |
cluster_id |
Yes (top-level + cluster_summary) |
Null when no cluster match |
pricing_window |
Yes (top-level + peak_pricing_summary) |
Peak vs off-peak rationale for the selected model (SP-244 / #165); the extension stderr logger carries pricing_window + peak_pricing |
Gap: the pi extension’s live stderr path (logRoutingDecision in .pi/extensions/smart-router) still emits a slim JSON object (selected_model_id, stage, reason_code, features, delegate) and does not yet call buildRoutingDecisionLogPayload. SQLite /smart-router history and the payload builder carry the full checklist; wire the extension logger in a follow-up if dogfood needs identical stderr shape.
History model id: /smart-router history resolves bare/smart-router virtual auto to the concrete planning-delegate primary (or qualifies Cursor opaque auto as cursor/auto) so operators see the delegated fleet model, not the virtual router id.
SAAR session pin and cache breakeven (v0.2.0 Continuity)
v0.2.0 adds Session-Aware Agentic Routing (SAAR) pin knobs (#72) and a cache breakeven gate (#73) that blocks tier switches when marginal_savings + future_cache_value <= cache_reprime_cost — preventing cheap-turn savings from invalidating a warm prefix cache.
| Knob | Env var | Default | Effect |
|---|---|---|---|
| Planning buffer | SMART_ROUTER_PLANNING_TURN_BUFFER |
2 |
First N turns may route planning to frontier while pin metadata stays economical |
| Prefix cache weight | SMART_ROUTER_PREFIX_CACHE_WEIGHT |
0.20 |
Discounted future cache credit in breakeven |
| Idle reopen | SMART_ROUTER_IDLE_TIMEOUT_SECONDS |
300 |
Seconds of inactivity before SAAR resets and pin reopens |
| Hard-lock upgrade gate | SMART_ROUTER_SWITCH_THRESHOLD |
0.5 |
Score threshold for tier upgrades after buffer exhaust |
Dogfood verification (multi-turn planning session)
- Start pi with routing logs:
SMART_ROUTER_LOG_ROUTING=1 pi(optional: tune SAAR env vars above). - Run
/model smart-router/autoand begin a multi-turn planning session (planning turns mixed with tool results). - Inspect stderr JSON lines — confirm
saar_summary.buffer_active/saar_reason_code: saar_buffer_activeon early planning turns, thenhard_lock: true/saar_hard_lockafter the buffer exhausts. - On a warm pinned session, trigger a
tool_resultsub-route — when breakeven fails, expectbreakeven_summary.decision: "blocked"andbreakeven_reason_code: breakeven_blockedwhile the pin holds. - Use
pi router explain(orPOST /v1/route/explain) on the same session —features.breakevenandfeatures.saarmirror telemetry fields for operator audit.
See routing-roadmap.md §2 P0 for design context.
Long-running pi sessions — in-memory state eviction (v0.21.0, #145). Session pins and in-memory routing snapshots live in process memory, not in the SQLite telemetry store. When pi ends a session, the extension's session_shutdown handler (reason: quit / reload / new / resume / fork) calls evictInMemorySessionState (src/api/session-eviction.ts) and drops all in-memory routing state for that session — pins, cache-breakeven snapshots, turn metadata. A new session starts cold: no stale pin, no warm-cache assumption carries over. Persistent telemetry in .pi-smart-router/state.db is untouched. As a safety net for orphaned sessions (e.g. a crashed pi process that never emitted session_shutdown), a sweep on session_start evicts sessions idle longer than ORPHAN_SESSION_TTL_MS (24 hours, exported from .pi/extensions/smart-router/session-lifecycle.ts); the sweep fails open — a missing session id or sweep error never blocks session start.
Planning delegate (v0.4.0 Delegate)
When a planning turn would route primary inference to frontier while a warm economical session pin is active, smart-router prefers cache-preserving delegation (#71):
- Pipeline (
turn_envelope) emitsplanning_delegate— primary stays on the pinned economical model;features.planning_delegatenames the frontier delegate model and compressed-context limits. - Pi extension (
.pi/extensions/smart-router) runs an ephemeral frontier sub-call with compressed context (tool execution history excluded by default), injects the result as an observation user message, then delegates primary streaming to the pinned economical model. - Fallback — when delegate is disabled, spawn fails, or the delegate model is missing from the registry, the extension falls back to a direct frontier route with a documented
fallback_reasonin explain/telemetry.
Stream piping (SP-170): Primary delegated inference live-forwards provider events to pi (start / text_delta / … as they arrive). The planning-delegate sub-call stays buffered — only the final observation text is injected into primary context; frontier tokens from the ephemeral sub-call are discarded and never reach the user-facing stream. On infra failover, a synthetic text_delta notice is pushed after the retry stream's start (no mutation of a buffered event array).
| Knob | Env var | Default | Effect |
|---|---|---|---|
| Delegate enabled | SMART_ROUTER_PLANNING_DELEGATE_ENABLED |
true |
When false, SAAR buffer allows direct frontier planning (planning_direct_frontier + planning_delegate_disabled) |
| Compressed message cap | SMART_ROUTER_PLANNING_DELEGATE_MAX_MESSAGES |
12 |
Max messages sent to the frontier sub-call |
| Compressed token cap | SMART_ROUTER_PLANNING_DELEGATE_MAX_TOKENS |
16384 |
Token budget for compressed delegate context |
| Exclude tool history | SMART_ROUTER_PLANNING_DELEGATE_EXCLUDE_EXECUTION_HISTORY |
true |
Strip tool-call / tool-result turns from delegate payload |
Coordination boundary with pi core: smart-router owns routing (when to delegate, which models, compressed limits, fallback reason codes). Sub-agent spawn and observation injection run in the pi extension via streamSimple — pi core must expose a delegate/stream API the extension can call; smart-router does not orchestrate pi's outer sub-agent scheduler. Operators enabling /model smart-router/auto get delegate behavior automatically when the extension is loaded; no separate pi sub-agent config is required beyond a frontier model in the registry.
Dogfood verification (planning delegate)
- Start pi with routing logs:
SMART_ROUTER_LOG_ROUTING=1 piand/model smart-router/auto. - Begin a session on an economical pin (routine prompts), then trigger planning turns (e.g. architecture or multi-step design work).
- Inspect stderr JSON — on delegate turns expect
reason_code: planning_delegate,planning_delegate_summary.path: "delegate",primary_model_idequal to the pin, anddelegate_model_idpointing at frontier. - Confirm primary inference stays on the economical model (cache-friendly) while stderr shows
[smart-router] planning delegate sub-call completedwith the frontier model id. - Disable delegate (
SMART_ROUTER_PLANNING_DELEGATE_ENABLED=false) and repeat — expectplanning_direct_frontierwithfallback_reason: planning_delegate_disabled. - Use
pi router explain(orPOST /v1/route/explain) on the same session —features.planning_delegatemirrors live routing (path: delegatevsdirect,fallback_reasonwhen applicable).
See routing-roadmap.md §2 P0 and GitHub #71 for acceptance criteria.
Adaptive reasoning (thinking level) (#166)
Adaptive reasoning tunes the thinking intensity of the model already selected — it never changes which model runs. Per turn class:
| Turn class | Policy level |
|---|---|
tool_result |
minimal |
main_loop |
low |
planning / planning_delegate |
medium |
frontier escalation / loop_escalation |
high |
pi passes the session thinking level on every call; the router treats pi's ambient default (medium) as adjustable by policy, and any other explicit level as an operator /thinking floor that is never lowered (a turn-class upgrade may still raise it). Chatty profiles (high verbosity_factor) additionally get a one-line conciseness nudge at minimal/low.
Three knobs that sound alike, do different things:
| Knob | Acts on | What it changes |
|---|---|---|
Adaptive reasoning (adaptive_reasoning.*) |
The delegated call's reasoning option |
Thinking intensity of the already-selected model — cost of thinking, not model choice |
frugality.lambda_verbosity |
Multi-objective selection scoring | Which model gets picked — penalizes verbose models while ranking candidates; never touches the delegated call's reasoning option |
/thinking (pi session command) |
The caller-provided reasoning level | Explicit operator override — an explicit level is never lowered by policy or bounds |
Operator knobs (config adaptive_reasoning / env):
| Key | Env var | Default | Effect |
|---|---|---|---|
enabled |
SMART_ROUTER_ADAPTIVE_REASONING_ENABLED |
true |
When false, the policy is skipped — delegated calls pass the session thinking level through unchanged (reasoning_reason_code: adaptive_reasoning_disabled) |
min_level |
SMART_ROUTER_ADAPTIVE_REASONING_MIN_LEVEL |
(none) | Floor: policy-derived levels are raised to at least this level. Discrete level (minimal|low|medium|high|xhigh|max) — deliberately not a free-form verbosity percent |
max_level |
SMART_ROUTER_ADAPTIVE_REASONING_MAX_LEVEL |
(none) | Ceiling: policy-derived levels (incl. turn-class upgrades) are capped at this level. When min_level exceeds max_level (e.g. via env), the ceiling wins (cost-safe) |
Floor/ceiling bind only what the policy itself derives. An explicit operator /thinking choice is never lowered by either bound (a floor can still raise one via the policy-upgrade path). Both bounds re-clamp down to the model's supported levels.
Fail-open behavior: models that do not support reasoning options (reasoning: false, or a thinkingLevelMap mapping every relevant level to null) pass caller options through unchanged — telemetry records reasoning_reason_code: reasoning_unsupported and the route never fails. Providers that ignore reasoning options degrade to a no-op.
Telemetry: each routed delegation records reasoning_level_requested (the session/caller level), reasoning_level_applied (the effective delegated level), and reasoning_reason_code (e.g. turn_envelope_main_loop, operator_thinking_floor, operator_floor_applied, operator_ceiling_applied, reasoning_unsupported) on the routing telemetry row — enriched post-delegation like usage actuals (SP-241). Inspect via /smart-router history.
Typed judgment (opt-in) (#183)
Typed judgment is an opt-in, non-deciding pipeline stage (typed_judgment, directly after turn_envelope and before context_fit) that asks a Jev-compatible “systemone” semantic judge six structured questions about each user-prompt turn and records the normalized answers on the routing context. The stage itself never selects a model. With #188 landed, enabling the stage allows downstream turn_envelope, low_intensity, and local_zero consumers to use those answers as described below. The default remains off, and a missing judgment preserves the deterministic / regex fallback behavior.
The six questions (stable ids), their normalized forms, and their consumers:
| Question id | Kind | What it asks | Normalized signal and consumer |
|---|---|---|---|
task_kind |
choice (10 anchored options) | The single kind of work the user message asks for: plan, implement, debug, refactor, review, research, explain, operate, write, chat |
task_kind plus task_kind_confidence; consumed by turn_envelope for planning classification (#188) |
capability_deserved |
score 0–3 | Setting price aside, how much model capability the request deserves | 0, 1/3, 2/3, or 1; consumed by low_intensity for tier hints and capability filtering (#188) |
needs_deep_reasoning |
noul (yes/no) | Whether the turn needs extended multi-step reasoning rather than recall, lookup, or a short direct edit | deep_reasoning_p (0–1); recorded only — the adaptive-reasoning consumer remains #190 |
code_gen |
score 0–3 | How much code or configuration must be written or modified | 0, 1/3, 2/3, or 1; recorded only — the HyDRA requirement-source consumer remains #189 |
tool_use |
score 0–3 | How much tool execution (shell, file, search, agentic calls) the turn requires | 0, 1/3, 2/3, or 1; consumed by the local_zero capability gate (#188) |
destructive |
noul (yes/no) | Whether satisfying the request could destroy, overwrite, or irreversibly modify data, state, or history | destructive_p (0–1); consumed by the local_zero safety gate (#188) |
Landed #188 consumer behavior:
| Consumer | Judgment field | Behavior and attribution |
|---|---|---|
turn_envelope |
task_kind |
At confidence ≥ typed_judgment.min_confidence, plan, debug, or review selects planning classification. A confident non-planning answer can replace only regex-derived planning when the host did not explicitly set turn_type. A positive judgment-driven planning override uses reason code typed_judgment_planning. |
low_intensity |
capability_deserved |
Level 0 hints zero-tier, level 1 (1/3) hints economical-cloud, and levels 2–3 (≥2/3) hint frontier-cloud. Any non-zero level removes zero-tier and flash-class candidates when another healthy candidate remains. At confidence ≥ min_confidence the judgment may replace the prior hint in either direction; below the threshold it may only raise a tier. Session-depth and loop-escalation frontier floors still win, and context-fit constraints remain authoritative. Explain attribution uses typed_judgment_tier_zero-tier, typed_judgment_tier_economical-cloud, or typed_judgment_tier_frontier-cloud. |
local_zero |
destructive, tool_use |
destructive_p ≥ 0.7 or judged tool_use above the effective local ceiling skips local dispatch with typed_judgment_destructive or typed_judgment_tool_use_shortfall. The ceiling is min(local model tool_use capability, local_zero.max_tool_use_requirement). These safety gates may skip local dispatch even below min_confidence; a missing tool_use answer retains the legacy cue-count gate, and a null judgment retains prior behavior. |
Remaining Sprint 2 work: #189 (typed judgment as a HyDRA requirement source), #190 (adaptive reasoning from needs_deep_reasoning), and #191 (eval comparison and latency benchmark). Epic #183 therefore remains open; enabling #188 does not change those consumers or flip any encoder, requirement-source, frugality, or reasoning default.
Observability: explain and LOG_ROUTING expose the normalized signal in features.typed_judgment (including confidences, skip_reason, and latency_ms), tier attribution in features.tier_hint_reason_code, and local gate outcomes in features.tier_selection.local_zero_skip_reasons.
Default off. typed_judgment.enabled defaults to false — existing installs make no judge call and pay zero added latency. When enabled, the stage runs only on user-prompt turns: tool_result turns, acknowledgement-only turns, and explicit force_model_id turns skip it, and any timeout or judge error fails open — routing proceeds exactly as if the stage were unwired. Skip/failure reason codes recorded on the routing context: typed_judgment_disabled, typed_judgment_tool_result, typed_judgment_acknowledgement, typed_judgment_force_model, typed_judgment_timeout, typed_judgment_error (plus typed_judgment_remote_key_missing when a remote endpoint has no API key available — see privacy note below).
Running the judge locally (OpenJev). The intended default is a local OpenJev server — no turn text leaves the machine:
- Endpoint:
http://127.0.0.1:8080/v1/systemone(default) - Model:
openjev-latest(default)
Latency. Measured on local OpenJev on Apple silicon: ~385–990 ms per judged user turn; timeout_ms defaults to 1500 to cover p99 with headroom. route() is serialized per pipeline, so judge latency adds to user-turn routing latency only — tool_result turns never invoke the judge and pay nothing.
Config keys and env overrides (SP-304):
Config key (typed_judgment.*) |
Env var | Default | Effect |
|---|---|---|---|
enabled |
SMART_ROUTER_TYPED_JUDGMENT_ENABLED |
false |
Master switch — no judge calls when off |
endpoint |
SMART_ROUTER_TYPED_JUDGMENT_ENDPOINT |
http://127.0.0.1:8080/v1/systemone |
Judge endpoint; must be loopback (127.0.0.1, ::1, or localhost) unless allow_remote is true |
model |
SMART_ROUTER_TYPED_JUDGMENT_MODEL |
openjev-latest |
Judge model id (OpenJev uses it; hosted Jev ignores it) |
timeout_ms |
SMART_ROUTER_TYPED_JUDGMENT_TIMEOUT_MS |
1500 |
Per-call timeout; on expiry the stage fails open with typed_judgment_timeout |
samples |
SMART_ROUTER_TYPED_JUDGMENT_SAMPLES |
1 |
Judge samples per question (OpenJev self-consistency extension; omitted for hosted Jev) |
api_key_env |
SMART_ROUTER_TYPED_JUDGMENT_API_KEY_ENV |
TYPESAFE_API_KEY |
Name of the env var holding the API key — never the key itself |
allow_remote |
SMART_ROUTER_TYPED_JUDGMENT_ALLOW_REMOTE |
false |
Permit non-loopback (hosted) endpoints; default false = fail closed to local judge only |
min_confidence |
SMART_ROUTER_TYPED_JUDGMENT_MIN_CONFIDENCE |
0.6 |
Confidence threshold for task_kind planning overrides and capability_deserved tier replacement; below it, capability judgments may raise but never lower a tier. The local_zero safety gates still apply |
Invalid env overrides (e.g. a remote endpoint without allow_remote) fail closed: the base config stays intact and disabled, and no judge call is made.
Privacy note (#184). With the default local OpenJev setup, turn text never leaves the machine — the judge state (token-capped, with no spend/budget keys) is sent only to the loopback endpoint. Pointing endpoint at a hosted judge (TypeSafe Jev or Codiv) sends turn text off-machine: that requires the explicit opt-in allow_remote: true, and the adapter reads the API key at call time from the environment variable named by api_key_env (default TYPESAFE_API_KEY). API keys are never read from, or stored in, config files — config carries only the env var name. If a remote endpoint is configured but the key env var is unset, the judge adapter is not created (typed_judgment_remote_key_missing) and routing proceeds without judgments. Prompt and judge-state text is never logged; errors are redacted before logging.
Attribution. The question rubrics — level descriptions and criteria for task_kind, capability_deserved, and needs_deep_reasoning — are adapted from pi-jev-model-router (MIT License, © 2026 Shah Rukh).
Degraded neural failover sandwich (#119)
When the encoder/neural stage (HyDRA) fails, is misconfigured, or exceeds its latency budget without a selection, routing fails open through a cheap chain instead of crashing the host agent (#119):
- learned — optional privacy-safe map keyed by requirement fingerprint (SHA-256 of the rounded requirement vector) or cluster id → preferred tier. Raw prompt text is never stored. Exact-key policy: fingerprint match first, cluster id second (no fuzzy matching). Writes are validated (bounded floats, snake_case cluster ids, tier enum) and capped (FIFO eviction) so confounder attacks cannot poison routing memory.
- heuristic — optional operator pattern pack (router_rules-style regex overlay) for known-simple intents. Deny-by-default (no match → no decision) and fail closed on invalid regex (the rule is rejected at load and never applies).
- safe_default — context-fit aware safe economical/frontier default (
degraded_safe_default).
Explain/telemetry expose route_path (neural | learned | heuristic | safe_default) plus route_path_confidence on every decision; degraded decisions carry reason codes degraded_learned_route, degraded_pattern_<rule_id>, or degraded_safe_default. A learned/pattern suggestion toward a cheaper tier is only honored when the cheap tool-use cue estimate is below pattern_tool_use_ceiling — a cheap overlay never alone overrides a predicted capability shortfall.
Knob (degraded_route operator config) |
Default | Effect |
|---|---|---|
enabled |
true |
When false, neural failures use the legacy safe_default stage pass-through |
learned_min_confidence |
0.6 |
Minimum learned-entry confidence to honor a tier suggestion |
learned_max_entries |
512 |
Learned-map cap per key space (FIFO eviction) |
pattern_tool_use_ceiling |
0.3 |
Tool-use cue ceiling for honoring cheaper-tier learned/pattern suggestions |
fail_closed_on_missing_weights |
false |
When true, missing/placeholder neural weights fail closed: the matcher throws before paying embedding cost and the pipeline routes through the degraded sandwich (neural_misconfigured) with the SP-251 reason codes on the decision. When false (default), routing stays fail-open — placeholder weights score with neutral defaults and the reason codes are advisory telemetry only |
Missing-weights reason codes (v0.21.0, #148). When the HyDRA matcher cannot load real neural weights, the decision surfaces structured reason codes (src/domain/matching/missing-weights-reason-codes.ts) on the routing decision / requirement_reason_codes — visible in explain (pi router explain / POST /v1/route/explain), /smart-router history, and telemetry — not stderr-only:
| Reason code | Meaning | Operator action |
|---|---|---|
hydra_weights_missing |
HyDRA projection-head weights artifact is absent or unloadable, so neural scoring is running on fallback behavior | Restore the weights artifact (see HyDRA model cache); routing continues fail-open unless fail_closed_on_missing_weights is set |
k4_heads_placeholder |
ModernBERT K4 heads are placeholder (untrained) weights — scores are neutral defaults, not learned predictions | Regenerate/install the trained K4 heads artifact; treat current K4 scores as non-authoritative |
With the default fail-open behavior these codes are advisory: the route proceeds with safe defaults. Set degraded_route.fail_closed_on_missing_weights: true when you prefer an explicit degraded-route decision (sandwich chain above, route_path records the branch taken) over silently routing on placeholder neural scores.
Distinct from soft heat affinity (healthy-path bias): this is failover / skip-expensive-stage only. Routing remains pre-generation — no FrugalGPT-style cascades (see routing-roadmap.md §1).
Virtual cost v2 (v0.5.0 subscription economics)
Virtual cost v2 extends SP-096 flat quota_cost_per_1m with deterministic subscription-window economics (#78). It inflates effective frontier cost late in a rolling quota window and credits warm prefix-cache value on active pins — without MDP or reinforcement-learning quota policy (SeqRoute HBR+CQL is deferred).
Formula (per turn)
effective_cost_usd = base × λ + quota_arbitrage_premium + exhaustion_risk_premium + kv_cache_savings
| Component | Meaning |
|---|---|
base |
SP-096 subscription virtual cost (quota_cost_per_1m) or sticker fallback_cost_per_1m |
| λ (quota decay) | Multiplier rising from 1 at full window toward lambda_max_multiplier as budget depletes |
| Quota arbitrage premium | Opportunity-cost uplift for burning subscription quota late in the window |
| Exhaustion risk premium | Extra penalty when remaining window fraction falls below exhaustion_risk_threshold |
| KV-cache savings | Negative credit when pin is active and prefix is warm (prefix_cache_discount × prefix_cache_weight) |
Window position
Rolling-window position is supplied to the router pipeline as quotaWindowPosition (library API / telemetry integration). Use remaining_window_fraction in [0, 1] (1 = full budget). Optionally derive it from elapsed time and consumed quota via `deriveRemainingWindowFracti