
# Budgets

Set a budget in US dollars and choose whether the gateway refuses new requests or only alerts when spend reaches the limit.

**Dashboard**

1. Choose a workspace at the top of the sidebar, or **All workspaces** to cap a person or a model everywhere.
2. Open **Spend & budgets** and choose **+ Set budget**.
3. Under **What to cap**, choose **A key**, **A person** or **A model**, then pick one.
4. Enter the **Limit (USD)** and choose **a day**, **a week**, **a month** or **in total**.
5. Under **When it is used up**, keep **Refuse new requests**, or choose **Only alert** for a production service whose outage would cost more than the overspend.
6. Choose **Alert at** and **Who is told**, check the summary, then choose **Set budget**.

The budget appears under **Budgets that apply here**.

![The Spend & budgets page showing spend against a monthly budget and spend per day by key](/images/dashboard/spend-budgets.png)

You need: an organisation Owner or Admin account, or a Workspace admin account for a person or a model in your workspace.

**HTTP API**

```bash
curl -sS "$TOKEDEX_URL/control/v1/budgets" \
  -H "Authorization: Bearer $TOKEDEX_ADMIN_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "target": { "kind": "key", "id": "<key-id>" },
    "window": "month",
    "limit": "50"
  }'
```

The gateway answers `201` with the budget, its spend so far and when it resets:

```json
{
  "object": "budget",
  "id": "bud_01M7V…",
  "target": { "kind": "key", "id": "01M7V…" },
  "window": "month",
  "limit": { "amount": "50.000000", "currency": "USD" },
  "onLimit": "refuse",
  "alertThresholds": [50, 75, 90, 100],
  "progress": {
    "spent": { "amount": "0.000000", "currency": "USD" },
    "state": "ok",
    "resetsAt": "2026-12-01T00:00:00.000Z"
  }
}
```

You need: an admin key with the `admin` preset in `TOKEDEX_ADMIN_KEY`, and `export TOKEDEX_URL='http://127.0.0.1:4000'`.

`target.kind` is `key`, `user` (a person, by user ID) or `model` (a model ID). A person's or a model's budget holds in every workspace unless you add `"within": { "kind": "workspace", "id": "<workspace-id>" }`. `window` is `day`, `week`, `month` or `total`. `"onLimit": "alert"` lets requests continue; use it for a production service whose outage would cost more than the overspend. `alertThresholds` sets the alert percentages.

## Budget types

The gateway checks every budget that applies before it contacts a provider. If any of those budgets is spent and set to refuse new requests, the request stops there.

| Budget | Counts | Who sets it |
| --- | --- | --- |
| Key | One API key | Organisation Owner or Admin |
| Person | A person's own keys, in one workspace or all | Organisation Owner or Admin; a Workspace admin, in their workspace |
| Model | Requests that name the model ID, in one workspace or all | The same as a person's |
| Workspace | Everything spent in the workspace | Organisation Owner or Admin |
| Organisation | Everything spent in every workspace | Organisation Owner or Admin |
| Default per API key | Each key in a workspace or the organisation without its own budget for that period | Organisation Owner or Admin; a Workspace admin can set or lower their workspace's |

The **Playground** also has a fixed allowance of $1 a day per person.

Start with a monthly budget on each workspace, set above your largest expected month, and lower it once you have a month of spend. The spend report counts the same calendar months. Then set a default per API key to limit the impact of a runaway key.

For a period, prefer a day (HTTP API: `day`) for a key that runs unattended, such as a scheduled job or an autonomous agent loop, so a runaway loop is caught within a day, and in total (HTTP API: `total`) for a fixed allowance, such as a contractor's.

**HTTP API**

An admin key needs `budgets.manage` for key, person and model budgets, and `workspaces.manage` for the others. The `admin` preset holds both. A `workspace-admin` key can also set or lower its workspace's `keyBudgetDefault` on `PUT /control/v1/workspaces/<workspace-id>/rules`.

## Cap a workspace or the organisation

**Dashboard**

1. Choose the workspace, or **All workspaces** for the organisation, and open **Settings**.
2. Enter the amount in the workspace's monthly budget, such as **Default's monthly budget (USD)**, or **Organization budget (USD)**. **Default budget per API key (USD)** is beside it.
3. Choose **Save**. Adding or lowering a budget opens **Review before applying**; choose **Apply changes**.

A workspace's budget is monthly in **Settings**. Set another period over HTTP.

**HTTP API**

```bash
curl -sS -X PUT "$TOKEDEX_URL/control/v1/workspaces/<workspace-id>/guardrail" \
  -H "Authorization: Bearer $TOKEDEX_ADMIN_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "budget": { "window": "month", "limit": "500" } }'
```

Adding or lowering a budget first answers `409` with the change to review:

```json
{
  "error": {
    "code": "review_required",
    "message": "this change reduces access: review it, then send its digest as acknowledge",
    "review": {
      "budgetsLowered": [{ "path": "workspaces.default.budget", "before": null, "after": { "window": "month", "limit": "500.00" } }],
      "digest": "sha256:921cdd79…"
    }
  }
}
```

Send the same body again with `"acknowledge": "<digest>"` to apply it. The organisation's budget takes the same body on `PUT /control/v1/policy`, and `keyBudgetDefault` sets the default per API key on either route.

## Change or remove a budget

**Dashboard**

On **Spend & budgets**, choose **Edit** on the budget's row, make the change and choose **Save budget**, or choose **Remove budget** and confirm. For a workspace's or the organisation's budget, edit or clear the amount in **Settings** and choose **Save**.

**HTTP API**

Find the budget's `id` with `GET /control/v1/budgets`, filtered by `targetKind`, `targetId`, `window` or `workspaceId`, then change it:

```bash
curl -sS -X PATCH "$TOKEDEX_URL/control/v1/budgets/<budget-id>" \
  -H "Authorization: Bearer $TOKEDEX_ADMIN_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "limit": "80" }'
```

Or delete it, which returns `"deleted": true`:

```bash
curl -sS -X DELETE "$TOKEDEX_URL/control/v1/budgets/<budget-id>" \
  -H "Authorization: Bearer $TOKEDEX_ADMIN_KEY"
```

To remove a workspace's or the organisation's budget, send `"budget": null` to its route, with no review.

The spend a budget counted stays when you change or remove it. To change a budget's period or target, remove it and set a new one.

## When a budget is used up

Once the gateway sees that a budget set to refuse new requests is spent, it returns `402` before contacting a provider:

```json
{
  "error": {
    "code": "budget_exceeded",
    "message": "budget exceeded: the key budget for today (UTC) is spent ($1.000000 of $1.000000); it resets at 2026-11-21T00:00:00.000Z",
    "type": "insufficient_quota",
    "limit": { "scope": "key", "window": "day", "resets_at": "2026-11-21T00:00:00.000Z", "limit_usd": "1.000000", "spent_usd": "1.000000" }
  }
}
```

A refusal by a workspace, organisation or model budget leaves out the amounts. For a key or person budget whose amounts the caller can see, the gateway also checks the cost of the requested maximum output at the cheapest eligible price. If that cost exceeds the amount left, the gateway refuses the request.

The gateway checks settled spend before each request and charges calls as they finish. Three concurrent requests costing $1 each can all pass a $1 workspace budget check and settle at $3. A replica can also keep admitting requests until it sees spend recorded by other replicas. Use lower request concurrency and output limits with small budgets to reduce the possible overrun.

While a budget applies, a model with no known price is refused with `403` `price_unavailable` until an organisation Owner or Admin sets its price in **Pricing**. Set missing prices before you add a budget, so requests to those models keep working. Calls a model made with no price, under no budget that refuses it, can be [priced later](/gateway/spend-and-usage#price-calls-made-with-no-price): their cost then counts in the budget periods they were made in.

**HTTP API**

`"unknownPrice": "allow"` on a key, person or model budget lets models with no known price through it.

A successful response reports the tightest of the key's and its holder's own budgets as it stood when the gateway accepted the request. The reset timestamp is for that same period.

```text
x-tokedex-budget-remaining-usd: 0.500000
x-tokedex-budget-limit-usd: 1.000000
x-tokedex-budget-scope: key
x-tokedex-budget-window: day
x-tokedex-budget-reset: 2026-11-21T00:00:00.000Z
```

The scope is `key`, `member` (a person in one workspace) or `user` (a person in all). Workspace, organisation and model budgets never appear here. A budget set to **Only alert** never refuses and never appears in the response headers.

## Budget resets

Daily budgets reset at midnight UTC, weekly ones on Monday and monthly ones on the 1st, also at midnight UTC. A budget in total never resets and counts from when you set it; the others count everything spent in the current period, including before you set them.

The gateway counts each request's cost in the UTC period when it accepted the request, even if the request finishes after the reset.

> **Important:** Check the period's spend so far before you set a day, week or month budget. A limit below it refuses requests at once.

Next: [Budget alerts](/gateway/budget-alerts), [Read the spend report](/gateway/spend-and-usage#read-the-spend-report) or [People and workspaces](/gateway/people-and-workspaces).
