createRouter() and a routing policy. Use the lower-level API when you need to inspect routing evidence, perform the selector call yourself, or persist state between phases.
Prepare And Finalize
prepareRoute(input) does candidate compatibility, cost estimation, decision recovery, strategy pruning, eval matching, and selector-request construction. Then finalizeRoute() parses and validates the selector’s answer.
RouteInput fields
RouteInput fields
- candidateModels — model IDs to consider
- metadataLookup — resolves model metadata
- pricing — returns model/cache rates or null
- messages — the conversation
- chainsByModel — prefix hashes per model
- prefixHits — stored provider-cache observations
- nowMs — timestamp for cache-warmth checks
- toolChoiceFp / responseFormatFp — prevent incompatible cache reuse
- selectorModel / selectorContextWindowChars — selector config
- modelFallbackEnabled / fallbackRequiresDifferentProvider — optional fallback
route(input, selector) is the convenience composition when you don’t need to interleave work.
Inspect Prepared Evidence
The prepared result exposes:- Compatible model/reasoning pairs
- Cache-aware cost estimates (routing evidence, not billing)
- Candidates removed by pruning
- Recovered previous decision and conversation identity
- Warnings from best-effort cost estimation
- The exact selector request
Selector Output
The selector returns JSON withselected_model, reasoning_effort, and reason. finalizeRoute() validates the selected pair appeared in the prepared candidates.
The pipeline can also request a fallback. modelFallbackEnabled controls whether one is required, fallbackRequiresDifferentProvider controls provider diversity. Fallback execution is host-owned.
Cache State
Provider caches make model continuity cheaper than switching. Supply:- chainsByModel — ordered prefix hashes per model
- prefixHits — stored provider-cache observations
- nowMs — determines if an observation is still warm
- toolChoiceFp / responseFormatFp — prevent incompatible cache reuse
prefixChain() creates Chat Completions hashes; optionsFingerprints() creates the expected fingerprint values.
Anonymous Selection
For training or evaluation, prevent the selector from memorizing model names:[0, 1) — pass a seeded PRNG to reproduce recorded permutations.
Intended for policy training, not production routing.
Speculative Execution
BecauseprepareRoute and finalizeRoute are separate, the host can start the compatible previous model while the selector runs. See the Speculative Routing guide for eligibility and failure semantics.
A complete no-network example is at examples/basic_route.ts — run it with bun run example:decision-pipeline.