Skip to main content

Tag routing

Tag routing lets the request decide which deployments of a model group it may use. Each deployment carries a list of tags, and each request carries tags from its body, its headers, its key or its team. The router keeps only deployments whose tags match before the routing strategy picks one. Typical uses are separating paid and free tiers onto different provider accounts, keeping regulated traffic on approved regions, or giving one team dedicated capacity. Tags are defined on the Tag Management page or with the /tag/* routes, can carry their own budget, and work without a license.

A real-world scenario​

A SaaS company runs the same claude-sonnet-5-5 for free and paying customers but wants paying customers on a provider account with a higher rate limit. The admin tags the premium deployment paid and the shared one free and default, turns on tag filtering, and gives the paying customers' keys the tag paid. Requests from those keys land on the premium account, everyone else stays on the shared one, and the Usage page reports spend per tag so finance can see what each tier costs.

Step by step​

In the Admin UI​

  1. Open /ui/tag-management as a proxy admin and click + Create New Tag. Type the Tag Name paid, pick the deployments it should route to under Tag these deployments, optionally open Budget & Rate Limits to set a Max Budget (USD) and Reset Budget period, and click Create Tag. Each chosen deployment that was added in the Admin UI or over the API gets the tag added to its Tags once, when the tag is created. A deployment defined in config.yaml is skipped, so give it the tag in config.yaml.
  2. Open /ui/router-settings, pick the Loadbalancing tab, turn on Enable Tag Filtering and click Save Changes.
  3. Open /ui/api-keys, create or edit the paying customer's key, and add paid under Tags. Do the same on a team under /ui/teams when every key of the team should carry it.
  4. Open /ui/models-and-endpoints and check the deployment's detail view shows paid under Tags. Add default to the deployment that should serve requests without tags.

With curl​

The same setup in config.yaml.

router_settings:
enable_tag_filtering: true

model_list:
- model_name: claude-sonnet-5-5
spanway_params:
model: anthropic/claude-sonnet-5-5
api_key: os.environ/ANTHROPIC_API_KEY_PAID
tags: ["paid"]
- model_name: claude-sonnet-5-5
spanway_params:
model: anthropic/claude-sonnet-5-5
api_key: os.environ/ANTHROPIC_API_KEY_FREE
tags: ["free", "default"]

Create the tag over the API and send a tagged request.

curl -X POST http://localhost:4000/tag/new \
-H "Authorization: Bearer $SPANWAY_MASTER_KEY" \
-H "Content-Type: application/json" \
-d '{"name": "paid", "description": "Paying customers", "models": ["3f9c0e2a-paid"], "max_budget": 200.0, "budget_duration": "30d"}'

curl -i -X POST http://localhost:4000/v1/chat/completions \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-H "x-spanway-tags: paid" \
-d '{"model": "claude-sonnet-5-5", "messages": [{"role": "user", "content": "Hello"}]}'

Tags can also be sent in the body as "tags": ["paid"] or "metadata": {"tags": ["paid"]}, or attached to a key or team with the tags field so that every request from it carries them.

A tag the model group does not know falls back to the deployments tagged default. To make a tag strict, set tag_routing_prefix in router_settings, for example route:, and send the tag as route:paid. The gateway removes the prefix, matches paid, and fails the request when no deployment carries paid instead of falling back.

Expected result​

POST /tag/new answers 200 with the stored tag.

{
"message": "Tag paid created successfully",
"tag": {"name": "paid", "description": "Paying customers", "models": ["3f9c0e2a-paid"], "model_info": {"3f9c0e2a-paid": "claude-sonnet-5-5"}, "created_at": "2026-11-21T09:12:44.102Z", "updated_at": "2026-11-21T09:12:44.102Z", "created_by": "admin@example.com"}
}

The tagged request is answered by the paid deployment, and its x-spanway-model-id header names it. A request without tags goes to the deployment tagged default. The spend log row records the tags, GET /tag/list shows the tag with its budget, and the Usage page reports spend per tag.

Matching rules​

Request tagsDeployments used
At least one tag that a deployment also carries (the default)Those deployments
Several tags, when tag_filtering_match_any is falseOnly deployments that carry all of the request's tags
No tagsDeployments tagged default, or every deployment when none is tagged default
!tagExcludes deployments carrying that tag
&tagRequires deployments to carry that tag

When a tag is known to the model group but no deployment matches, the request fails with an error naming the tags. When the tag appears on no deployment of the group, the request goes to the deployments tagged default, so a typo or a tag meant for another model does not block traffic. Send the tag with tag_routing_prefix to make it fail instead.

Match any tag or every tag​

tag_filtering_match_any in router_settings decides how many tags must line up, and it shows as Tag Filtering Match Any on the Router Settings page. It is on by default, and then a deployment is used when it shares at least one tag with the request. Turn it off when a request must reach a deployment that carries all of its tags, for example paid and eu together. It only matters while tag filtering is on.

Turning tag filtering on per model group or per key​

SettingWhereDefaultOnOff
enable_tag_filteringrouter_settingsfalseEvery model group filters by tags.Tags only record spend.
enable_tag_filteringA deployment's model_infounset, follows the routerThis model group filters by tags even when the router setting is off. Set it the same on every deployment of the group.This model group ignores tags even when the router setting is on.
enable_tag_filteringA key's or team's router_settingsunsetRequests from that key or team filter by tags. This wins over the model group setting.No effect.
allow_fail_openA deployment's model_infofalseWhen !tag or &tag rules leave no deployment, the request goes to the default pool instead of failing, as long as the rules from the key or team still hold.The request fails with the tag routing error.

Who can set tags​

Tags from the request body, the x-spanway-tags header and metadata.tags are trusted by default, so any key can send x-spanway-tags: paid and reach the paid deployment and spend against the paid tag's budget. When tags decide which tier a customer gets, set reject_clientside_metadata_tags: true in general_settings. The gateway then refuses a request that sends tags in metadata.tags, the top-level tags field or the x-spanway-tags header with a 400, and tags only come from the key and the team.

Matching on headers​

tag_regex on a deployment lists regular expressions matched against the request's User-Agent header, written as User-Agent: <value>. No other header is matched. A match routes the request to that deployment without the client sending tags, and it only works while tag filtering is on.

model_list:
- model_name: claude-sonnet-5-5
spanway_params:
model: bedrock/us.anthropic.claude-sonnet-5-5
aws_region_name: us-east-1
tag_regex: ["^User-Agent: claude-code/.*"]

Managing tags​

MethodPathSummary
POST/tag/newCreate a tag
POST/tag/updateUpdate a tag
POST/tag/infoRead tags by name
GET/tag/listList tags
POST/tag/deleteDelete a tag

Allowed models tag the chosen database deployments once, when the tag is created. Changing the list later with /tag/update or deleting the tag does not add or remove the tag on any deployment. Routing always follows each deployment's own Tags field, so edit the deployment to change where tagged requests go. /tag/update changes only the fields you send, so {"name": "paid", "max_budget": 300} keeps the tag's models and description.

A tag's budget can also carry rpm_limit and tpm_limit. Send them to /tag/new or /tag/update. The gateway then refuses requests carrying the tag with a 429 once the tag passes that many requests or tokens in a minute. The Admin UI forms do not set these two fields yet.

Tags created through these routes can carry their own budgets and allowed models, and the usage pages report spend per tag. A tag's budget refuses requests that carry the tag once it is spent, see tag budgets. A tag a request passes in without a definition still records spend and shows on the Tag Management page as a dynamic spend tag.

Tag spend reports​

MethodPathSummary
GET/tag/daily/activity/aggregatedDaily activity per tag
GET/tag/daily/activity/aggregated/keysKeys ranked by spend for a tag
GET/tag/daily/activity/aggregated/searchSearch the keys with activity for a tag
GET/tag/daily/activity/aggregated/model_top_keysTop keys for one model in a tag
GET/tag/daily/activity/exportDownload tag daily activity as CSV or JSON
GET/tag/daily/activityDaily spend and usage per tag

Proxy admins and admin viewers see every tag. An internal user sees only the spend that their own keys produced, and asking for another key's spend returns an empty answer instead of an error.

Audit​

Every create, update and delete of a tag writes a row to the audit log with the table Spanway_TagTable, the tag name, who made the change and the values before and after, including the budget fields. Each deployment that /tag/new tags gets its own updated row on Spanway_ProxyModelTable with its tags before and after. See audit logs.

Errors​

What you seeWhyFix
401 Not allowed to access model due to tags configuration. Passed model=claude-sonnet-5-5 and tags=['paid']Tag filtering is on and no deployment of the group carries a tag the group knows.Tag a deployment, or add default to one so untagged requests have somewhere to go.
400 Client-side 'x-spanway-tags' not allowed in request. (or 'tags', 'metadata.tags')reject_clientside_metadata_tags is on and the request sent its own tags.Put the tags on the key or team instead.
Tags have no effectenable_tag_filtering is off.Turn on Enable Tag Filtering on the Loadbalancing tab.
400 Tag paid already existsPOST /tag/new with a name that is taken.Use POST /tag/update, or pick another name.
404 Tags not found: ['paid']/tag/info named an undefined tag.Create it first, or check the spelling on the Tag Management page.
404 Tag paid not found/tag/update or /tag/delete named an undefined tag.Check the spelling on the Tag Management page.
500 Database not connected, or on /tag/new DB not connected. This endpoint needs a database; set DATABASE_URL ...The gateway has no database.Set DATABASE_URL and restart.
500 No models configured on proxy from /tag/newThe gateway serves no models yet, so there is nothing to tag.Add a model with /model/new or config.yaml first.
The Admin UI cannot open a tag and says it is a dynamic spend tagRequests passed the tag in without a definition.Create the tag with the same name to give it models and a budget.
A request with the tag is refused with a budget errorThe tag's budget is spent.Raise Max Budget (USD), or wait for the Reset Budget period.
401 Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. Route=/tag/new. ...The key belongs to an internal user.Use a proxy admin key.
403 user not allowed to access this route, role= proxy_admin_viewer. Trying to access: /tag/newAdmin viewers can read tags but not change them.Use a proxy admin key.
429 naming the tag's rate limitThe tag's rpm_limit or tpm_limit was reached.Wait a minute, or raise the limit with /tag/update.

Logs​

LevelMessageMeaningAction
ERRORError creating tag: <reason>Storing a new tag failed, usually the database.Check the database connection and retry.
ERRORError updating tag: <reason>Saving a tag change failed.Check the database connection and retry.
ERRORError adding tag to deployment: <reason>The tag was stored but a deployment could not be tagged.Add the tag on the deployment's Tags field.
INFOTag paid not added to deployment <id>: it is defined in config.yaml, set its tags thereA chosen deployment lives in config.yaml.Normal. Add the tag in config.yaml.
ERRORError getting model names: <reason>The tag was stored without the model names shown next to the ids.Normal unless it repeats, then check the database.
WARNINGtag_regex: invalid pattern '<pattern>', skipping itA deployment's tag_regex entry is not a valid regular expression and is ignored.Fix the pattern.

Run the gateway with --detailed_debug to see the routing decisions behind a tag match.