Skip to main content

Adaptive router

The adaptive router is for teams that would rather let real traffic decide which model handles which kind of request. For every request it classifies the latest user message into one of seven request types, then picks a model by Thompson sampling a Beta posterior kept per request type and model, scored as a weighted sum of estimated quality and normalised cost. After each response it reads the conversation for signs of satisfaction or trouble and credits or blames the model that served the turn, so the posteriors move with real traffic. Nothing is sticky, every call resamples. It is configured as a model_list entry whose model is auto_router/adaptive_router and is part of the free gateway. A gateway without a license may register one auto router of each kind, and a license with the auto_router feature allows several adaptive routers with different names.

A real-world scenario​

An internal developer assistant gets code generation, code explanation and the odd writing request. The team is not sure whether claude-haiku-5-5 is good enough for code, so instead of guessing it creates an adaptive router smart over claude-sonnet-5-5 and claude-haiku-5-5 with a 0.7 quality and 0.3 cost weighting. After a week the Adaptive Router tab of Router Settings shows Sonnet winning code generation with an 86 percent estimated quality from 34 samples while Haiku holds factual lookups, and the router now sends each request type to the model the users were happiest with.

Step by step​

In the Admin UI​

  1. Open /ui/models-and-endpoints as a proxy admin. On each candidate deployment's detail view add adaptive_router_preferences with quality_tier (1 to 3) and optional strengths under Model Info, then save.
  2. Add the router in config.yaml as shown below and restart the gateway, because the Add Auto Router dialog only builds complexity routers. The router appears on the Auto-Routers tab with Type Adaptive.
  3. Give the keys or teams that should use it access to the model smart.
  4. Open /ui/router-settings and pick the Adaptive Router tab. Each router gets a section with its quality and cost weights, the pending update count and a table of Request type, Model, Samples and Estimated quality. Click Refresh to reload it.

With curl​

model_list:
- model_name: claude-sonnet-5-5
spanway_params:
model: anthropic/claude-sonnet-5-5
api_key: os.environ/ANTHROPIC_API_KEY
model_info:
input_cost_per_token: 0.000003
adaptive_router_preferences:
quality_tier: 3
strengths: ["code_generation", "analytical_reasoning"]
- model_name: claude-haiku-5-5
spanway_params:
model: anthropic/claude-haiku-5-5
api_key: os.environ/ANTHROPIC_API_KEY
model_info:
input_cost_per_token: 0.0000008
adaptive_router_preferences:
quality_tier: 2
strengths: ["general", "factual_lookup"]
- model_name: smart
spanway_params:
model: auto_router/adaptive_router
adaptive_router_config:
available_models: ["claude-sonnet-5-5", "claude-haiku-5-5"]
weights:
quality: 0.7
cost: 0.3

Send a request, then read the learned state.

curl -i -X POST http://localhost:4000/v1/chat/completions \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-H "x-spanway-min-quality-tier: 2" \
-d '{"model": "smart", "messages": [{"role": "user", "content": "Write a function that parses ISO dates"}]}'

curl http://localhost:4000/adaptive_router/state -H "Authorization: Bearer $SPANWAY_MASTER_KEY"

Expected result​

The chat completion carries x-spanway-adaptive-router-model with the model the router sampled, and the spend log row records the router, the request type and that model at its own price. See routing decision headers for every header the routers set. The state call answers with one snapshot per adaptive router.

{
"routers": [
{
"router_name": "smart",
"available_models": ["claude-sonnet-5-5", "claude-haiku-5-5"],
"weights": {"quality": 0.7, "cost": 0.3},
"model_costs": {"claude-sonnet-5-5": 1.0, "claude-haiku-5-5": 0.27},
"cells": [
{"request_type": "code_generation", "model": "claude-sonnet-5-5", "alpha": 38.0, "beta": 6.0, "samples": 34, "quality_mean": 0.86}
],
"feedback_contexts_live": 12,
"feedback_attributed_total": 418,
"feedback_without_context_total": 3,
"cross_model_feedback_total": 27,
"response_signal_updates_total": 102,
"queue": 4
}
]
}

samples counts real observations, the prior excluded, and quality_mean is the estimated quality the Admin UI shows as a percentage. The numbers come from the gateway replica that answered, and the state never holds prompt or response text.

Settings​

SettingMeaning
adaptive_router_config.available_modelsModel groups the router may choose from. Each must exist in model_list.
adaptive_router_config.weightsquality and cost weights of the score, 0.7 and 0.3 by default.
maximum_adaptive_router_session_retention_periodA general_settings duration such as 30d. Session rows with no activity within it are deleted. Unset keeps them.
model_info.adaptive_router_preferences.quality_tier1 to 3. Sets the cold start prior, a mean of 0.3, 0.5 or 0.7.
model_info.adaptive_router_preferences.strengthsRequest types this model is declared good at, adding 0.3 to the prior for those cells.

Request types are code_generation, code_understanding, technical_design, analytical_reasoning, writing, factual_lookup and general, classified by deterministic rules over the user message. A caller can send the x-spanway-min-quality-tier header, or min_quality_tier in metadata, to restrict the choice to models at that tier or above. When no candidate meets the tier, the request fails, there is no default model to fall back to.

Learning​

Each cell starts with a prior of total mass 10, so about ten real observations move it. Conversation identity is the client supplied session id when present, otherwise a hash of the caller identity and the first message, and in-memory contexts expire after 24 hours. The session rows in the database stay until maximum_adaptive_router_session_retention_period removes them. A satisfied turn adds to alpha. Misalignment, stagnation, disengagement and failure each add to beta, a detected loop adds half, and exhaustion signals are treated as uptime rather than quality. Feedback in the current user message is attributed to the model that produced the previous response, while signals in the response are attributed to the model that produced it. Updates are skipped on conversations with fewer than four messages, and a cell stops accepting deltas once alpha plus beta reaches 200. Updates are aggregated in memory and flushed to the database by a background task every ten seconds, so state survives restarts and is shared across instances. The per-session rows in Spanway_AdaptiveRouterSession hold only the signal counts, turn counts and the last response status for each session and model. The message text and tool call history the signals are detected from stay in memory and are never written to the database.

Latency is not part of the score, and signals are regex and tool call based, so a slow model can still win and the detectors are biased towards English prompts.

MethodPathSummary
GET/adaptive_router/stateRead what the adaptive routers have learned

Errors​

What you seeWhyFix
The gateway fails at start up with adaptive_router deployment smart with tags [] already existsTwo adaptive routers use the same model name and tags.Give each router its own name or tags.
403 At most 1 auto-router(s) ... A Spanway license with the 'auto_router' feature lifts the limit. when saving a second routerThe gateway has no license with the auto_router feature.Load such a license or keep one router per kind.
The router never picks one of its modelsAn available_models entry is not in model_list, and the router skips it without an error.Add the model to model_list or remove it from available_models.
500 AdaptiveRouter[smart]: no models meet min_quality_tier=3The x-spanway-min-quality-tier header or min_quality_tier metadata is higher than every candidate's quality_tier.Send a lower tier or add a model with that tier.
404 No adaptive_router is configured on this proxy.The state route found no adaptive router.Add the router and restart.
403 on /adaptive_router/stateThe key is not a proxy admin or admin viewer.Use an admin key.
Every cell stays at its priorConversations are shorter than four messages, or no session id is sent so contexts cannot be matched.Send a client session id and expect learning only on multi-turn conversations.
The Admin UI tab shows different numbers on each reloadReplicas answer in turn and each holds its own in-memory posteriors until the ten second flush.Compare after the flush, or read one replica directly.
400 Invalid model name passed in model=smartThe key or team may not call the router.Add smart to its models.