Shared budgets
A shared budget is a named set of limits that many objects can point at. Instead of typing a 5 dollar monthly cap and a 20 request per minute limit onto every customer you create, you create the budget once, give it an id such as free-tier, and pass that id when you create an end user, a tag, an organization or a project. Changing the budget changes the limits for all of them at the next request. Each object still keeps its own spend, so two end users on one budget of 20 dollars can each spend 20 dollars. Shared budgets are free on every gateway and need a database.
A real-world scenario
A product team offers a free tier and a paid tier of its assistant. Every free user may spend 5 dollars a month and send 20 requests a minute, and paid users get 50 dollars and 120 requests a minute. The team creates two budgets, free-tier and pro-tier, and the signup service creates each new customer on the gateway with the matching budget_id. When marketing raises the free tier to 8 dollars, one change to the free-tier budget applies to every free user at their next request, and nobody has to touch thousands of customer records.
Create a budget and attach it
-
In the Admin UI open Budgets in the left nav and click Create Budget. Fill in Budget ID (
free-tier) and Max Requests per minute (20), click Optional Settings to open it, fill in Max Budget (USD) (5) and Reset Budget (monthly), then click Create Budget. Leave Optional Settings open while you click, because closing it drops the two values in it. A soft budget is set with the API. The toast reads Budget Created and the row appears in the table with its Max Budget, TPM, RPM, TPD (batch) and Reset columns. The same step with the API, as a proxy admin:curl -X POST http://localhost:4000/budget/new \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"budget_id": "free-tier", "max_budget": 5, "soft_budget": 4, "budget_duration": "30d", "rpm_limit": 20}'Expected result: the stored budget, with
budget_reset_atset to the end of the first 30 days from now.{"budget_id": "free-tier","max_budget": 5.0,"soft_budget": 4.0,"rpm_limit": 20,"budget_duration": "30d","budget_reset_at": "2026-12-21T00:00:00Z","created_at": "2026-11-21T09:30:00Z","created_by": "admin"} -
Attach it to an end user. Create the customer with the budget id instead of its own limits:
curl -X POST http://localhost:4000/customer/new \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"user_id": "customer-42", "budget_id": "free-tier"}'Expected result: the customer row with
budget_id: "free-tier". On the Customers page the customer's Spend / Budget column shows its spend against the 5 dollar cap. -
Or let keys hand the budget out. Signed in as a proxy admin, open Virtual Keys, create or edit the Service Account key your application uses, and pick
free-tierunder Default Customer Budget. The control is shown only for Service Account keys and only to proxy admins. Every new customer that key creates through theuserfield or thex-spanway-end-user-idheader gets the budget. The same default for the whole gateway isspanway_settings.max_end_user_budget_id, and customers that already have their own budget keep it. -
Attach it to a tag, an organization or a project the same way, with
budget_idinPOST /tag/new,POST /organization/neworPOST /project/new, see tag budgets and organization budgets. -
Change the limits later from the Budgets page with the row's edit action and Save, or with
POST /budget/update. Every attached object sees the new limits on its next request.
Expected result when the budget is spent: the request is refused with a 422 budget_exceeded error, for example ExceededBudget: End User=customer-42 over budget. Spend=5.01, Budget=5.0. Set spanway_settings.budget_exceeded_status_code: 429 if your clients only back off on 429. A spent customer or tag budget sends no alert. The budget_alerts alert, when alerting is on, covers spent budgets of keys, users, teams, projects and organizations. Spend resets at budget_reset_at, and from then on every budget_duration, as described in when budgets reset.
Fields
| Field | Meaning |
|---|---|
budget_id | The id to attach the budget by. One is generated when you leave it out. |
max_budget | Hard cap in US dollars. Requests fail once it is spent. |
soft_budget | Spend level that sends an alert without blocking. The alert fires only for keys, teams and projects. On a shared budget used by end users, tags or organizations it is stored but sends nothing. |
budget_duration | How often spend resets: a number and a unit, s, m, h, d, w or mo, such as 1h, 1d, 2w or 30d, or the words hourly, daily, weekly or monthly, which mean 1h, 24h, 7d and 30d. |
budget_reset_at | When the first reset happens. It defaults to the end of the first budget_duration from now. |
tpm_limit, rpm_limit | Tokens and requests per minute. |
tpd_limit | Tokens per day, charged by batch submissions instead of tpm_limit and rpm_limit. |
max_parallel_requests | Requests allowed at the same time. |
model_max_budget | Separate limits per model, such as {"claude-haiku-5-5": {"max_budget": 10, "budget_duration": "1d"}}. |
max_budget and soft_budget must be finite and not negative, and budget_duration must be a valid duration, or the route answers 400. With audit logging on, a Spanway Enterprise feature, every change writes a row to the audit log.
When budgets reset
A budget resets to zero spend at budget_reset_at, then every budget_duration after that. The reset job runs about every 10 minutes, so spend goes back to zero within about 10 minutes after the reset time, not to the second. Daily and longer budgets reset at midnight in the gateway's time zone. Two spanway_settings keys change that: timezone (default UTC), for example America/Chicago so a daily budget resets at your midnight, and budget_reset_time (default "00:00"), a quoted 24-hour time such as "06:00" for a reset at six in the morning. A budget_reset_time the gateway cannot read stops it at startup with Invalid budget_reset_time .... The settings reference lists these and the reset job settings. The full lifecycle of the /budget routes, with an example for each, is on managing budgets with the API.
Fail-closed enforcement
Budgets fail closed by default. When the gateway cannot read current spend because Redis or the database is unavailable, a request that a budget applies to gets a 503 instead of being admitted. Set general_settings.fail_closed_budget_enforcement: false to admit those requests on the per instance spend instead, see Redis requirements.
Provider budgets
The Provider budgets tab of the Budgets page shows a different kind of budget: the per provider spend caps from the router settings that route traffic away from a provider once it has spent its budget. It reads GET /provider/budgets, see provider budget routing.
Errors
| Status | Meaning | Fix |
|---|---|---|
400 Budget with id 'free-tier' already exists. | POST /budget/new reused an id. | Pick another id, or change the existing budget with POST /budget/update. |
400 max_budget must be a non-negative finite number. | A negative, infinite or not-a-number amount. | Send 0 or more. |
400 Invalid budget_duration '<value>'. ... | The duration is not a valid period, for example 1hr, or never advances. | Use a number followed by s, m, h, d, w or mo, or hourly, daily, weekly or monthly. |
400 budget_id is required from POST /budget/update. | The update did not say which budget to change. | Send budget_id. |
404 Budget with id '<id>' not found from POST /budget/update or POST /budget/delete. | No budget has that id, and nothing changed. | Check the id with GET /budget/list. |
400 Budget <id> is the budget of organization <org>. ... from POST /budget/delete. | An organization still uses the budget. | Give the organization another budget first. |
422 budget_exceeded from a model request, such as ExceededBudget: End User=<id> over budget. ... or Budget has been exceeded! ... | The object spent its max_budget in the current period. | Raise the budget, or wait for the reset. The status is budget_exceeded_status_code, 422 by default. |
| 503 on a request that a budget applies to. | Redis or the database is unavailable and budgets fail closed. | Restore the store, or set fail_closed_budget_enforcement: false to admit on per instance spend. |
| A customer keeps its old limits after you change the budget. | The customer has a budget of its own, not the shared one. | Check budget_id on GET /customer/info, and remove the customer's own max_budget. |
See the error reference for the same messages next to every other error.
Logs
| Level | Line | Meaning | Action |
|---|---|---|---|
| WARNING | Unrecognized budget_duration '<value>'; falling back to a next-midnight reset. ... | A stored budget has a reset period the gateway cannot read, so it resets every midnight. | Set a valid budget_duration. |
| WARNING | general_settings.fail_closed_budget_enforcement=<value> is not a boolean, keeping the fail-closed default | The switch is not true or false, so budgets keep failing closed. | Fix the value. |
| WARNING | Failed to evict the team member default budget cache | A budget change was saved, but a cached copy was not cleared, so team members may see the old limit for a short while. | Check Redis. |
Related
- Managing budgets with the API for every
/budgetroute with its request and response. - End users for creating customers and the proxy-wide default customer budget.
- Team budgets for budgets on keys, teams, tags and organizations.
- Budgets page for the Admin UI reference.
- Alerting for the
budget_alertsalert type. - Audit logs for the rows budget changes write.