docsOperate

AI routing -- levels, policies and rules

Every call MemQL makes to a model is decided by three things, in this order:

  1. the level the call declares -- how much intelligence it needs,
  2. the rule that matches the call -- which looks at what the call is for,
  3. the policy that rule names -- an ordered chain of places to look.

Nothing in the platform names a model at a call site. That is the point: a model name at a call site is a release every time your fleet changes.


Fleet provides a persistent visual policy editor and review of typed Ask policy proposals, following Supervised Visual Composition. Generating proposals requires compatible inference to be configured. Manual policy editing remains available independently. This page describes the underlying engine contract.

The three nouns

Level

A level is how much intelligence a call needs. There are exactly four, and there will not be a fifth -- an abstraction you cannot hold in your head is not one.

LevelWhat declares it
fastTriage, intake, classification, summaries, suggestions, the safety classifier
strongAn agent's reply, a conductor turn, an authoring design pass
reasoningEmitting or repairing a construct, re-planning a run
embeddingsEvery embedding call

Every DSL prompt carries @level. A prompt without one refuses to load, and a product bundle mounted at MEMQL_DSL_PATH is held to the same rule. A Go call site with no prompt -- suggest, the safety classifier, vision, embeddings, the chat handler -- names its level in the request.

Modality is never declared. Whether a call is chat, streaming chat, tools, structured output, vision or an embedding is derived from the call itself, and the router interface-checks it. You do not write it anywhere.

Policy

A policy is an ordered chain of places to look. The general chains reach paid inference last; the embeddings chain names only the bound embedder:

PolicyChain
fastLocalFirstfleet:fastest, app:*, federation:cheapest
localFirstfleet:strongest, app:*, federation:cheapest
localOnlyfleet:strongest
federationStrongestfleet:strongest, app:*, federation:strongest
embeddingsBindingembedder:active

The order inside a chain is three kinds of cost, increasing:

  • fleet: -- a model on hardware you already own. Costs electricity.
  • app: -- a signed-in Claude Code or Codex on one of your machines. Costs a subscription you already pay for; MemQL is not billed for it.
  • federation: -- a metered vendor. Costs money, and is therefore the hop the cost ceiling gates.

The ceiling is asked only when a local door preceded the vendor in the chain. A chain that starts at a vendor is a decision somebody made, not a fallback, and refusing it here would break every deliberately-paid policy.

A chain entry is one of a closed set of forms:

text
streamClaudeSonnet a provider by name
fleet:strongest the best local model that can serve this call
fleet:fastest the quickest local model that can serve this call
fleet:qwen3.8:27b one local model by id
app:* any signed-in app on one of the caller's machines
app:claude-code one app by id
app:claude-code:claude-opus-5 one app by id, with its model pinned
federation:cheapest the cheapest vendor record that qualifies
federation:strongest the strongest vendor record that qualifies
federation:streamClaudeSonnet one vendor record by name
policy:localFirst another policy, expanded at load

fleet:* is retired. It said "any", which is not what it did; write fleet:strongest.

An app id is held to the engine's closed runnable set at LOAD. app:gemini-cli refuses to load, naming the set, because the engine has no protocol for a third app -- so an entry naming one would be a chain step that could only ever be passed over, which reads months later as a door that is shut rather than as a policy that is wrong. Growing the set is a value change in core/airoute/apps.go, not a release.

app:<id>:<model> pins that app's model, the way fleet:<modelId> pins a fleet model. The wildcard cannot take one: app:* names any signed-in app and a model name belongs to ONE app, so app:*:claude-opus-5 would ask Codex for a Claude model. It refuses rather than being ignored on the apps it cannot apply to. What the app ACTUALLY served with comes back on the decision row as servedModel, which is how you see an app that rerouted or ignored the pin.

fleet:strongest keeps measured structured validity first, then the owner's explicit model preference. Its size heuristic ranks active parameters per token descending, using total parameters when the runtime has not reported an active count; context window, recognized quantization precision, and model id break ties. For otherwise equal variants, Q8 wins over Q4 retained after an upgrade; an explicit Q4 preference still wins over this tie-break. A 27B dense model therefore ranks ahead of a 35B mixture with 3B active parameters when neither has a measurement or an explicit preference. Total parameters remain available for memory planning.

fleet:fastest ranks the same effective parameter count ascending and excludes models below 3B effective parameters, including unknown sizes. This floor is a routing heuristic, not a quality benchmark. With 27B and 9B dense models available, ordinary fast calls choose 9B and strong calls choose 27B. A 0.8B model remains available through an explicit fleet:<modelId> pin. The floor applies to every candidate expanded from the fast selector, including fallback candidates.

Fast ordering currently uses size, not measured throughput: token rates, audio real-time factors and image timings are different units and are not compared. Fast selection does not consult the owner's strongest-model preference.

federation:cheapest sorts a record with no cost figures LAST, and the decision record says so. Sorting it first would make a price nobody filled in the cheapest thing in your catalog.

A policy may name a policy. policy:<name> is expanded at load, cycles refuse to load and name the loop, and the router only ever walks a chain with no policy: left in it.

Rule

A rule maps a call's metadata to a policy. It is the half a policy cannot express: a chain says where to look and can never say which calls it is for.

memql
/// An operator's agent reply reasons.
@when(prompt="agentReply", role="operator")
@level("reasoning")
@policy("localFirst")
@precedence(60)
@onUnavailable("degrade")
rule operatorReasoning { }

@when takes a closed key set. Every key is optional and all present keys are ANDed:

KeyMatches
levelthe level the call declared
modalitythe derived modality
promptthe DSL prompt name
rolethe agent's role slug
actorRolethe calling person's cluster role
taga call tag, such as background
touchesa concept-id prefix the call's footprint matches

role and actorRole are different questions on purpose. An operator watching a non-operator agent work is not an operator turn, and a rule that could not tell them apart would route on who is watching rather than on what is acting.

The other annotations:

  • @policy -- required. The chain this rule selects.
  • @level -- optional. Overrides the level the call declared. Both the declared and the effective level land on the decision record.
  • @precedence(N) -- highest first. A tie is a load error: a tie is resolved by nothing, so the rule that wins would differ between replicas.
  • @onUnavailable("degrade" | "park") -- what happens when the chain is exhausted. Unset means degrade.
  • @exclude("fleet:<modelId>") -- repeatable. Removes one concrete model from this rule's resolution.
  • @locked -- embedded tree only. See below.

The first matching rule wins. A call that matches nothing is impossible: the shipped default rule states no conditions, so it matches everything, and it cannot be removed.


Degrade or park

When the chain is exhausted at the requested level, the winning rule decides.

  • degrade walks the chain again one level down -- reasoning to strong to fast -- and records servedLevel and degraded: true.
  • park returns the refusal with the door report. A work run parks; an interactive surface shows the sentence.

fast is the floor and does not degrade. embeddings never degrades, at any setting: a degraded embedder does not return a worse vector, it returns one in a different vector space, so it does not belong in the index it is about to be written to and every later similarity read comes back plausible and wrong.

No degradation is silent. The decision record and the run's step both say what actually served.


The shipped rules

The general ones, all @locked. The rest of dsl/rules/rules.memql pins particular prompts (the rule compiler, document research) to local models.

RuleConditionLevelPolicyExhausted
defaultnone -- matches everything--localFirstdegrade
fastLanelevel="fast"--fastLocalFirstdegrade
backgroundLanetag="background"--localFirstdegrade
backgroundEscalationtag="backgroundEscalation"stronglocalFirstdegrade
operatorReasoningprompt="agentReply", role="operator"reasoninglocalFirstdegrade
reasoningParkslevel="reasoning"--federationStrongestpark
pipelineLocalFirstlevel="reasoning", tag="pipeline"--localFirstStrongestpark
embeddingsBoundlevel="embeddings"--embeddingsBindingpark
compilerLocalOnlyprompt="compileRule"--localOnlypark

operatorReasoning degrades while reasoningParks parks, and the difference is worth learning. A person is waiting on an agent reply and would rather have a smaller model's answer than a card telling them to open a laptop. A call that declared reasoning -- emitting a construct, re-planning a run -- would rather park: a degraded reasoning call does not return a worse answer, it returns a confident answer produced by a model that could not do the work, and nothing downstream can tell the difference.

Pipeline work runs local first

A pipeline's AI -- the independent PR review, a failed run's diagnosis, the repository change a diagnosis requests -- carries the pipeline tag: the review because the pipeline runner makes the call, the others because their work goal arrived through a pipeline (requestedVia: "pipeline", read off the goal by the replica doing the work, a planner's replan of such a run included). pipelineLocalFirst outranks reasoningParks for those calls and names localFirstStrongest: the strongest local model that can hold the call, then a signed-in app, then the strongest vendor, in that order. federationStrongest has the same entries but is one of the chains Auto ranks, and at reasoning Auto puts an app above a local model.

The context floor still decides the local door per call. A review renders at most 1 MiB, which is a floor of 266,240 tokens: no 262,144-token local model holds it, so a full-size review goes straight to the app, and a small one runs locally. When nothing can serve the call the work parks.

What @locked means

Three things, and all three are enforced:

  1. A locked rule evaluates before every unlocked rule, regardless of precedence. You may add rules and give them any precedence; they do not quietly become these.
  2. Locked definitions are re-read from the embedded tree on every boot, so nothing done to one at runtime survives a restart.
  3. A runtime-authored rule may not carry @locked and may not take a shipped name.

Adding your own rule

Two ways, and both produce the same thing.

In the DSL, in a product bundle mounted at MEMQL_DSL_PATH: write a rule construct with a precedence above the shipped one you want to win against.

At runtime, through routingRuleActivate, which takes the structured form (conditions, policy, precedence, on-unavailable, excludes), renders the construct deterministically -- no model is involved -- runs the same gates the DSL path runs, and activates it live. Owner or developer.

Either way the rule is refused if it names a shipped rule or policy, carries @locked, or collides on precedence with another unlocked rule.

"I want a paid model for everything"

Write one rule above the default:

memql
@when()
@policy("myVendorFirst")
@precedence(500)
rule alwaysVendor { }

with a policy whose chain starts at that vendor. It is explicit, it is named on every decision record, and the cost ceiling still governs it.


Routes a person picks in Ask

A route decides which source does each job for one message. Ask offers four kinds, and the turn carries the choice as its source, route:<id>:

RouteQuick tasks (fast)Main work (strong)Deep reasoning (reasoning)
Auto (route:auto, or no source)the rules decide, rankedthe rules decide, rankedthe rules decide, ranked
Local (route:local)fleet:fastestfleet:strongestfleet:strongest
Apps (route:apps)app:*app:*app:*
Your own (route:<id>)your sourcesyour sourcesyour sources

Search and memory is never a route's. Every route serves embeddings from the cluster's bound embedder: the index holds vectors from exactly one model. Vision, speech, transcription and image generation follow the rules in every route.

A route never falls through. A call made under Local, Apps or one of your own walks that route's sources for its level and nothing else. When they are exhausted it degrades inside the route (reasoning to strong to fast, through the route's own lists) and then refuses with every_door_shut, naming the route. It never reaches a rule's chain or cloud consent. The decision record carries the route in route.

Local and Apps can be changed for the whole cluster by an owner or a developer: each job holds one to four sources (fleet:, app: or federation:). The change is an override in the routing document, beside the policy and rule overrides and under the same revision; Restore default removes it. Your own routes are v1:router:route rows: named by you, private to you, made from a copy of Local, Apps or nothing.

A turn naming a route you do not have is refused with route_policy_unknown; one whose route cannot serve every job for you right now is refused with route_not_ready and the reason ("Needs an embedding model", "No signed-in app on your machines"), before any goal exists.

App selection on a machine

Fleet > Machines > your machine > Apps owns the app strategy. Ask keeps its Route and Where controls; it does not carry an app-strategy override.

  • Auto (default) balances observed token usage, response time and successful calls. Usage balance, Speed and Reliability weights are separate from the general routing Auto balance below.
  • Round robin alternates available apps for new calls.
  • Claude Code only and Codex only use that app exclusively, including when it is unavailable.

The usage score uses up to 200 recent owner-scoped app sessions from the last two hours. Unknown usage stays unknown; token counts are not remaining-credit estimates. Availability is tracked separately for each app on each owned machine, and only an app its machine has found ready is used. A quota refusal stays effective until a check on that machine finds the app working again; a reset passing does not send a real call to find out. This evidence survives restarts and replica changes and does not disappear as other sessions enter the usage window. Auto and Round robin can use another enabled app; an Only strategy cannot.

After purchasing credits or signing in, there is nothing to press. The machine keeps checking, within five minutes for Codex and fifteen for Claude Code, and the app comes back when a check finds it working. A Cockpit that predates those checks reports none, so its refusal lasts until the reset the app named, or fifteen minutes when it named none, and the next call is what finds out. A later quota refusal blocks new calls again, and a successful call clears it. Quota evidence from older builds is honored while present in recent history; newly recorded refusals have a durable marker independent of that history.

Each active session stays with its app. New calls receive their full supplied conversation and contract, and delegated steps receive their inputs and workspace context. A fallback repeats that same handover only after a refusal proven to precede any work: no served model, no result, no tokens, and no recorded or dropped actions. An app that runs out of usage or loses its login partway hands the step to the next ready app with a record of what already ran, so the next app carries on rather than starting again (local apps). An uncertain interruption is never automatically replayed. The existing per-level model mapping remains in the app harness, and records name the model actually reported by the app.

Selecting Apps with Where Auto permits an enabled app session on the owner's machine. It does not authorize general computer tools or override an explicit Workbench destination. Ownership, app permissions, kill switches and active-run checks remain mandatory.

Auto

Auto keeps the rules. What it adds is the order inside the three general shipped chains -- localFirst, fastLocalFirst and federationStrongest -- which it ranks for each call on four signals:

  • Speed: the median latency each kind of source served this level with, over the last two hours of decisions.
  • Reliability: the share of those calls that succeeded.
  • Cost: zero for your own machines and subscription apps, the whole scale for a metered vendor.
  • Quality: a fixed, level-aware rank -- at quick tasks every source is nearly equal; at reasoning a strong vendor model, then an app, then a local model.

An unmeasured signal is neutral, and ties keep the chain's own local-first order, so a cluster nobody has measured routes as it always did. The signals are weighed by the cluster's Auto balance -- Speed, Cost and Quality, 0 to 100 each, 50 each recommended -- and reliability always weighs their mean. A person's monthly budget (myBudget, myBudgetSave) bends the order: from 80% of it vendors go after every free source, and at 100% they are left out of that person's calls. A chain Auto reordered asks the cost ceiling before every vendor hop. The decision record keeps one (auto) entry in considered with every score.

Builtins: routesForActor, routeSave, routeDelete, routeReset, autoBalance, autoBalanceSave, autoBalanceReset, myBudget and myBudgetSave.

Fleet capacity

Under every route, a fleet source admits a machine only when it can take the call now. A machine without room is ruled out with the figures in considered. Room is its free memory plus what idle models other than this one hold, and the call wants what it still has to load plus a reserve: a sixteenth of the pool, between 1 and 4 GB. Where models share memory with a machine's workspaces, its free memory leaves out the 3 GB kept for them. What it has to load comes from the catalog's memoryNeedBytes or the estimate from its sizeBytes, whichever is larger, with 2 GB more for each 32K of context above 32K, less what is already loaded. A machine with every slot in use, or one whose recent calls mostly failed or ran over three times slower than its peers, goes behind every machine that passes and is used only when none does. Memory is judged from the cockpit's capacity report (free memory and loaded models, on every heartbeat); a machine that does not report is admitted on its slots alone. fleetCapacity returns what each of your machines can take on: slots in use, memory, loaded models, recent latency and tokens per second, whether it is degraded, and for each model the room it wants and whether it fits.

The machine has the last word, and it runs the same sum on the same reading of free memory. Its cockpit checks its own memory before handing the call to a runtime, and a machine that cannot take the call ends it with insufficient_memory having run nothing. That now happens mainly when memory has moved since the last heartbeat. The call then goes to the next machine holding the model, and when there is none, to the route's next source. No other failure moves a call to another machine: one a runtime was handed may have generated, and running it again would be the same generation twice.

Explicit API choices and automatic Ask routing

Ask uses the cluster's routing policies automatically for each step. It has no conversation-wide source picker. API callers may still supply an explicit source and level on a turn (AiChatMsg.provider and AiChatMsg.level). Those explicit choices apply to every model call of the turn's goal on every node -- the planner's triage and compile as much as the agent's reply.

SourceMeans
emptyAuto: the rules decide, exactly as without a choice
app:claude-code, app:codex, app:*pin that app on the owner's machine
fleet:strongest, fleet:fastest, fleet:<modelId>pin a local model
federation:cheapest, federation:strongestpin a vendor
policy:<name>a route: that policy's chain is walked instead of the rule's
route:local, route:apps, route:<id>a person's route: its sources per level, never falling through (above)

A pin is the owner's own and is never substituted. It is walked as a one-entry chain, it skips every rule, and when it cannot serve the call refuses with every_door_shut naming what was pinned ([pinned to app:claude-code: a pinned source is never substituted]). Because the pin is the owner's, an app door reached through it passes the app gate on their machine.

A route fails over. Its chain is walked entry by entry like a rule's -- so a planner that cannot reach an app door yet answers from the entry behind it. A route removed after the turn chose it is a plain error, not a park: no machine waking would ever serve it.

The level binds the run's steps -- the reply, a compose. Calls made while compiling (triage, the compile pass) and the failure classifier run outside any step and keep the level they declare, while still going where the source says. Embeddings, vision and audio are never re-routed by a choice.

A person's override for one step (re-run or branch) is more specific than their explicit API choice for the goal, so its model and level each win over it.

Where it lives. The choice is written onto v1:work:goal.routing and v1:work:run.routing ({source, level, by}, by stamped by the server) -- never into the goal's input, which is what the model reads. The planner's compile claim and the agent's dispatch each build their run context from the run ROW, and the router applies the choice to every request it resolves (router.ResolveFor -> memql.ApplyRunRouting), so no node needs anything from the one that took the turn. A fork, a replay and a branch keep their source run's choice; a goal a run's step opens (a compose, agent()) inherits it for the same owner.

Refusals before any goal exists. A word outside the table, or a level other than empty / fast / strong / reasoning, refuses the turn with gRPC InvalidArgument and metadata code = route_source_invalid, route_level_invalid or route_policy_unknown (an unknown route), field = source or level.


Reading what the router decided

Every resolution writes one v1:router:call row, on success and on a park. Beyond the tokens, cost and latency the row already carried, a decision carries:

FieldSays
level / servedLevel / degradedwhat was asked for, what served, and whether it was a step down
rulewhich rule matched
policywhich chain it named
doorlocal, app, federation or session
servedModel / servedEffortwhat the SURFACE reported serving it with, empty when it said nothing
consideredevery entry the walk passed over, its door, and why -- kept on success as well as on a refusal
touchesthe call's footprint
minContextTokensthe context floor this resolution was made against
callerKindwhat KIND of caller made the call: user, system, connector, anonymous or unattributed
cacheKindwhich CACHE answered, when one did: exact or semantic. Absent means a provider answered
executionSurfacewhich machine took the call, as fleet:<registrationId>, whether it served it or failed it. outcome says which, and a failed call counts against that machine in fleet capacity, including one that ran out of its caller's time. A machine that declined before handing the call to a runtime, or that nothing had been heard from when its caller gave up, is neither named nor counted

session is a fourth door, not a flag on app. app means a subscription app answered this TURN; session means one was handed the whole STEP and drove its own loop (design D7 of the app-session record). They are separate values because this field is what you filter on, and folding them would make the entire history of a cluster whose only open door is a signed-in Claude Code read as ordinary chat turns.

callerKind is what makes an empty userId an ANSWER. A blank user used to say two very different things at once -- "a person made this call and nobody wrote down which one" and "no person made this call at all, it was a boot seed or a retention sweep" -- so every unattributed row had to be read as a possible gap. system is the cluster acting and correctly has no user; unattributed is a Go call site that established no caller at all, which is the one honest answer for a context nobody stamped. It is DERIVED from the context where the request is built, never supplied by a caller, and nothing routes on it: no @when key reads it, because a rule that did would turn evidence into an input.

cacheKind is how you tell a cache hit from a provider call. The exact-hash response cache is consulted AFTER the router resolves -- its key folds in the resolved provider name, which is what invalidates a cached answer when routing changes -- so a hit is a call that matched a rule, walked a chain and took a door without reaching a provider. Such a row names its cache and carries a REAL ZERO for every token and every cost: nothing was sent, nothing was billed, and tokensEstimated / pricingConfigured are false because nothing was estimated and no cost was computed. Absent means a provider answered, which is what every row written before this was. The whole decision travels with it, considered included -- the calls a cache serves are by definition the ones that repeat, so dropping their records would make them the decisions the rule corpus can never be checked against. Before this a warm page wrote no rows at all and was indistinguishable from an idle cluster.

servedModel is a REPORT, never the request. model is what the chain resolved -- for an app door that is the door's own name, claude-code -- and what actually ran is the app's to state. Both fields are EMPTY when the surface said nothing, which reads as unknown: a value copied from the request would record as measured something nobody measured, in the one case anybody would want to look at. Claude Code's headless output states no effort at all, so an empty servedEffort is the ordinary case.

Read them with routerDecisionsRecent, filtering by since, level, door, rule and outcome. Floored at admin, which on this ladder admits admin, developer and owner: a decision record carries no prompt content and no error message -- both are deliberately left out of the projection -- and an admin answering "why did this go to a vendor" needs it.

considered being kept on success is the part that makes a rule falsifiable. What a chain did not pick is half the decision, and before this the report was accumulated and then dropped the moment an entry won.

These rows are never broadcast -- one row per model call is the same volume argument that excludes v1:worker:invocation from the live feeds. They are written through ONE bounded queue drained by ONE writer, so the ledger costs a node one goroutine and at most one concurrent write however many calls are in flight; a full queue drops the record and counts it on RecordsDropped, because a node whose database cannot keep up with its own model calls must not grow its heap instead.


What is still in Go, and why

A kill switch a policy can author around is not a kill switch. These stay in code, outside the rule system, and no rule can reach them:

  • the process-wide LLM rate ceiling, the identical-request circuit breaker and the cumulative spend kill switch (component/memql/ai_guard.go, mirrored for fleet and app calls by ai_guard_fleet.go against the same shared accounting);
  • the run's token and cost ceilings (component/work/budget.go);
  • door classification, provider availability, and the refusal codes.

See LLM cost control for the full defence-in-depth picture.


When a call cannot be served

The refusal names every door and why each did not open, because "no provider available" is a sentence with no action in it -- the fixes live in different places and which one applies is exactly what the door list says.

  • every_door_shut -- the chain was exhausted. A condition the world changes: a lid opens, somebody signs in, a model finishes pulling. Worth re-checking.
  • ceiling_reached -- a paid provider could have served the call and the cost ceiling is reached. A condition only a person changes. Worth asking about once.

The local door's report goes further than the door: it names the machines it considered and why each was ruled out, because "your fleet is unavailable" sends you nowhere while "laptop: offline; desktop: does not offer qwen3.8:27b" names the laptop to open and the model to pull.


Fleet History displays inference cost estimates with model/source evidence separately from billing. Local calls need an owner-confirmed tariff and measured or explicitly estimated energy; unavailable electricity evidence is not interpreted as zero cost.