
# Budget alerts

Get an alert each time spend crosses one of a budget's thresholds, in the dashboard and, if you set it up, by email or in Slack.

**Dashboard**

1. Choose a workspace at the top of the sidebar, or **All workspaces** for every workspace you can see.
2. Open **Alerts**. The badge on its icon counts your unread alerts, and, for an organisation Owner or Admin, each model serving calls with no price.
3. On the **Inbox** tab, choose **Unread** or **All**. A model serving calls with no price is listed above the alerts, with **Set price**, until a price covers it; see [Price calls made with no price](/gateway/spend-and-usage#price-calls-made-with-no-price). It is not sent by email or to Slack.
4. On an alert, choose **View spend** to open what spent on **Spend & budgets**, or **Mark read**. **Mark all read** clears the list.

You need: an account that can see the budget, such as an organisation Owner or Admin, or a Workspace admin. Connections and their deliveries need an organisation Owner or Admin. Changing a connection can ask you to confirm it's you.

**HTTP API**

```bash
curl -sS "$TOKEDEX_URL/control/v1/alerts" \
  -H "Authorization: Bearer $TOKEDEX_ADMIN_KEY"
```

```json
{
  "object": "list",
  "data": [
    {
      "object": "spend_alert",
      "id": "alr_01M7V92A…",
      "read": false,
      "crossed": [50, 100],
      "percent": 158,
      "exhausted": true,
      "spent": { "amount": "0.000158", "currency": "USD" },
      "budget": { "id": "bud_01M7V91X…", "window": "month", "limit": { "amount": "0.000100", "currency": "USD" }, "thresholds": [50, 100] },
      "affected": { "kind": "key", "id": "01M7V8Z2…", "name": "research-app", "workspace": { "id": "ws_01M7V8K7…", "name": "Research" } }
    }
  ]
}
```

Each alert names the budget, the thresholds crossed and what was spent; `exhausted` is `true` once the budget is used up. Add `?workspaceId=<workspace-id>` for one workspace's alerts.

You need: an admin key that can read budgets, such as one with the `viewer` or `finance` preset, in `TOKEDEX_ADMIN_KEY`, and `export TOKEDEX_URL='http://127.0.0.1:4000'`.

`GET /control/v1/alerts/unread-count` returns `count`. To mark alerts read, send `{ "all": true }` or `{ "alertIds": ["<alert-id>"] }` to `POST /control/v1/alerts/read`; read state belongs to each person or key.

An alert only warns: the budget itself decides what happens once it is used up. To set one, see [Budgets](/gateway/budgets).

## Alert thresholds

A budget's thresholds are percentages of its limit, chosen under **Alert at** when you set it. A budget set with **+ Set budget** starts with 80% and 100%; one set elsewhere alerts at 50%, 75%, 90% and 100%. A budget with no thresholds raises no alerts, and the **Playground**'s daily allowance raises none. On a budget that refuses requests, the default, add an earlier threshold, such as 50%, so whoever is told has time to act before requests are refused. 80% and 100% are enough for a budget set to **Only alert**.

- Each threshold alerts once per period, or once in all for a budget in total. Spend that passes several at once raises one alert that lists them.
- A raised limit alerts again only past what was already reported: a used-up $10 daily budget raised to $20 alerts next at $15 (75%). A lowered limit alerts on the thresholds it newly crosses.
- The gateway checks budgets every 15 seconds. Spend that settles just after a period ends can raise that period's alerts for about an hour.
- Calls [priced after they were made](/gateway/spend-and-usage#price-calls-made-with-no-price) add their cost to the periods they were made in. In the current period that cost counts toward its thresholds; a period that has ended alerts no more.

## Alert visibility

You see an alert only when you can see both its budget's limit and what was spent.

| Role | Alerts they see |
| --- | --- |
| Organisation Owner, Admin or Viewer | Every alert |
| Workspace admin or Workspace viewer | In their workspaces: the workspace's and models' budgets, and every key's and person's |
| Developer | In their workspaces: the workspace's and models' budgets, their own keys' and their own |

The people a budget tells, chosen under **Who is told** on the budget, see its alerts unread; anyone else who can see it finds them under **All**, already read. Connections send alerts whoever a budget tells. Tell the people who can act, such as someone who can raise the limit or the key's holder, since only they see the alert unread.

The gateway keeps alerts for 180 days. An alert older than 30 days counts as read.

## Send alerts by email

Email needs an SMTP server in the gateway's environment, which whoever runs the gateway sets. Until then, **Create email connection** is unavailable.

| Variable | Value |
| --- | --- |
| `TOKEDEX_SMTP_URL` | The server, as `smtp://[<user>:<password>@]<host>[:<port>]`, or `smtps://…` for TLS from the first byte. Percent-encode the user name and password. |
| `TOKEDEX_SMTP_HOST`, `TOKEDEX_SMTP_PORT`, `TOKEDEX_SMTP_USER`, `TOKEDEX_SMTP_PASSWORD` | The same server in parts, in place of `TOKEDEX_SMTP_URL`, never with it. The user and password go together. The port defaults to `587`. |
| `TOKEDEX_SMTP_FROM` | The sender, as `alerts@example.com` or `Spend Alerts <alerts@example.com>`. Required with a server. |
| `TOKEDEX_SMTP_TLS` | `starttls`, which fails if the server cannot upgrade; `implicit`; or `none`, which allows a user and password only to a loopback host. Defaults to `implicit` for `smtps://` or port 465, otherwise `starttls`. |

On Kubernetes, set them under `extraEnv` in the Helm chart's values, taking the password, or a URL that holds one, from a Secret as [Deploy to production](/gateway/deploy) shows for provider keys. For a local gateway, set them in the repository's `.env` file. Restart the gateway after a change: a malformed value, a server without a sender, or a `TOKEDEX_SMTP_*` setting without a server stops it from starting.

To try email without a mail server, use Mailpit, which catches every message and shows it at `http://127.0.0.1:8025`: add `COMPOSE_PROFILES=mail`, `TOKEDEX_SMTP_URL=smtp://mailpit:1025`, `TOKEDEX_SMTP_TLS=none` and `TOKEDEX_SMTP_FROM=alerts@example.com` to `.env` and run `docker compose up -d`. For a gateway started with `npm start`, use `smtp://127.0.0.1:1025`, run `docker compose --profile mail up -d mailpit` and restart the gateway.

A gateway has at most 10 connections.

**HTTP API**

Connections, test sends and deliveries are managed only from a signed-in dashboard session, by an organisation Owner or Admin: their routes answer `session_required` to an admin key.

**Dashboard**

1. Open **Alerts → Delivery** and choose **Create email connection**.
2. Enter a **Name**, and choose which alerts it sends under **Send alerts at**, **About** and **Also send alerts from per-key default budgets**.
3. Under **People**, choose people on the gateway. Each gets only the alerts they could see in their own inbox.
4. Under **Other addresses**, enter outside addresses, one per line. Each gets every alert the connection sends.
5. Choose **Create email connection**, then **Send test**.

The connection appears under **Connections** and sends alerts raised from then on, one copy per address. Prefer **People** to **Other addresses** where you can, because each person then gets only what they may see on the gateway.

## Post alerts to Slack

**Dashboard**

1. Open **Alerts → Delivery**, choose **Create Slack connection**, then **Webhook URL**.
2. Choose **Create a Slack app ↗**. In the form Slack fills in, create and install the app and pick the channel.
3. Copy the webhook URL from the app's **Incoming Webhooks** and paste it under **Slack webhook URL**.
4. Enter a **Name** after the channel, such as `#finance-alerts`: Slack does not say where a webhook posts.
5. Choose which alerts it sends, then **Create Slack connection** and **Send test**.

The test message appears in the channel. Everyone there reads every alert the connection sends, whatever they can see on the gateway, but no secret or prompt text, so post to a channel limited to people who may see spend. Anyone with the webhook URL can post to the channel, so the gateway never shows it again.

To pick the channel in Slack instead, choose **Slack app**, which needs the gateway's public `https` address.

If deliveries read "Gave up: Slack no longer accepts this webhook", choose **Reconnect** on the connection's card and pick the channel again. Where **Reconnect** does not show, **Edit** the connection and paste a new webhook URL.

**HTTP API**

Slack connections are made only in the dashboard, by an organisation Owner or Admin.

## Test a connection

**Dashboard**

Choose **Send test** on the connection's card. Each recipient, or the Slack channel, gets a message marked as a test.

A connection takes one test every 30 seconds across all replicas, or another at once after a change.

## Check deliveries

**Dashboard**

**Edit → Recent deliveries** shows what a connection sent, and **Sent to** on an alert in the inbox shows where it went.

A failed send is retried for up to 10 tries or 24 hours and reads **Retrying** meanwhile. Slack can specify a minimum wait before the next attempt. If the delivery expires first, the gateway gives up.

| You see | What to do |
| --- | --- |
| Gave up: Slack no longer accepts this webhook | [Reconnect the Slack connection](#post-alerts-to-slack) |
| The mail server refused it, or was unavailable | Ask whoever runs the gateway to check its mail settings |
| Skipped: the person may no longer see this alert | Nothing: their access changed after the alert was raised |
| Cancelled: the connection is turned off, or was deleted | Select **Send alerts through this connection** under **Edit**; later alerts are sent, cancelled ones are not |

The **Audit log** records every connection change and test send.

Next: [Budgets](/gateway/budgets) or [Spend and usage](/gateway/spend-and-usage).
