# Spending caps

A spending cap is a monthly limit, in US dollars, that you put on a project or
on one API key. When a cap would be crossed, the platform refuses the work
before it starts and does not charge for it. Use a cap to stop one project, or
one key that an agent or script holds, from spending more than you expect.

A cap is optional. With no cap set, only your balance limits what is spent.

## What a cap covers {#what-a-cap-covers}

A cap counts what is charged in the calendar month (UTC):

- **Inference**: chat and embedding requests.
- **Training runs**: starting, resuming and the time a run trains.
- **Search builds**: embedding a dataset into a [Search index](../models/playground-and-search.md#search).
- **Similarity scans**: embedding a dataset's items to
  [find near-duplicates and more](../datasets/similarity-scans.md).

A **project cap** counts what is charged to that project. An **API key cap**
counts what that key requested, the runs it started, the Search indexes it
built and the similarity scans it started. Work you do in the app, signed in as yourself, is not limited by a key's
cap, and it counts toward a project's cap like any other charge to the project.
Adding funds does not lift a cap. Both caps and your balance apply: a request is
served only if none of them refuses it.

Spend that happened before the platform began to record it per key is not in a
key's total. The `spent_since` field on a key says from when its total counts.

## Set a cap {#set-a-cap}

**Project cap.** Open the project's **Settings** and fill **Monthly spending
cap, US$**. Leave it empty for no cap. The screen shows the month's spend
against the cap as a bar.

**API key cap.** Open **API keys**. The **Monthly cap** column shows each key's
spend against its cap. Choose **Set cap** (or **Edit cap**), enter an amount and
choose **Save cap**, or **Remove cap** to take it off. You can also fill
**Monthly cap, US$ (optional)** when you create the key.

A cap is from $0.01 to $100,000, in cents at most. A point or a comma may
separate the cents. Text that is not such an amount saves nothing: the cap you
have stays until you save an amount or leave the field empty. Lowering a cap below this
month's spend is allowed; it takes effect on the next request.

Only a person can set or change a cap. An API key, even with every scope,
answers `403 person_required` with a `url` for the screen: a key cannot lift the
limit that bounds it. Through the API:

```text
PATCH /api/v1/keys/{key_id}/cap
{"monthly_cap_usd": "25.00"}

PUT /api/v1/projects/{owner_slug}/{project_number}
{"monthly_cap_usd": "100.00"}
```

`null` removes a cap. On the key route the field is required (an empty body
answers `422`). On the project route, leaving the field out keeps the cap.

## What happens at the cap {#at-the-cap}

The cap is checked before the work starts, and the most the work can cost is
counted against it, when a request, a run or a Search build starts (and when a run
resumes). The platform checks that the month's spend, plus the most that other
running work can still cost, plus the most this work can cost (the same ceiling
the balance check uses) fits under the cap, and counts that amount in the same
step. Two requests that start at the
same moment cannot both take the last room under a cap: one is served and the
other is refused. Work that lands exactly on the cap is served.

When the work is charged, the amount counted is replaced by what the work really
cost, and the rest stops counting the moment the work ends, whether it finished,
failed, was refused while a model warmed up or was busy, or was cancelled. A
refused request is **not charged**, and nothing is counted for it. **A cap is
never exceeded:** the month's spend never goes past it. Requests are served again
from the 1st of the next month (UTC) or as soon as you raise the cap. Counting an
amount against a cap does not touch your balance.

**Work that runs past the end of a month.** A charge counts in the month it is
made in. A run that started on the 30th and is still training on the 1st is
charged in the new month for the time it trains then, so the most it can still
cost also counts against the new month's cap until it ends. Early in a month, a
cap can therefore refuse new work while a run from the previous month is still
going; the room comes back as that run is charged or ends.

The refusal is `429`:

- Inference answers with `type` `insufficient_quota` and `code`
  `key_cap_reached` or `project_cap_reached`, with no `Retry-After` header,
  because retrying does not help until the cap changes or the month ends.
- Other `/api/v1` routes (runs, Search) answer
  `{"detail": {"code": "key_cap_reached" | "project_cap_reached", "message": "..."}}`.

The message names the cap amount and never a key or project name. When both
caps would refuse, the order of checks is: access (`404`), balance (`402`), the
key cap, then the project cap. The Playground shows the refusal as a refusal,
with the cap amount and the date requests resume.

- **A run** counts the most it can cost against the cap when it starts or resumes,
  and is never paused or stopped by a cap. Until it finishes, pauses or is
  cancelled, that amount counts against the cap, so other work that would not fit
  beside it is refused. What the run does not spend stops counting when it ends. See
  [Training runs](../training/jobs.md#pause-resume-finish-and-cancel).
- **A Search build** counts its spending ceiling against the cap when you start it and runs to
  the end inside it. If you set a cap on a key or project after a build started,
  and the month's spend is already at that cap, the build ends `failed` with
  `failure_code` `key_cap_reached` or `project_cap_reached` at its next batch.
- **A similarity scan** counts the spending ceiling you set against the cap when you start it,
  and never crosses that ceiling. A scan that would cross a cap is refused before
  it starts and writes nothing. If a cap is set after a scan started and the
  month's spend is already at it, the scan ends `failed` with `failure_code`
  `key_cap_reached` or `project_cap_reached` at its next batch. See
  [Similarity scans](../datasets/similarity-scans.md#price-and-ceiling).

## Read a cap from the API {#read-a-cap}

Every key row carries `monthly_cap_usd`, `spent_this_month_usd`, `spent_since`,
`cap_reached` (the server's own comparison, never a client sum) and
`cap_resets_at` (the 1st of next month, 00:00 UTC). A project read carries the
same five fields; the projects list carries none. `monthly_cap_usd: null` means
no cap is set.
