Skip to main content
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 Anthropic prompt_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 n with leaseTurnsRemaining: 3 serves turns n through n + 3.
  • An edited history that is shorter than before ends the lease.
  • router.select() and router.fetch() honor leases. router.evaluatePolicy() does not.
The default in-memory store expires a lease 30 minutes after its last served turn. For replicas or restarts, provide a LeaseStore backed by your database or cache:
The router reads the turn from each request, so the store needs no read-modify-write, and lease position survives restarts and other replicas. Store failures are advisory. A failed store call does not fail the request, but a lease may not be honored or saved.
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. onSpeculativeSelection reports 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

With leasePrefetchTurns: 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:
A decision returned here is served without the policy, session pins, leases, or speculation. Nothing is recorded for later turns.

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, or thinking_level_unavailable.
  • With strict: true, the router serves only the named model and rejects other requests with strict_model_required.
  • sessionPinKey maps 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 under pin:<key>.

Decision Sources

Every selection reports where it came from as selection.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 generateId when tests or tracing need deterministic response IDs.
  • Deploy the fetch handler 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.