createRouter handles request normalization, eligibility, selection, execution, protocol serialization, and optional fallback. Your host remains responsible for credentials, deployment, durable state, and application telemetry.
Leases
A policy can commit a model for later turns:cacheKey (OpenAI or Anthropic prompt_cache_key). The default in-memory store expires leases after 30 minutes. createRouter calls pruneExpired() before each cacheKey-bearing selection, and every served turn refreshes the TTL, so leases serve while they still have turns and have not expired. router.select() and router.fetch() honor leases; router.evaluatePolicy() does not.
A selection lease and a provider prompt cache are related but different: the lease keeps routing on one model, while the provider decides whether the prompt prefix is cached. See Prompt Caching for the complete responsibility boundary.
For replicas or restarts, provide a LeaseStore backed by your database or cache:
Hooks
Hooks are fire-and-forget and may return promises. A throwing or rejecting hook does not change the response:onSelection observes the selected decision. Completion and error hooks identify the model that actually served, including a fallback.
Fallback
Enable one cross-model retry at the router boundary:IDs and deployment
UsegenerateId when tests or application tracing require deterministic response IDs. Deploy the fetch handler behind your web server or Bun’s server. The router itself 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/retry orchestration, use the lower-level /policy-engine phases described in Decision Pipeline, Prompt Caching, and Speculative Routing.