Skip to main content

Fallback management API

A fallback is the model the gateway tries next when a model keeps failing. The router settings hold a list of fallbacks for every model, and the Fallbacks tab of Router Settings edits that list by hand. The fallback management routes do the same job for scripts. POST /fallback sets the fallbacks of one model, GET /fallback/{model} reads them and DELETE /fallback/{model} removes them. A change applies to running traffic at once, is stored in the database so it survives a restart, and is written to the audit log. Only the stored router settings are rewritten, so values that come from config.yaml or the environment, such as a Redis password, never reach the database. A fallback list that config.yaml sets is owned by the file and refused with a 400, because the file would win again on the next restart. default_fallbacks keep working after every change. The routes are part of the free gateway and need STORE_MODEL_IN_DB=True and a connected database.

A real-world scenario​

An on-call engineer learns that the Anthropic account behind claude-sonnet-5-5 will be rate limited for the next hour. Rather than opening the Admin UI and re-entering every setting, the runbook runs one POST /fallback that sends failed claude-sonnet-5-5 calls to gpt-6.1-sol. Requests that hit the rate limit are retried on the fallback instead of failing, and the spend logs show which model answered. When the hour is over, a DELETE /fallback/claude-sonnet-5-5 puts things back, and both changes sit in the audit log with the engineer's key.

Step by step​

In the Admin UI​

  1. Open /ui/router-settings as a proxy admin and pick the Fallbacks tab.
  2. Click Add Fallbacks. In Configure Model Fallbacks pick the Primary Model and then the models of the Fallback Chain in the order they should be tried, up to 10.
  3. Click Save All Configurations. The table shows the primary model with its chain.
  4. Click Test fallback on the row to send a real test message to the primary model with the fallback flag set, which can incur provider cost. A toast names the model you asked for and the model that answered.
  5. Use Edit fallback to change the chain and Delete fallback to remove it. The dialog warns that a delete cannot be undone.

With curl​

Set the general fallbacks of one model.

curl -X POST http://localhost:4000/fallback \
-H "Authorization: Bearer $SPANWAY_MASTER_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "claude-sonnet-5-5", "fallback_models": ["gpt-6.1-sol"], "fallback_type": "general"}'
FieldWhat it does
modelThe model name clients call. It must exist on the gateway.
fallback_modelsThe models to try next, in order. Every one must exist on the gateway, and the model cannot be its own fallback.
fallback_typegeneral (the default) for any error, context_window for prompts too long for the model, or content_policy for provider content filter refusals.

Read and remove them.

curl "http://localhost:4000/fallback/claude-sonnet-5-5?fallback_type=general" \
-H "Authorization: Bearer $SPANWAY_MASTER_KEY"

curl -X DELETE "http://localhost:4000/fallback/claude-sonnet-5-5?fallback_type=general" \
-H "Authorization: Bearer $SPANWAY_MASTER_KEY"

Posting for a model that already has fallbacks of that type replaces its list. Each fallback_type is a separate list, so a model can have general, context window and content policy fallbacks at the same time.

Expected result​

The POST answers 200 and names whether the list was created or updated.

{
"model": "claude-sonnet-5-5",
"fallback_models": ["gpt-6.1-sol"],
"fallback_type": "general",
"message": "Fallback configuration created successfully"
}

From that moment a request for claude-sonnet-5-5 whose retries all fail goes to gpt-6.1-sol. The response carries x-spanway-attempted-fallbacks: 1 and x-spanway-model-group names the model that answered. The new list shows on the Fallbacks tab, GET /fallback/claude-sonnet-5-5 returns it, and the audit log has a row for router_settings.fallbacks with the lists before and after. The DELETE answers 200 with "message": "Fallback configuration deleted successfully" and the router stops falling back for that model at once.

Errors​

What you seeWhyFix
404 Model 'x' not found in routerThe primary model is not on the gateway. The body lists available_models.Use a name from available_models, or add the model first.
400 Invalid fallback models: [...]A fallback model is not on the gateway.Fix the name, or add the model first.
400 Model 'x' cannot be its own fallbackThe model appears in its own fallback_models.Remove it from the list.
400 Database storage not enabledWrites need STORE_MODEL_IN_DB=True.Set the variable on the gateway and restart it. Reads still work without it.
400 router_settings key 'fallbacks' is set in the config file and cannot be changed here.config.yaml sets that fallback list, and the file wins on restart.Edit the file, or remove the list from it so the database owns it.
404 No general fallbacks configured for model 'x'The model has no list of that type.Check fallback_type, or read the whole router settings with GET /config/list.
401 Authentication Error, Only proxy admin can be used to ...The key is not a proxy admin key.Use a proxy admin key.
403 user not allowed to access this route, role= proxy_admin_viewer. ...An admin viewer tried to change a fallback. Admin viewers may only read them.Use a proxy admin key.
500 Router not initializedThe gateway has not finished loading its model list.Retry after start up.
The Admin UI shows the old chain after a POSTThe page caches the router settings it loaded.Click Reset on the Router Settings page to reload it.