Skip to main content

Provider budget routing

Provider budget routing puts a spend cap on each provider over a time window, for example 100 US dollars a day on OpenAI and 500 a week on Anthropic. Once a provider reaches its budget, the router stops sending it traffic until the window resets, and requests go to other deployments of the same model group. It is useful when you have committed spend with one provider and pay as you go accounts with others, or when one account must never exceed a monthly figure. The filter is part of the free gateway and needs Redis when more than one gateway instance runs.

A real-world scenario​

A company has prepaid 500 dollars a week of Anthropic usage and keeps an OpenAI account as a backstop. Both providers serve the model group frontier. With a 500 dollar weekly budget on anthropic and a 100 dollar daily budget on openai, traffic flows to Anthropic until the prepaid amount is used, then the router sends frontier requests to OpenAI for the rest of the week. When the week resets, Anthropic takes over again. Nobody watches a dashboard to make it happen, and GET /provider/budgets shows how much of each window is left.

Step by step​

In the Admin UI​

  1. Open /ui/models-and-endpoints as a proxy admin and make sure the model group has one deployment per provider, for example frontier on openai/gpt-6.1-sol and on anthropic/claude-sonnet-5-5.
  2. Open /ui/caching, pick Coordination Redis, enter the Redis the gateway replicas share, click Test Connection and Save Changes, and restart. Provider spend counters live in Redis so every replica sees the same totals.
  3. Open /ui/budgets, choose the Provider budgets tab and add a budget for each provider (proxy admins only, see the Budgets page). The tab saves router_settings.provider_budget_config to the gateway config in the database and shows which budgets the running gateway has not applied yet, because provider budgets take effect when the gateway restarts, so restart after saving. Budgets set in config.yaml, as shown below, cannot be changed from the UI.
  4. Open /ui/usage after traffic flows to see spend per provider, and call GET /provider/budgets to see the current window of each budget.

With curl​

router_settings:
provider_budget_config:
openai:
budget_limit: 100
time_period: 1d
anthropic:
budget_limit: 500
time_period: 7d
redis_host: os.environ/REDIS_HOST
redis_port: os.environ/REDIS_PORT
redis_password: os.environ/REDIS_PASSWORD

model_list:
- model_name: frontier
spanway_params:
model: openai/gpt-6.1-sol
api_key: os.environ/OPENAI_API_KEY
- model_name: frontier
spanway_params:
model: anthropic/claude-sonnet-5-5
api_key: os.environ/ANTHROPIC_API_KEY

The keys under provider_budget_config are provider names as used in model prefixes. budget_limit is in US dollars and time_period uses the usual duration format such as 1h, 1d, 7d or 30d. Only budget_limit and time_period are used. A tpm_limit or rpm_limit in an entry is ignored, so set rate limits on the deployments instead, see rate limits.

curl -i -X POST http://localhost:4000/v1/chat/completions \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "frontier", "messages": [{"role": "user", "content": "Draft the release notes"}]}'

curl http://localhost:4000/provider/budgets -H "Authorization: Bearer $SPANWAY_MASTER_KEY"

Expected result​

While both providers are under budget the request is served by either deployment, as the routing strategy decides. Once Anthropic has spent 500 dollars in the current seven day window, every frontier request is served by OpenAI and the x-spanway-model-id header names the OpenAI deployment. GET /provider/budgets lists each provider with its budget_limit, time_period, the spend so far in the window and when the window resets.

How it works​

Budget limiting is a filter, not a routing strategy. After the router has collected the healthy deployments of a group, it removes the ones whose provider has spent its budget in the current window, and then the normal routing strategy picks among the rest. That means it combines with simple-shuffle, latency based routing or any other strategy. If every deployment is over budget, the request fails with an error that names the exhausted budgets.

Spend per provider is tracked in Redis so that all gateway instances share the same counters, which is why Redis is required for this feature when more than one instance runs. Costs come from the bundled price map or your custom pricing.

Deployment budgets​

The same filter applies to budgets on individual deployments, set with max_budget and budget_duration in a deployment's spanway_params. Deployment budgets help when one account of a provider has a monthly commitment and another does not.

Tag budgets in routing​

A budget on a tag created with /tag/new does not take part in this filter. It refuses requests that carry the tag once it is spent, see tag budgets. To have the router skip deployments for a tag that is over budget instead, set tag_budget_config under spanway_settings in the config file.

spanway_settings:
tag_budget_config:
chat-app:
max_budget: 200
budget_duration: 30d

These budgets are tracked like provider budgets and can only be set in the config file. They need no license.

Errors​

What you seeWhyFix
429 No deployments available - crossed budget: ... followed by the deployments that were skippedEvery deployment of the group belongs to a provider over budget.Add a deployment on another provider, raise a budget_limit, or wait for the window to reset.
Budgets reset on every restart, or replicas disagreeNo Redis, so each replica counts on its own in memory.Configure the coordination Redis or the redis_* router settings.
A provider keeps serving after its budget is spentThe provider key in provider_budget_config does not match the model prefix, for example azure versus azure_ai.Use the prefix exactly as it appears in spanway_params.model.
Spend is counted as zeroThe model has no price in the price map or in model_info.Add custom pricing for the model.
403 on GET /provider/budgetsThe key is not a proxy admin.Use a proxy admin key.