# Attribution, TPD and budget alerts

**Last updated:** 2026-11-25T02:20:13-03:00 (UTC-3)

Additions to [budgets and TPM](/docs/features/budgets-and-tpm/), under the same flags:

- **Attribution:** charge a key's usage to a user, project or organization.
- **TPD:** a tokens-per-day limit.
- **Soft budget and alerts:** warn when usage reaches 80% or 100%, with or without blocking.

## Attribution to a user, project and organization

Besides `key`, `team`, `model` and `end_user` (see [budgets and TPM](/docs/features/budgets-and-tpm/#policies)), a policy can have `user`, `project` or `organization` as its subject.

- A **user** is a local gateway user.
- **Projects** and **organizations** are financial accounts created in the console. A project has an organization as its parent. Type and parent cannot change after creation.
- A **managed key** can be attributed to a user, a project and an organization. The user must be an active member of the key's team.

Attribution is done only by a global admin, with versioning and audit. Headers, claims, email and the request body **never** choose these subjects. A key without attribution gets no inferred financial subject.

How it works at admission:
- Every applicable policy must have balance (AND), including model, user, project and organization.
- Changing the attribution affects only future reservations; what was already charged keeps the previous subjects.
- A disabled account or a removed member **blocks** the call, instead of silently removing the ceiling.

Financial accounts do not isolate tenants and do not grant a role, access or entitlement. The console lists up to 500 accounts and 500 attributions.

## Tenant ceiling

With `organizations`, the subject `tenant` names a gateway [organization](/docs/features/organizations/) by its ID: one budget, TPM or TPD counter shared by every key of every team of that organization, read on the request path with the organization's model ceiling. It is a ceiling set by the global administrator; a team or key policy inside the organization still applies on top (every applicable policy must have balance). It is not the `organization` subject above, which is a financial account a key is attributed to.

```yaml
quotas:
  policies:
    - {id: acme-month, subject: tenant, subject_id: acme, budget_usd: "500", budget_period: month}
```

An [organization admin](/docs/features/organization-admins/) reads its tenant policies and cannot change them.

**When a team moves to another organization**, each tenant counter keeps what was charged to it. A request is charged to the organization its team is in when it is admitted, so the former organization's window keeps what the team spent there (it is not freed, and an exceeded budget stays exceeded), and the new organization's window counts the team from its first request after the move, never its past. The team's own policy, and those of its keys and users, have their own counters and run across the move unchanged. See [when a team moves](/docs/features/organizations/#when-a-team-moves).

## One policy for a list of subjects

With the operator flag `quota_subject_lists` (off by default; requires `budget_limits` or `tpm_limits`), a policy can name an explicit list of subjects instead of one, with `subject: subjects` and `subjects`:

```yaml
quotas:
  policies:
    - id: contractor-limits
      subject: subjects
      subjects:
        - {subject: key, id: key_contractor_a}
        - {subject: key, id: key_contractor_b}
        - {subject: user, id: user_ana}
        - {subject: membership, id: user_bob/team_core}
      budget_usd: "50"
      budget_period: month
      tpm: 20000
```

- **One counter per subject.** The limits are written once and apply to each listed subject separately, exactly as one policy per subject would: `key_contractor_a` and `key_contractor_b` each have their own 50 USD. A request is charged on the counter of every listed subject it belongs to; a request of `key_contractor_a` attributed to `user_ana` uses both counters, and each must have room. There is no shared counter for the list: to share one ceiling, use a team, a project or a [tenant](#tenant-ceiling).
- **Entries:** each `subjects[]` entry has `subject`, the kind below, and `id`, the existing key, team, user or project ID (`<user ID>/<team ID>` for a membership).
- **Kinds:** `key`, `team`, `user`, `project` and `membership`, mixed freely (a counter is identified by its kind and ID, so mixing is never ambiguous); 1–256 unique subjects; `subject_id` stays empty and a list has no per-child template.
- **Every listed subject must exist** when a global admin writes it (`422 unknown_quota_subject` otherwise). An [organization admin](/docs/features/organization-admins/) may list only its organization's keys, teams, users and memberships (`403 organization_forbidden`, checked in the write transaction before and after, as for one subject) and lists only the lists whose every subject is its own.
- **Status** shows one row per listed subject under the policy's ID; the Prometheus series `gateway_budget_remaining_usd{policy=...}` shows the tightest of them, once.
- **Audit:** the write is recorded under the organization every listed subject shares; a list spanning organizations is recorded under none, so no organization admin sees another organization's subjects in its audit view.
- **Console:** the **Subject** choice **A list of subjects (one counter each)** takes one `kind:id` per line.

## Membership: a user within one team

With the operator flag `membership_budgets` (off by default; requires `budget_limits` or `tpm_limits`), the subject `membership` names one user within one team, as `<user ID>/<team ID>`:

```yaml
quotas:
  policies:
    - {id: ana-in-core, subject: membership, subject_id: user_ana/team_core, budget_usd: "20", budget_period: month}
```

- It counts only the requests of keys of that team [attributed](#attribution-to-a-user-project-and-organization) to that user, on its own counter; the same user's requests through another team's keys do not count. A key without a user attribution is no membership's.
- The user must be an active member of the active team when a global admin writes the policy. An organization admin may write it when both the team and the user are of its organization and the user is a member of the team.
- **Console:** the **Subject** choice **Team member (a user within one team)**.

**Both flags:** they control creating these policies (YAML and API: `422 feature_disabled` while off). A policy created while a flag was on keeps being enforced after it is turned off, so a limit never disappears silently; delete it to remove it. These subjects are enforced by the PostgreSQL quota store. No migration; an older binary rejects the flags, `subject: subjects`, `subjects` and `subject: membership` in YAML and in stored policies: delete such policies before rolling back.

## Tokens per day (TPD)

`tpd` in a policy limits a subject's tokens per UTC day, alongside `tpm` and `budget_usd`; a policy can have any combination of the three.

```yaml
features:
  tpm_limits: true
  tpd_limits: true          # boot; requires tpm_limits
quotas:
  policies:
    - {id: estagiarios, subject: team, subject_id: interns, tpm: 20000, tpd: 500000}
```

- The reservation uses the same estimate as TPM, and the settlement returns to the day whatever was not consumed.
- When the limit is exceeded, the response carries `X-Gateway-Quota-Dimension: tokens_day` and a `Retry-After` until UTC midnight.
- Image, audio, video and rerank have no tokens: when a TPD applies, they are rejected.
- In the console, the policy editor has the **Tokens per UTC day (TPD)** field, and the status shows consumption, balance and when the day resets.

## Soft budget and alerts

```yaml
quotas:
  policies:
    - id: marketing
      subject: team
      subject_id: marketing
      budget_usd: "200"
      budget_period: month
      soft_budget: true            # warns, does not block
      alert_thresholds: [80, 100]
```

- **`soft_budget: true`** keeps reservation, settlement and history, but does not deny when the USD ceiling is reached. TPM and other hard budgets still deny.
- **`alert_thresholds`**: up to five unique integer percentages between 1 and 100. Without the list, there are no alerts.
- Both require a positive budget and a supported calendar period. Lifetime additionally requires its [independent flag and retained-history upgrade](/docs/features/budgets-and-tpm/#lifetime-budgets), with no temporal renewal. Week, quarter and year additionally require the [calendar budget flag and upgrade](/docs/features/budgets-and-tpm/#calendar-budgets-and-upgrade); alerts retain the selected period’s exact UTC window.

Each alert is recorded together with the balance change that triggered it, with the policy, subject, window, percentage, committed amount, ceiling, origin (reservation or settlement), request ID and trace ID. An alert happens once per policy, subject, window and percentage, even with concurrent requests. A later, smaller settlement does not clear the alert.

In the console, the alert can be acknowledged; the acknowledgment is audited and does not return balance.

These alerts stay in the console panel. For email, Slack or a webhook, use the [alert channels](/docs/features/alerting/).

## Limits

- The committed amount includes reservations that do not have an outcome yet: it is not an invoice or final spend.
- Turning off enforcement removes the ceilings; do not use that as a way to authorize someone.
- Before going back to an earlier gateway version, remove policies with subjects or fields that version does not know.

## Admin API routes

Every route below needs a console session or an admin Bearer token, the role's permission and, for writes, the `X-CSRF-Token` header; the full contract is in the [API reference](/docs/api/).

| Route | What it does |
| --- | --- |
| `GET /api/v1/admin/quota-accounts` | Lists the financial accounts (projects and organizations) |
| `POST /api/v1/admin/quota-accounts` | Creates a project or organization account |
| `PUT /api/v1/admin/quota-accounts/{id}` | Edits an account at its current version; type and parent never change |
| `GET /api/v1/admin/quota-attributions` | Lists the user, project and organization each managed key is attributed to |
| `PUT /api/v1/admin/quota-attributions/{key_id}` | Sets a key's attribution; it applies to future reservations only |
| `GET /api/v1/admin/quota-alerts` | Lists the budget alerts recorded when a threshold was crossed |
| `POST /api/v1/admin/quota-alerts/{id}/ack` | Acknowledges an alert; audited, and it returns no balance |

## See also

- [Budgets and TPM](/docs/features/budgets-and-tpm/).
- [Organizations](/docs/features/organizations/) and [access control](/docs/features/access-control/).
- [Alerts](/docs/features/alerting/).
