Cost optimisation
The Cost Optimization page under Observability in the Admin UI gathers the gateway's cost reporting in four tabs, Overall, Prompt Compression, Prompt Caching and Auto-Router. It answers the question a finance lead asks every month, what did the gateway save us and where, and gives admins the tools to find the next saving. This page explains the data behind the auto router and prompt caching tabs and the routes that feed them. The page is part of the free gateway and needs a connected database.
A real-world scenario
A platform lead is asked whether the auto router rolled out last quarter was worth it. She opens Cost Optimization, sets the range to the quarter, and reads on the Auto-Router tab that smart moved 48 million tokens for 212 dollars where the premium model alone would have cost 824, a 74 percent saving net of classifier cost. On the Prompt Caching tab she filters to Spanway injected and sees the cache breakpoints the gateway added saved another 0.04 dollars per request on the long system prompt. The Cache leakage by virtual key card then shows one key sending large uncached prompts, which becomes next month's project.
Step by step
In the Admin UI
- Open
/ui/cost-optimizationas a proxy admin or admin viewer. The Overall tab shows Total recorded savings split into Compression savings, Prompt caching savings and Auto-router savings, with the Savings chart over time and the Savings by driver donut. Set the date range at the top right. - Pick the Auto-Router tab and its Usage sub tab. Choose a router in the dropdown or keep All auto-routers. Read Total estimated savings, the spend rows, Routing by tier and the Router usage and savings table.
- Pick the Prompt Caching tab. Set the date range, then switch the Prompt caching requests table between All caching, Spanway injected and Cache hits. Click a request id to open it on the Logs page. Read Cache leakage by virtual key or By model to find traffic that is missing prompt caching.
- Turn on Automatic Anthropic prompt caching in the Prompt Caching card and pick a Cache lifetime (TTL) if you want the gateway to insert cache breakpoints itself. Each change saves at once. The switches are the
spanway_settingskeysenable_anthropic_prompt_caching(default off),anthropic_prompt_caching_ttl(default unset, which means 5 minutes) andopenai_system_messages_first(default off), and the settings reference explains what each one does when on and when off. - To prove a router before switching a team to it, use the Shadow Evals sub tab, described on shadow evals.
With curl
Read a session's spend and savings for the key that ran it, which a coding agent can show in its status line.
curl "http://localhost:4000/auto_router/session?session_id=4d1c0b2e" -H "Authorization: Bearer $SPANWAY_API_KEY"
Read the auto router benchmarks for a window, as the Usage sub tab does.
curl "http://localhost:4000/auto_router/benchmarks?start_date=2026-10-22&end_date=2026-11-21" \
-H "Authorization: Bearer $SPANWAY_MASTER_KEY"
Narrow the benchmarks to one internal user, or to one key with api_key set to the key's token hash from the Virtual Keys page. Turns recorded before user attribution existed only show up under their key.
curl "http://localhost:4000/auto_router/benchmarks?start_date=2026-10-22&end_date=2026-11-21&user_id=dana" \
-H "Authorization: Bearer $SPANWAY_MASTER_KEY"
List requests with their prompt caching result, as the Prompt Caching tab does.
curl "http://localhost:4000/cost_optimization/prompt_caching/requests?start_date=2026-11-01T00:00:00Z&end_date=2026-11-21T00:00:00Z&filter=injected&page_size=50" \
-H "Authorization: Bearer $SPANWAY_MASTER_KEY"
Expected result
The session call answers with the rollup for the calling key.
{
"session_id": "4d1c0b2e",
"router_name": "smart",
"router_type": "complexity",
"turns": 14,
"last_model": "claude-haiku-5-5",
"spend": 0.0412,
"savings_estimated_turns": 14,
"savings_estimated_actual_spend": 0.0412,
"saved_spend": 0.118,
"baseline_spend": 0.1592,
"savings_estimated_baseline_spend": 0.1592,
"baseline_model": "anthropic/claude-sonnet-5-5",
"baseline_models": {"anthropic/claude-sonnet-5-5": 14}
}
The benchmarks call answers with totals for the window, routers_in_scope and one entry per configured router under groups, each with sessions, turns, tokens, spend, classifier cost, saved_spend, baseline_spend, saved_pct, tier_turns and a cache block with the hit rate by bucket. The prompt caching call answers with a page of requests.
{
"requests": [
{"request_id": "chatcmpl-8f1c2e", "start_time": "2026-11-20T14:02:11Z", "model": "claude-sonnet-5-5", "gateway_injected": true, "cache_read_tokens": 18240, "cache_creation_tokens": 0, "spend": 0.0091, "net_savings": 0.0412}
],
"page_size": 50,
"has_more": true,
"next_cursor": {"start_time": "2026-11-20T14:02:11Z", "request_id": "chatcmpl-8f1c2e"}
}
filter is all, injected for requests where the gateway added cache control, or hits for requests that read from the cache. Pages continue with cursor_start_time and cursor_request_id copied from next_cursor.
Savings baseline
A router that picks a cheap model saves money only relative to what the traffic would have cost without it. For every complexity router the gateway derives that counterfactual once, from the router's own tier ladder. A deployment has to run the model that can carry the hardest request it will see, so the baseline is the priciest model in the hardest configured tier, ranked against a fixed reference request of 20,000 prompt tokens, 1,000 completion tokens and a mostly cached prompt. Baselines are always provider qualified so that a bare model name cannot be priced against another vendor's rate.
Each routed request records the baseline model and deployment with its routing decision. The spend writer prices the request at the baseline's rate as well as the actual rate, and the per session rollup accumulates actual spend, estimated baseline spend and the difference. Adaptive and quality routers derive no baseline, so they report usage but no savings. general_settings.maximum_autorouter_session_retention_period controls how long the session rollups are kept.
Predicting cache cost
POST /cost/predict-cache compares the next native Anthropic request on two deployment ids, estimating cost with provider token counting and the key's recent cache telemetry. It supports one message content breakpoint, text and client tools, and reports unknown for thinking, images, non default API versions and beta headers. Each comparison spends up to four provider count requests and does not generate tokens, warm caches or change routing.
| 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 |
| GET | /cost_optimization/prompt_caching/requests | List requests with their prompt caching result |
| POST | /cost/predict-cache | Predict the prompt cache cost of staying on or switching a deployment |
Errors
/auto_router/benchmarks, /auto_router/session and /cost_optimization/prompt_caching/requests answer errors as {"detail": "<message>"}.
| What you see | Why | Fix |
|---|---|---|
| Auto-router usage is visible to proxy admin roles only | The benchmarks route answered 403. | Use a proxy admin or admin viewer account. |
403 Only proxy admin roles can view auto-router benchmarks across the deployment | The benchmarks route over the API with a key that is not a proxy admin or admin viewer. | Use an admin key. |
403 Only proxy admin roles can view prompt caching requests | The same for the caching requests route. | Use an admin key. |
404 No auto-routed turns recorded for session '<id>' under this key | The session has no flushed auto routed turn yet, or it ran under another key. | Wait a moment after the first turn, and call with the key that ran the session. |
400 end_date must not be earlier than start_date | The dates are reversed. | Swap them. |
400 Invalid date format: <value>. Expected: 'YYYY-MM-DD' | /auto_router/benchmarks takes plain dates. | Send 2026-11-01, not a timestamp. |
400 cursor_start_time and cursor_request_id must be provided together | Only one cursor field was sent. | Copy both from next_cursor. |
401 Authentication Error, No api key passed in. | No key was sent. | Send Authorization: Bearer <key>. |
404 Deployment not found from POST /cost/predict-cache | One of the two deployment ids is unknown or belongs to a model the key's team cannot see. | Copy the ids from Models + Endpoints. |
503 Model router is unavailable from POST /cost/predict-cache | The gateway has not loaded its models yet. | Retry after startup finishes. |
| Unavailable in the savings rows | Requests predate savings tracking, or the router is adaptive or quality and has no baseline. | Nothing to fix. Newer complexity router traffic fills the rows in. |
500 DB not connected. This endpoint needs a database; ... | The page reads rollups from the database. | Connect a database. |
| Failed to load prompt caching settings | The settings read failed, often during a restart. | Reload the tab. |
| A caching switch goes back and a toast shows an error | The gateway refused the change, for example for an admin viewer. | Sign in as a proxy admin. |
The error reference lists the same messages.
Logs
| Level | Line | Meaning | Action |
|---|---|---|---|
| ERROR | shadow_eval: funnel seed failed for job <id>: <error> | A shadow eval started, but its coverage counters could not be created in the database, so coverage shows as unknown for that job. The job itself keeps running. | Check the database if it repeats. Do not start the job again, the running job would answer 409. |
| ERROR | Auto router routing test failed. Due to error - <error> | The test call on the Auto-Router tab failed. | Read the error, usually a model or key problem of the router. |
| WARNING | Spend tracking - database out of connections or rollup row lock timed out ... | A rollup write was put off to the next flush, so new savings show a little later. | None when rare, see the log reference. |
Related
- Shadow evals test a router on live traffic before switching a team to it.
- Auto routing creates the routers this page measures.
- Prompt caching explains provider side caching and the gateway's injected breakpoints.
- Headroom prompt compression is the compression the Prompt Compression tab configures.
- Cost Optimization page documents every control of the four tabs.