> ## 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 token prices you set. Each log carries the cost, and policies read it. Policies also see what each user has spent, so you can enforce budgets.

All amounts are in USD.

## Steps

### 1. Set Token Prices

Open [Token Pricing](https://app.formal.ai/llm-pricing) in the Control Plane.

* Set a provider default to price every model of that provider.
* Click **Add model price** to price one model. A model price overrides the provider default.
* 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.

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.

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

### 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.llm`. See [LLM resources](https://docs.formal.ai/docs/guides/policies/evaluation.md#llm-resources) for every field.

| Field | Spend |
| - | - |
| `user_spend_usd_daily` | Since 00:00 UTC today |
| `user_spend_usd_weekly` | Since 00:00 UTC on Monday |
| `user_spend_usd_monthly` | Since 00:00 UTC on the first of the month |

Spend refreshes every few minutes, so a burst of requests can exceed a budget slightly. A field is absent when nothing was spent, so read it with `object.get`.

The following policy blocks a user's requests after $50 in a day or $500 in a month:

```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.llm, "user_spend_usd_daily", 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.llm, "user_spend_usd_monthly", 0) >= monthly_budget
}
```

**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 `model`, `provider_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 {
  input.llm.price_source != "model"
}
```

Use `not input.llm.priced` instead to accept provider defaults.

## Troubleshooting

> **Cost is missing from logs**
>
> **Cause:** no model price or provider default matches the request. **Fix:** price the model from **Seen in logs without a model price**.

> **A budget policy never triggers**
>
> **Cause:** the policy reads the spend field directly, and it is absent before any spend. **Fix:** read it with `object.get(input.llm, "user_spend_usd_daily", 0)`.

## Next Steps

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

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


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