createRouter handles request normalization, eligibility, selection, execution, protocol serialization, and optional fallback. Your host handles credentials, deployment, durable state, and telemetry.
Leases
A policy can commit a model for later turns:- A lease applies only when the request has a
cacheKey(OpenAI or Anthropicprompt_cache_key). - Leases count conversation turns. A turn is a completed assistant reply in the request’s history, including tool-call continuations. Retries do not use a turn.
- A lease committed at turn
nwithleaseTurnsRemaining: 3serves turnsnthroughn + 3. - An edited history that is shorter than before ends the lease.
router.select()androuter.fetch()honor leases.router.evaluatePolicy()does not.
LeaseStore backed by your database or cache:
A lease and a provider prompt cache are different. The lease keeps routing on one model. The provider decides whether a prompt prefix is cached. See Prompt Caching.
Hooks
Hooks are fire-and-forget. They may return promises. A throwing or rejecting hook does not change the response.onSelection receives the selected decision. Completion and error hooks report the model that actually served the request, including after a fallback.
Fallback
Fallback is off by default. Enable one cross-model retry at the router:- The router tries one other eligible candidate when the primary fails before any output reaches the client. That covers a failed start, or a stream that fails before its first event.
- After output has been sent, a failure ends the stream with an error.
- Same-model retries and provider error classification belong to the executor.
- A failed leased primary is released before the next turn.
Speculative Routing
Start the provider call without waiting for the policy:- For a request with a
cacheKey, the router serves the conversation’s previous decision immediately and runs the policy in the background. - The policy’s answer serves the next turn.
onSpeculativeSelectionreports it. - The first turn, and any turn whose previous model cannot serve the request, waits for the policy.
- Leases take precedence. A failed background selection keeps the previous model.
- A failed speculative primary is released before the next turn.
- The previous decision is stored in the lease store under
speculation:<cacheKey>.
Lease Prefetch
WithleasePrefetchTurns: n, the router selects a lease’s successor in the background once the lease has n turns left. The first turn after the lease ends serves that successor and its lease, without waiting for the policy. The successor is stored under pending:<cacheKey>.
Auxiliary Requests
auxiliary serves background work, such as titles or summaries, outside conversation routing:
Pinned Models And Sessions
- A request with the model
dari/model/<id>is served by that model at the requested reasoning level, or the model’s default. It skips the policy, leases, and speculation. - If the router cannot serve it, the request fails with
model_not_enabled,model_unavailable, orthinking_level_unavailable. - With
strict: true, the router serves only the named model and rejects other requests withstrict_model_required. sessionPinKeymaps a request to a session. A session’s first request runs the policy. Later requests keep that model and reasoning level, skipping leases and speculation. Pins are stored underpin:<key>.
Decision Sources
Every selection reports where it came from asselection.source: policy, explicit_model, strict_model, auxiliary, session_pin, lease_hold, prefetched_lease, speculative, or fallback. Hooks receive it for telemetry.
Sources are checked in this order: named model, auxiliary, session pin, lease, prefetched lease, speculation, policy.
IDs And Deployment
- Use
generateIdwhen tests or tracing need deterministic response IDs. - Deploy the
fetchhandler behind your web server, or with Bun’s server. - The router does not persist conversations or provider-cache observations.
When To Leave createRouter
createRouter is selector-first. If you need durable routing state, speculative execution, cache-aware cost estimates, or custom billing and retry orchestration, use the lower-level /policy-engine phases. See Decision Pipeline, Prompt Caching, and Speculative Routing.