> ## Documentation Index
> Fetch the complete documentation index at: https://docs.formal.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Control LLM Spend

> Price LLM tokens, then cap what each user spends with policies

## Overview

Formal estimates the cost of every LLM request from model prices you set. Each log carries the cost, and policies read it. Policies also see each user's spend and token count, so you can enforce budgets.

Each model price has its own currency. Formal never converts between currencies, so spend in each currency is totalled separately.

## Steps

### 1. Set Model Prices

Open [LLM Prices](https://app.formal.ai/llm-model-prices) in the Control Plane.

The **author** made the model; the **provider** serves it. For example, Anthropic authors Claude, while Anthropic and Cursor can both provide access to it.

* Click **Add model price** and pick the model. Known models fill in their **Author**.
* Leave the provider as **All providers** to set the author's default price, used on every provider.
* To price a provider differently, such as Anthropic models through Cursor, add a price for that provider. It replaces the author's default for that provider.
* Pick the **Currency** the prices are in. It defaults to USD, and known models fill in their USD list prices.
* Add aliases for names the model is requested under, such as `opus-4.1` in Cursor.

Formal matches a model price when the requested model contains its name or an alias. The longest match wins. Any price for the request's provider wins over the author's defaults, even a shorter match.

Formal has no built-in price list. **Seen in logs without a model price** lists models requested in the last 30 days that no model price matches. Click **Set price** to price one.

Changing a model's currency doesn't touch earlier logs. They keep the cost and currency they were recorded with.

**Verify:** send a request, then open its log. The LLM section shows `cost` and `currency`.

### 2. Add Advanced Prices

Expand **Advanced pricing** in the price dialog. Each empty field falls back to the base price.

| Field | Charged for |
| - | - |
| Cache read | Input tokens served from the prompt cache |
| Cache write | Input tokens written to the prompt cache |
| Reasoning | Output tokens spent on reasoning |

### 3. Enforce Budgets

Request rules read spend in `input.user.spend`. See [Spend object](https://docs.formal.ai/docs/guides/policies/evaluation.md#spend-object) for details.

| Window | Since |
| - | - |
| `today` | 00:00 UTC today |
| `this_week` | 00:00 UTC on Monday |
| `this_month` | 00:00 UTC on the first of the month |

Each window has `cost`, a map from currency code to amount, and `token_count`. For example, `input.user.spend.today.cost.USD` is today's spend in USD. A currency is absent until the user spends in it, so read it with `object.get`. `token_count` also counts unpriced models.

Spend refreshes every few minutes, so a burst of requests can exceed a budget slightly.

The following policy blocks a user's requests after $50 in a day or $500 in a month of USD-priced models:

```rego theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
package formal.v2

import future.keywords.if

daily_budget := 50

monthly_budget := 500

request := {
  "action": "block",
  "type": "block_with_custom_message",
  "message": sprintf("Daily LLM budget of $%d reached", [daily_budget]),
} if {
  object.get(input.user.spend.today.cost, "USD", 0) >= daily_budget
} else := {
  "action": "block",
  "type": "block_with_custom_message",
  "message": sprintf("Monthly LLM budget of $%d reached", [monthly_budget]),
} if {
  object.get(input.user.spend.this_month.cost, "USD", 0) >= monthly_budget
}
```

To cap tokens instead, compare `token_count`, for example `input.user.spend.this_week.token_count >= 5000000`.

**Verify:** lower a budget below your own spend, send a request, and confirm the block appears in logs.

### 4. Block Unpriced Models

`priced` is `false` when no price matches the request. `price_source` is `provider` for a provider's price, `author` for an author's default, or `none`.

The following policy blocks models without a model price, so new models need a price before use:

```rego theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
package formal.v2

import future.keywords.if

request := {
  "action": "block",
  "type": "block_with_custom_message",
  "message": sprintf("%s has no price yet. Ask an admin to set one.", [input.llm.model]),
} if {
  not input.llm.priced
}
```

## Troubleshooting

> [accordion] Cost is missing from logs
>
> **Cause:** neither a price for the request's provider nor a default for the model's author matches the request. **Fix:** price the model from **Seen in logs without a model price**.

> [accordion] A provider's cost ignores the author's default
>
> **Cause:** a provider's price replaces the author's default for that provider, including cache and reasoning prices. **Fix:** set every price you need on the provider's price, or delete it to use the default.

> [accordion] A budget policy never triggers
>
> **Cause:** the policy reads spend in the `response` rule, or the request has no Formal user. **Fix:** read `input.user.spend` in the `request` rule.

## Next Steps

> [card] [AI Usage](https://docs.formal.ai/docs/guides/ai-governance/usage.md)
>
> See which models and clients people use

> [card] [Policy Evaluation](https://docs.formal.ai/docs/guides/policies/evaluation.md#llm-resources)
>
> Every field LLM policies can read


## Related topics

- [LLM Gateway](https://docs.formal.ai/docs/guides/solutions/llm-gateway.md)
- [Govern AI Usage](https://docs.formal.ai/docs/guides/use-cases/govern-ai-usage.md)
- [What is Formal?](https://docs.formal.ai/docs/guides/getting-started/what-is-formal.md)
- [LLM](https://docs.formal.ai/docs/guides/core-concepts/resources/llm.md)
- [Policy Evaluation](https://docs.formal.ai/docs/guides/policies/evaluation.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.