Skip to main content

Budget management API

This page documents the 7 gateway endpoints tagged budget management in the gateway's OpenAPI document. Every example targets a gateway on http://localhost:4000 and authenticates with the SPANWAY_API_KEY environment variable, which holds the master key or a virtual key that has access to the route. Request and response fields are taken from the schema the running gateway publishes at /openapi.json, and the same document is bundled with this site as openapi.json.

MethodPathSummary
GET/management/v1/budgetsList the budgets, paged, sorted and filtered
POST/budget/newCreate a shared budget
POST/budget/updateChange a shared budget
POST/budget/infoLook up budgets by id
GET/budget/settingsShow the editable fields of a budget
GET/budget/listList every budget
POST/budget/deleteDelete a budget

Endpoints​

GET /management/v1/budgets​

List the budgets, paged, sorted and filtered

List the shared budgets defined on the gateway, the named spend caps and rate limits that end users, tags, organizations and projects point at by budget_id, a page at a time. This is what the Admin UI Budgets page reads.

Only a proxy admin or an admin viewer may call it, anyone else gets 403. Nothing is changed.

sort takes a comma-separated list of budget_id, max_budget, tpm_limit, rpm_limit, tpd_limit or created_at, each optionally prefixed with - for descending, and defaults to -created_at. budget_id is appended to every sort as the tiebreaker. q is a case-insensitive substring match on budget_id. page_size defaults to 50 and is capped at 100. Filters are filter[budget_duration][in|is_null], filter[max_budget][gte|lte|is_null] and filter[created_at][gte|lte], and any other query parameter is refused with 400 so a mistyped filter never returns more than you asked for.

Docs (https://docs.spanway.ai/docs/proxy/budgets)

Example request:

curl -X GET "http://localhost:4000/management/v1/budgets" \
-H "Authorization: Bearer $SPANWAY_API_KEY"

Responses:

StatusDescriptionBody
200Successful Responseapplication/json ListResponse_BudgetListItem_
400A sort, filter or other query parameter is unknown or has a bad valueapplication/problem+json object
401No valid key or Admin UI session was presentedapplication/json ProxyError
403The caller is not a proxy admin or an admin viewerapplication/problem+json object
500Reading the budgets failedapplication/problem+json object
503The gateway has no database connectedapplication/problem+json object

Response fields (200):

FieldTypeRequiredDescription
dataarray of BudgetListItemYesData
metaListMetaYesPage-mode counterpart to PageMeta: an entity list pays for the COUNT(*) so the table can show a page count.
linksListLinksYesPage-mode counterpart to PageLinks. first/last are knowable here because the total count is.

POST /budget/new​

Create a shared budget

Create a budget that end users, tags, organizations and projects can share by pointing at its budget_id.

max_budget is the hard cap in US dollars, soft_budget only raises an alert, budget_duration says how often spend resets (1h, 1d, 30d), and tpm_limit, rpm_limit, tpd_limit and max_parallel_requests cap the rate. model_max_budget holds separate limits per model. Leave budget_id out to have one generated, and leave budget_reset_at out to reset at the end of the first duration from now. A budget_id that already exists, a negative or infinite amount, or a duration that never advances is a 400. Proxy admins only. The change is written to the audit log.

Docs (https://docs.spanway.ai/docs/proxy/budget_management)

Request body (application/json, schema BudgetNewRequest):

FieldTypeRequiredDescription
budget_idstring or nullNoThe unique budget id.
max_budgetnumber or nullNoRequests will fail if this budget (in USD) is exceeded.
soft_budgetnumber or nullNoRequests will NOT fail if this is exceeded. Will fire alerting though.
max_parallel_requestsinteger or nullNoMax concurrent requests allowed for this budget id.
tpm_limitinteger or nullNoMax tokens per minute, allowed for this budget id.
rpm_limitinteger or nullNoMax requests per minute, allowed for this budget id.
tpd_limitinteger or nullNoMax tokens per day, charged by batch submissions, allowed for this budget id.
budget_durationstring or nullNoHow often spend resets to zero, for example '1h', '1d', '7d', '30d' or '1mo'
model_max_budgetobject or nullNoMax budget for each model (e.g. {'gpt-4o': {'max_budget': '0.0000001', 'budget_duration': '1d', 'tpm_limit': 1000, 'rpm_limit': 1000}})
budget_reset_atstring or nullNoDatetime when the budget is reset

Example request:

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
}'

Responses:

StatusDescriptionBody
200The budget as storedapplication/json Spanway_BudgetTableFull
400The request breaks a rule named in the detailapplication/json ErrorDetail
401No valid key was sent, or the key belongs to someone who is not a proxy adminapplication/json ProxyError
403An admin viewer tried to change a budget, and admin viewers can only readapplication/json ProxyError
422Validation Errorapplication/json HTTPValidationError
500The gateway has no database connectedapplication/json ErrorDetail

Response fields (200):

FieldTypeRequiredDescription
budget_idstring or nullNoBudget Id
soft_budgetnumber or nullNoSoft Budget
max_budgetnumber or nullNoMax Budget
max_parallel_requestsinteger or nullNoMax Parallel Requests
tpm_limitinteger or nullNoTpm Limit
rpm_limitinteger or nullNoRpm Limit
tpd_limitinteger or nullNoTpd Limit
model_max_budgetobject or nullNoModel Max Budget
budget_durationstring or nullNoBudget Duration
allowed_modelsarray of string or nullNoAllowed Models
temp_budget_increasenumber or nullNoTemp Budget Increase
temp_budget_expirystring or nullNoTemp Budget Expiry
budget_reset_atstring or nullNoBudget Reset At
created_atstringYesCreated At
created_bystring or nullNoCreated By
updated_atstring or nullNoUpdated At
updated_bystring or nullNoUpdated By

POST /budget/update​

Change a shared budget

Change the fields you send on the budget named by budget_id, and leave every other field as it is.

Every object attached to the budget sees the new limits on its next request. Sending a new budget_duration moves budget_reset_at to the end of the first new duration unless you pass budget_reset_at yourself, and sending budget_duration: null clears the reset time. A missing budget_id, a negative or infinite amount, or a duration that never advances is a 400, and a budget_id that does not exist is a 404. Proxy admins only. With audit logging on, the change is written to the audit log.

Docs (https://docs.spanway.ai/docs/proxy/budget_management)

Request body (application/json, schema BudgetNewRequest):

FieldTypeRequiredDescription
budget_idstring or nullNoThe unique budget id.
max_budgetnumber or nullNoRequests will fail if this budget (in USD) is exceeded.
soft_budgetnumber or nullNoRequests will NOT fail if this is exceeded. Will fire alerting though.
max_parallel_requestsinteger or nullNoMax concurrent requests allowed for this budget id.
tpm_limitinteger or nullNoMax tokens per minute, allowed for this budget id.
rpm_limitinteger or nullNoMax requests per minute, allowed for this budget id.
tpd_limitinteger or nullNoMax tokens per day, charged by batch submissions, allowed for this budget id.
budget_durationstring or nullNoHow often spend resets to zero, for example '1h', '1d', '7d', '30d' or '1mo'
model_max_budgetobject or nullNoMax budget for each model (e.g. {'gpt-4o': {'max_budget': '0.0000001', 'budget_duration': '1d', 'tpm_limit': 1000, 'rpm_limit': 1000}})
budget_reset_atstring or nullNoDatetime when the budget is reset

Example request:

curl -X POST "http://localhost:4000/budget/update" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"budget_id": "free-tier",
"max_budget": 10
}'

Responses:

StatusDescriptionBody
200The budget as storedapplication/json Spanway_BudgetTableFull
400The request breaks a rule named in the detailapplication/json ErrorDetail
401No valid key was sent, or the key belongs to someone who is not a proxy adminapplication/json ProxyError
403An admin viewer tried to change a budget, and admin viewers can only readapplication/json ProxyError
404No budget has that budget_idapplication/json ErrorDetail
422Validation Errorapplication/json HTTPValidationError
500The gateway has no database connectedapplication/json ErrorDetail

Response fields (200):

FieldTypeRequiredDescription
budget_idstring or nullNoBudget Id
soft_budgetnumber or nullNoSoft Budget
max_budgetnumber or nullNoMax Budget
max_parallel_requestsinteger or nullNoMax Parallel Requests
tpm_limitinteger or nullNoTpm Limit
rpm_limitinteger or nullNoRpm Limit
tpd_limitinteger or nullNoTpd Limit
model_max_budgetobject or nullNoModel Max Budget
budget_durationstring or nullNoBudget Duration
allowed_modelsarray of string or nullNoAllowed Models
temp_budget_increasenumber or nullNoTemp Budget Increase
temp_budget_expirystring or nullNoTemp Budget Expiry
budget_reset_atstring or nullNoBudget Reset At
created_atstringYesCreated At
created_bystring or nullNoCreated By
updated_atstring or nullNoUpdated At
updated_bystring or nullNoUpdated By

POST /budget/info​

Look up budgets by id

Return the stored budgets for the budget_id values in budgets.

Ids that do not exist are left out of the answer rather than reported, so compare the result with what you sent. An empty list is a 400. Proxy admins and admin viewers only.

Docs (https://docs.spanway.ai/docs/proxy/budget_management)

Request body (application/json, schema BudgetRequest):

FieldTypeRequiredDescription
budgetsarray of stringYesBudgets

Example request:

curl -X POST "http://localhost:4000/budget/info" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"budgets": [
"free-tier",
"pro-tier"
]
}'

Responses:

StatusDescriptionBody
200The budgets that exist among the ids sentapplication/json array of Spanway_BudgetTableFull
400The request breaks a rule named in the detailapplication/json ErrorDetail
401No valid key was sent, or the key belongs to someone who is not a proxy adminapplication/json ProxyError
422Validation Errorapplication/json HTTPValidationError
500The gateway has no database connectedapplication/json ErrorDetail

GET /budget/settings​

Show the editable fields of a budget

List every field a budget can carry, with its type, description and the value stored for budget_id.

The Admin UI uses this to build the edit form. An unknown budget_id returns the fields with empty values. Proxy admins and admin viewers only, a different role or a gateway without a database is a 400.

Docs (https://docs.spanway.ai/docs/proxy/budget_management)

Parameters:

NameInTypeRequiredDescription
budget_idquerystringYes

Example request:

curl -X GET "http://localhost:4000/budget/settings?budget_id=budget_id-123" \
-H "Authorization: Bearer $SPANWAY_API_KEY"

Responses:

StatusDescriptionBody
200One entry per editable field with its type, description and current valueapplication/json array of ConfigList
400The caller is not a proxy admin or admin viewer, or no database is connectedapplication/json ErrorDetail
401No valid key was sent, or the key belongs to someone who is not a proxy adminapplication/json ProxyError
422Validation Errorapplication/json HTTPValidationError

GET /budget/list​

List every budget

Return every budget as one array, which is what the Budgets page of the Admin UI shows.

Use GET /management/v1/budgets for a paged, sorted and filtered list. Proxy admins and admin viewers only, a different role or a gateway without a database is a 400.

Docs (https://docs.spanway.ai/docs/proxy/budget_management)

Example request:

curl -X GET "http://localhost:4000/budget/list" \
-H "Authorization: Bearer $SPANWAY_API_KEY"

Responses:

StatusDescriptionBody
200Every budget in the databaseapplication/json array of Spanway_BudgetTableFull
400The caller is not a proxy admin or admin viewer, or no database is connectedapplication/json ErrorDetail
401No valid key was sent, or the key belongs to someone who is not a proxy adminapplication/json ProxyError

POST /budget/delete​

Delete a budget

Delete the budget named by id and return the row that was removed.

Keys, end users, tags and projects that pointed at it keep their own spend but lose the shared limits, so move them to another budget first. A budget that an organization uses cannot be deleted, which is a 400 naming the organization, and an unknown id is a 404. Proxy admins only, any other role is a 400. With audit logging on, the deletion is written to the audit log.

Docs (https://docs.spanway.ai/docs/proxy/budget_management)

Request body (application/json, schema BudgetDeleteRequest):

FieldTypeRequiredDescription
idstringYesId

Example request:

curl -X POST "http://localhost:4000/budget/delete" \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"id": "free-tier"
}'

Responses:

StatusDescriptionBody
200The budget as storedapplication/json Spanway_BudgetTableFull
400The request breaks a rule named in the detailapplication/json ErrorDetail
401No valid key was sent, or the key belongs to someone who is not a proxy adminapplication/json ProxyError
403An admin viewer tried to change a budget, and admin viewers can only readapplication/json ProxyError
404No budget has that idapplication/json ErrorDetail
422Validation Errorapplication/json HTTPValidationError
500The gateway has no database connectedapplication/json ErrorDetail

Response fields (200):

FieldTypeRequiredDescription
budget_idstring or nullNoBudget Id
soft_budgetnumber or nullNoSoft Budget
max_budgetnumber or nullNoMax Budget
max_parallel_requestsinteger or nullNoMax Parallel Requests
tpm_limitinteger or nullNoTpm Limit
rpm_limitinteger or nullNoRpm Limit
tpd_limitinteger or nullNoTpd Limit
model_max_budgetobject or nullNoModel Max Budget
budget_durationstring or nullNoBudget Duration
allowed_modelsarray of string or nullNoAllowed Models
temp_budget_increasenumber or nullNoTemp Budget Increase
temp_budget_expirystring or nullNoTemp Budget Expiry
budget_reset_atstring or nullNoBudget Reset At
created_atstringYesCreated At
created_bystring or nullNoCreated By
updated_atstring or nullNoUpdated At
updated_bystring or nullNoUpdated By