Cascades and adaptive learning
Rendered from docs/cascades-and-learning.md at commit 18614e438388. The repository file is authoritative.
Neither changes default behaviour. The deterministic, evidence-ranked router stays the default and the fallback; each feature below can be switched off (or rolled back) to return to it.
Cascades
aliases:
auto-cascade:
profile: quality
cascade:
enabled: true
first_profile: fast # who answers first: the best eligible deployment under this profile
escalate_on: [length, empty, invalid_json, missing_tool_call]
max_stages: 2
A request to this alias is answered first by the best eligible deployment under first_profile. If that
answer hits a structural trigger it is discarded — nothing has been sent to the client — and the next
stage (the alias's own ranking) answers.
| trigger | meaning |
|---|---|
length |
the answer was cut off by the output limit |
empty |
no content and no tool call |
invalid_json |
response_format asked for JSON and the content is not valid JSON, not of the schema's top-level type, or misses top-level required keys |
missing_tool_call |
tool_choice required a call and none was returned |
Rules that do not bend: every stage comes from the deployments that passed the same hard filters for
this request; cascades apply to non-streaming requests only (a stream is committed at its first
byte) and never inside a tool round trip, a reasoning continuation or a pinned session — the decision
says cascade.not_applied and why. The triggers are not a quality judgement: a fluent wrong answer is
accepted, and no model's self-reported confidence is used. The last stage's answer is returned as it is,
even if it also fails a check (X-ModelSaar-Cascade: stages=2; accepted=false); nothing is repaired.
The cost is real: an escalation adds the first stage's full latency. The decision record carries the plan
and the trace (cascade.stages, cascade.trace).
Ensembles are implemented only where aggregation is well defined — bounded voting over closed choices
(POST /v1/vote, guardrail ensembles; docs/pipelines.md). Free-text ensembles are an explicit non-goal.
Scheduling across hosts (replicas)
routing:
replica_scheduling: least_loaded # default: off
When the best-ranked deployment has verified replicas — the same model identity (e.g. the same Ollama
digest) served by another backend, typically another host with its own resource_pool — the request goes
to the replica whose backend and pool are least occupied (in-flight + uncertain + queued, relative to
their limits), and if admission refuses that one it spills over to the next replica before anything is
dispatched. Different models are never treated as replicas (ranking decides between models), and a pinned
session, a tool round trip or a reasoning continuation stays on its deployment. The decision records
scheduling.replicas and the occupancy it saw. Occupancy is what this gateway dispatched; load put on a
host by other clients is invisible to it. This is replica-level load sharing, not a cluster scheduler:
there is no work stealing, no cross-host batching and no placement of models. Evidence:
tests/test_replica_scheduling.py (3; two fixture backends as two hosts); also exercised on a Mac and a GX10, each serving identical model weights.
Adaptive learning from outcomes
learning:
enabled: true # default false: nothing is recorded
holdout_fraction: 0.2
min_samples: 20 # per (deployment, task) cell in the training split
min_holdout: 10
min_improvement: 0.0
retention_days: 30
What is recorded (state database only, never sent anywhere): request id, deployment id and fingerprint, the routing task label, one boolean, a source tag, a timestamp. No prompt, answer, tool argument or caller identity.
POST /v1/feedback {"request_id": "req_…", "success": true|false}— the originating client’s verdict for a completed request. An opt-in decision record includes a caller proof unique to that request and the model fingerprint captured at dispatch. Feedback received after a model changes still belongs to the original fingerprint. Older records without this evidence cannot receive feedback. Free text is rejected.- A structural failure the gateway observed itself (bad envelope, truncated/failed/timed-out stream) is recorded as a failure. A successful HTTP response is not recorded as a success.
Outcomes never change routing by themselves.
modelsaar learn status
modelsaar learn build # snapshot a policy version; score it on the holdout split
modelsaar learn activate 3 # only possible when the holdout verdict is "better"
modelsaar learn rollback # back to the simpler router at once
modelsaar learn purge # delete every outcome and policy
build takes the outcomes whose fingerprint matches the current deployment, splits them by a hash of the
request id, estimates success per (deployment, task) from the training split (shrunk towards what the
router uses today) and compares both predictions on the holdout split by Brier score. A version can be
activated only with verdict better; there is no override. Activated estimates appear in decisions as
evidence learned:v<N>:<samples> — never as measured — and a harness measurement for the same task always
wins. A changed model (new fingerprint) no longer matches. There is no exploration: the gateway never
sends a request somewhere in order to learn.
Known limits: feedback is whatever clients choose to send (selection bias — people report failures more readily); the holdout comes from the same traffic as the training split; the estimate is per task label, not per prompt. This is why activation is an administrator's decision on a visible report.
Evidence
tests/test_cascade.py (6), tests/test_learning.py (5) and tests/test_replica_scheduling.py (3), real HTTP against the fake backend
(scripted outcomes). A real llama.cpp model also exercised the learning cycle through activation, a routed request and rollback. Live evidence. Estimating routing benefit on real traffic requires a
measured run in the style of docs/plans/measured-routing-quality.md on representative traffic.