Auto router API
This page documents the 6 gateway endpoints tagged auto router in the gateway's OpenAPI document. Every example targets a gateway on http://localhost:4000 and authenticates with the SPANWAY_API_KEY environment variable, which holds the master key or a virtual key that has access to the route. Request and response fields are taken from the schema the running gateway publishes at /openapi.json, and the same document is bundled with this site as openapi.json.
| Method | Path | Summary |
|---|---|---|
| GET | /auto_router/benchmarks | Auto router usage and savings over a date range |
| GET | /auto_router/session | Spend and last model of one auto routed session |
| POST | /auto_router/shadow_eval/start | Start a shadow eval of an auto router |
| GET | /auto_router/shadow_eval | List shadow eval jobs |
| GET | /auto_router/shadow_eval/{job_id} | Read one shadow eval job with its results |
| POST | /auto_router/shadow_eval/{job_id}/stop | Stop a running shadow eval |
Endpoints
GET /auto_router/benchmarks
Auto router usage and savings over a date range
How much the auto routers saved and how they behaved over a date range, for the Auto-Router tab of the Cost Optimization page. Proxy admins and admin viewers only.
The answer has the savings against each router's baseline model, how long sessions ran and how well prompt
caching worked for each kind of routing decision. Money counts only requests on the chosen UTC days, and the
savings total matches the Overall tab. Session shape and caching cover every session that overlaps the range.
Send api_key (a key's token hash) or user_id to see one key or one internal user, and note that turns
recorded before user attribution existed only show up under their key. Every configured router appears, with
zeros until it serves traffic, and routers_in_scope counts those too. The data comes from a rollup written
as each request is billed, so the route stays fast on a busy gateway.
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
start_date | query | string or null | No | YYYY-MM-DD UTC, inclusive (defaults to 30 days before end_date) |
end_date | query | string or null | No | YYYY-MM-DD UTC, inclusive (defaults to today) |
api_key | query | string or null | No | Filter to one virtual key token hash |
user_id | query | string or null | No | Filter to one canonical internal user recorded on each turn |
Example request:
curl -X GET "http://localhost:4000/auto_router/benchmarks" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | Totals for the window and one group per configured auto router. | application/json AutoRouterBenchmarksResponse |
| 400 | end_date must not be earlier than start_date or Invalid date format: 2026-13-01. Expected: 'YYYY-MM-DD' | application/json ErrorDetail |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 403 | Only proxy admin roles can view auto-router benchmarks across the deployment | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | DB not connected. This endpoint needs a database; set DATABASE_URL to a PostgreSQL connection string (postgresql://...) to enable it. See https://docs.spanway.ai/docs/proxy/virtual_keys | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
start_date | string | Yes | Window start day, YYYY-MM-DD UTC, inclusive |
end_date | string | Yes | Window end day, YYYY-MM-DD UTC, inclusive |
routers_in_scope | integer | Yes | How many groups this response carries. Every auto-router configured on the proxy counts, whether or not it served anything in the window. To count only the routers that did serve traffic, filter groups to the entries w |
totals | AutoRouterBenchmarkTotals | Yes | Auto-routed traffic in the window. Turns, spend and savings count requests on the selected UTC days; |
groups | array of AutoRouterBenchmarkGroup | Yes | One entry per auto-router, listed from the model registry rather than from the rollup, so a router appears as soon as it is configured and reads zero until it serves traffic. Semantic auto-routers are absent: they record |
GET /auto_router/session
Spend and last model of one auto routed session
One auto-routed session, for the key that ran it: the model its last turn was routed to and the session's spend against the router's savings baseline. Built for a coding agent's status line or stop hook, so any virtual key may call it and only ever sees rows written under its own key hash. Reads the Spanway_AutoRouterSession rollup, which the asynchronous spend flush fills a moment after each turn; a session with no flushed auto-routed turn yet is a 404. The id is bounded the way the writer bounded it, so an oversized client id still finds its row.
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
session_id | query | string | Yes | The client session id (x-*-session-id header) the turns were sent under |
Example request:
curl -X GET "http://localhost:4000/auto_router/session?session_id=session_id-123" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The session rollup for the calling key. | application/json AutoRouterSessionResponse |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 404 | No auto-routed turns recorded for session 'claude-code-4f1a' under this key | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | DB not connected. This endpoint needs a database; set DATABASE_URL to a PostgreSQL connection string (postgresql://...) to enable it. See https://docs.spanway.ai/docs/proxy/virtual_keys | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
session_id | string | Yes | Session Id |
router_name | string | Yes | The auto-router alias the session's requests were sent to |
router_type | string | Yes | complexity, adaptive or quality |
turns | integer | Yes | Auto-routed turns the rollup has recorded for this session so far |
last_model | string | Yes | The deployment model the most recent turn was routed to |
spend | number | Yes | What the session's routed traffic actually cost, classifier calls included |
savings_estimated_turns | integer | Yes | Requests whose savings estimate recorded its baseline cost |
savings_estimated_actual_spend | number | Yes | Actual spend, including classifier cost, for requests whose estimate recorded its baseline cost |
saved_spend | number or null | Yes | Recorded historical savings plus newer estimates, net of classifier cost |
baseline_spend | number or null | Yes | Estimated single-model cost: spend plus recorded savings |
savings_estimated_baseline_spend | number or null | Yes | Estimated single-model cost for requests whose estimate recorded its baseline cost |
baseline_model | string or null | Yes | The savings baseline recorded by most session turns, including historical turns, recorded turn by turn, so it still names the counterfactual after the router is reconfigured or removed. None when no turn recorded one: ro |
baseline_models | object | Yes | Session turns recording each baseline model; more than one entry means the router's baseline changed mid-session; these counts do not imply savings coverage |
POST /auto_router/shadow_eval/start
Start a shadow eval of an auto router
Start a shadow eval: duplicate a sampled slice of one or more targets' live traffic against a second arm, judge the two responses blind, and stratify win rates by tier, by the model that served the real arm, and by target.
A target is a virtual key, a team, or a user. Team and user targets match on the identity every request resolves to at auth time, so they cover JWT-authenticated traffic, which presents no virtual key; a user target samples that user's traffic across all their teams, whether it arrives on a JWT or a key they own. models narrows every target to requests for those model groups, so a user plus one model samples that user's traffic on that model across every key they own; it is forward-only, since a reverse job already samples exactly the traffic its own router served.
A forward job answers whether the targets should adopt router_name: it samples the requests the router did not serve and duplicates them through it. A reverse job answers whether a target already on the router still gains from it: it samples the requests the router did serve and duplicates them against baseline_model. A target can hold one active job per direction, so both questions can run at once, and a request matching several jobs' targets (say its key and its team) is sampled by each, separately budgeted.
Shadow responses are never served to users. Each target samples until its recorded eval spend, the shadow and judge calls' own cost, reaches max_budget dollars, the job's window ends, or the job is stopped, so one target running out of budget does not end sampling for the others; sampling changes propagate to pods within about 10 seconds. Shadow and judge calls bill to the sampled request's own identity but are excluded from request counts and auto-router adoption metrics.
Request body (application/json, schema StartShadowEvalRequest):
| Field | Type | Required | Description |
|---|---|---|---|
api_key_ids | array of string | No | Hashed virtual keys whose traffic will be shadowed. Combined with team_ids and user_ids the job needs at least one target and at most 100, which also bounds every read the job's endpoints make. Each target carries its ow |
team_ids | array of string | No | Teams whose traffic will be shadowed, matched on the team every authenticated request resolves to, so a team's JWT-auth and virtual-key traffic are both sampled |
user_ids | array of string | No | Users whose traffic will be shadowed, matched on the user every authenticated request resolves to across all their teams: JWT requests carrying their subject claim and virtual keys they own |
models | array of string | No | Model groups to narrow the sampled traffic to, matched on the group the caller requested and resolved through model_group_alias, so an alias and its target are one name. Empty samples every model the targets use. This AN |
router_name | string or null | No | The auto-router under evaluation, in either direction: the single-router spelling of router_names. Provide exactly one of the two fields |
router_names | array of string | No | The auto-routers under evaluation, at most 4. Every sampled request runs through every router listed and each arm is judged independently against the same real response, so routers compare head-to-head on identical traff |
direction | enum: "forward", "reverse" | No | forward answers 'should this key adopt router_name': it samples the requests the key did NOT route through the router and duplicates them through it. reverse answers 'is the router still worth it for a key already on it' |
baseline_model | string or null | No | Required when direction is reverse and rejected otherwise: the fixed model the router's own responses are judged against. Must be a plain model rather than another auto-router |
shadow_percentage | number | Yes | Percentage of each target's requests to duplicate through the router |
judge_model | string | No | Model used to blindly judge real vs. shadow responses. The judge only compares two answers, so a mid-tier model (Claude Sonnet or GPT-4o class) is the sweet spot: small/nano-class models produce unreliable or malformed v |
duration_days | integer | No | How many days the job samples traffic before completing on its own |
max_budget | number | No | Per-target USD budget for the eval's own overhead, the shadow-arm and judge calls, priced with the same figures the spend pipeline bills. EACH scoped target samples until its recorded eval spend reaches this, so a job ov |
Example request:
curl -X POST "http://localhost:4000/auto_router/shadow_eval/start" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"team_ids": [
"team-platform"
],
"router_names": [
"smart"
],
"direction": "forward",
"shadow_percentage": 10,
"judge_model": "claude-sonnet-5-5",
"duration_days": 7,
"max_budget": 25
}'
Responses:
| Status | Description | Body |
|---|---|---|
| 201 | The new job with its targets. | application/json ShadowEvalJobResponse |
| 400 | Not a configured auto-router: 'smart' or team_ids not on this proxy: team-unknown or judge_model 'smart' is an auto-router; it must be a plain model or judge_model 'claude-judge' is neither a model configured on this proxy nor a provider-qualified public model name (e.g. 'anthropic/claude-sonnet-5') or judge_model 'anthropic/claude-sonnet-5' uses the Spanway SDK but required credentials are not configured: ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN or models not served by this proxy: 'gpt-unknown' or judge_model 'claude-sonnet-5-5' is also an arm this job would judge: tier model 'claude-sonnet-5-5'. A judge scores its own answers higher than a rival's, so the win rates would measure the judge; pick a judge that serves neither arm | application/json ErrorDetail |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 403 | Only a proxy admin can start a shadow eval | application/json ErrorDetail |
| 409 | Already in an active forward shadow eval job: team team-platform (job 7c1f2a90-5e3b-4d8a-9b61-2f0e8c4a1d77). Stop it first. or A requested target was claimed by another forward shadow eval job concurrently. Stop it first. | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | DB not connected. This endpoint needs a database; set DATABASE_URL to a PostgreSQL connection string (postgresql://...) to enable it. See https://docs.spanway.ai/docs/proxy/virtual_keys | application/json ErrorDetail |
GET /auto_router/shadow_eval
List shadow eval jobs
List shadow eval jobs, newest first, each target with its attempt count so status is accurate. Judged counts, spend, and results ride the detail endpoint only.
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
target_type | query | enum: "key", "team", "user" or null | No | Kind of target to filter on; requires target_id |
target_id | query | string or null | No | Filter to jobs that shadow this target, alone or alongside others |
limit | query | integer | No | Newest jobs to return |
Example request:
curl -X GET "http://localhost:4000/auto_router/shadow_eval" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | Jobs newest first, without results. | application/json array of ShadowEvalJobResponse |
| 400 | target_type and target_id filter together; pass both or neither | application/json ErrorDetail |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 403 | Only proxy admin roles can view shadow evals | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | DB not connected. This endpoint needs a database; set DATABASE_URL to a PostgreSQL connection string (postgresql://...) to enable it. See https://docs.spanway.ai/docs/proxy/virtual_keys | application/json ErrorDetail |
GET /auto_router/shadow_eval/{job_id}
Read one shadow eval job with its results
Shows one shadow eval job with how many turns were judged, what the judge cost, the latest error, and the win rates by tier, model and router, so an operator can decide whether to switch traffic to the auto router. Nothing changes.
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
job_id | path | string | Yes |
Example request:
curl -X GET "http://localhost:4000/auto_router/shadow_eval/job_id-123" \
-H "Authorization: Bearer $SPANWAY_API_KEY"
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The job with derived counts, judge spend, the latest error and stratified verdicts. | application/json ShadowEvalJobResponse |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 403 | Only proxy admin roles can view shadow evals | application/json ErrorDetail |
| 404 | No shadow eval job 7c1f2a90-5e3b-4d8a-9b61-2f0e8c4a1d77 | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | DB not connected. This endpoint needs a database; set DATABASE_URL to a PostgreSQL connection string (postgresql://...) to enable it. See https://docs.spanway.ai/docs/proxy/virtual_keys | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
job_id | string | Yes | Job Id |
targets | array of ShadowEvalJobTargetResponse | Yes | The targets whose traffic this job evaluates, and only theirs, each with its own budget |
router_names | array of string | Yes | Every auto-router this job runs as a shadow arm. Multi-router jobs sample one slice of traffic and judge every arm against the same real responses |
models | array of string | No | Model groups the sampled traffic is narrowed to; empty means every model the targets use |
direction | enum: "forward", "reverse" | No | Direction |
baseline_model | string or null | No | Baseline Model |
judge_model | string | Yes | Judge Model |
shadow_percentage | number | Yes | Shadow Percentage |
created_at | string | Yes | Created At |
ends_at | string | Yes | Ends At |
stopped_by | string or null | No | The operator who stopped the job early, recorded by the stop endpoint; 'unknown' backfilled by migration for jobs that displayed stopped when the column arrived; None when the job ended on its own. Its presence is what m |
judged_count | integer or null | No | Verdicts recorded; detail endpoint only |
error_count | integer or null | No | Sampled attempts that errored; detail endpoint only |
judge_spend | number or null | No | Judge cost so far; detail endpoint only |
last_error | string or null | No | Most recent attempt error; detail endpoint only |
results | AutoRouterShadowEvalResult or null | No | Stratified verdicts; detail endpoint only |
router_name | string | Yes | The first router, kept for callers that predate router_names; derived so the |
status | enum: "running", "completed", "stopped" | Yes | Three recorded facts, no history-guessing: a stop is stopped_by (the migration |
POST /auto_router/shadow_eval/{job_id}/stop
Stop a running shadow eval
Stop an active shadow eval job, every target it scopes at once. Attempts are kept; sampling halts within ~10s. Targets that already stopped on their own budget keep the stopped_at they earned. The statement is the whole state machine: it claims the job only while a leg still samples inside the window with no stop recorded, so a racing operator, a same-instant budget spend, and a repeat stop all read the same 400 with the status the job actually holds.
Parameters:
| Name | In | Type | Required | Description |
|---|---|---|---|---|
job_id | path | string | Yes |
Request body (application/json, object):
Example request:
curl -X POST "http://localhost:4000/auto_router/shadow_eval/job_id-123/stop" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{}'
Responses:
| Status | Description | Body |
|---|---|---|
| 200 | The job as it stands after the stop. | application/json ShadowEvalJobResponse |
| 400 | Job 7c1f2a90-5e3b-4d8a-9b61-2f0e8c4a1d77 is already stopped | application/json ErrorDetail |
| 401 | No valid key or Admin UI session was presented | application/json ProxyError |
| 403 | Only a proxy admin can stop a shadow eval | application/json ErrorDetail |
| 404 | No shadow eval job 7c1f2a90-5e3b-4d8a-9b61-2f0e8c4a1d77 | application/json ErrorDetail |
| 422 | Validation Error | application/json HTTPValidationError |
| 500 | DB not connected. This endpoint needs a database; set DATABASE_URL to a PostgreSQL connection string (postgresql://...) to enable it. See https://docs.spanway.ai/docs/proxy/virtual_keys | application/json ErrorDetail |
Response fields (200):
| Field | Type | Required | Description |
|---|---|---|---|
job_id | string | Yes | Job Id |
targets | array of ShadowEvalJobTargetResponse | Yes | The targets whose traffic this job evaluates, and only theirs, each with its own budget |
router_names | array of string | Yes | Every auto-router this job runs as a shadow arm. Multi-router jobs sample one slice of traffic and judge every arm against the same real responses |
models | array of string | No | Model groups the sampled traffic is narrowed to; empty means every model the targets use |
direction | enum: "forward", "reverse" | No | Direction |
baseline_model | string or null | No | Baseline Model |
judge_model | string | Yes | Judge Model |
shadow_percentage | number | Yes | Shadow Percentage |
created_at | string | Yes | Created At |
ends_at | string | Yes | Ends At |
stopped_by | string or null | No | The operator who stopped the job early, recorded by the stop endpoint; 'unknown' backfilled by migration for jobs that displayed stopped when the column arrived; None when the job ended on its own. Its presence is what m |
judged_count | integer or null | No | Verdicts recorded; detail endpoint only |
error_count | integer or null | No | Sampled attempts that errored; detail endpoint only |
judge_spend | number or null | No | Judge cost so far; detail endpoint only |
last_error | string or null | No | Most recent attempt error; detail endpoint only |
results | AutoRouterShadowEvalResult or null | No | Stratified verdicts; detail endpoint only |
router_name | string | Yes | The first router, kept for callers that predate router_names; derived so the |
status | enum: "running", "completed", "stopped" | Yes | Three recorded facts, no history-guessing: a stop is stopped_by (the migration |