AI cost management for LLM traffic

Formal estimates the cost of each LLM request from the token prices you set and records it under the person who sent it. Request policies read that user's spend for the day, week and month, in cost or in tokens, and block the next request once a budget is reached.

policy · llm budgetexample
# request rule: $50 a day, $500 a month (example values)
AGENT  claude-cli   maya@acme.com
ALLOW  llm  claude-sonnet-4-6, today under budget
BLOCK  llm  Daily LLM budget of $50 reached

The problem

Why do LLM costs need a cap per person?

A coding agent stuck in a loop keeps calling the model, and the provider counts every token against an API key, often one a whole team shares. AI cost management has to act on the request itself, while the spend is still small and you still know who sent it. Formal puts an estimated cost on each LLM request as it passes through, under the person who sent it, and a request policy can refuse the next one once that person’s daily, weekly or monthly budget is spent.

  1. Agents that loop overnight

    A coding agent stuck on a failing test keeps calling the model until someone stops it, and a script left running over a weekend does the same.

  2. One API key shared by a team

    The provider counts those tokens against a key the whole team uses, so no request on the bill has an owner, and the invoice arrives after the spend.

  3. New models nobody priced

    When a provider ships a new model and an agent switches to it, a budget counted in dollars has nothing to add up.

today · no policy on the pathexample
01
claude-cli · mayaPOST api.anthropic.com/v1/messages, retry on a failing testALLOWED
02
shared API keyPOST api.anthropic.com/v1/messagesALLOWED
03
cursor · leomodel new-model-x, no price setALLOWED

# Without a check on the request, all three go through.

Definition

What is AI cost management for LLM traffic?

AI cost managementnoun

AI cost management for LLM traffic means pricing each request sent to a model provider, attributing its cost to the person or agent behind it, and enforcing a budget before the next request goes out. LLM cost management and AI spend tracking are other names for the same job.
  • Most tools start from the bill

    They import invoices and usage exports from cloud and model providers, then allocate the total to teams for showback or chargeback. Finance needs that view, and it arrives after the money is spent.

  • A cost on each request

    A cost attached to each request answers a different question: whose agent is burning through today’s budget right now, and should its next request go through?

  • Checked where the request already passes

    Formal runs the check in the Formal Endpoint on laptops and in Pods, or in a Connector in your VPC. Interception itself is covered on the AI gateway page; this one starts once the request is in the proxy.

How it works

How to set up per-user LLM budgets

Docs for each step: LLM spend, LLM resources, the spend object and AI usage.

  1. Step 1 of 4

    Set token prices

    Open LLM Prices in the Control Plane and add a price per model. Known models fill in their author and USD list prices, which you check against what you pay.

    LLM PRICESexample
    • claude-opus · author anthropic · all providers✓
    • claude-opus · provider cursor · alias opus-4.1✓
    • list prices checked against the contract✓
  2. Step 2 of 4

    Capture LLM traffic

    Turn on Transparent Mode on macOS or Linux laptops, run the Endpoint as a Pod sidecar, or point server-side apps at a Connector with a Formal user on each request.

    WHO GETS CHARGEDexample
    • claude-cli · maya-mbpMAYA@ACME.COM
    • agent pod · native sidecarSIDECAR IDENTITY
    • support app · X-Formal-User-* headersFORMAL USER
  3. Step 3 of 4

    Write a budget policy

    Add a request rule that reads input.user.spend and start it in Dry-run. To test it, lower a budget below your own spend and look for the block in Logs.

    REQUEST RULEexample
    • today's spend under budgetALLOWED
    • budget lowered below your own spendDRY RUN
    • Daily LLM budget of $50 reachedBLOCKED
  4. Step 4 of 4

    Review spend and unpriced models

    Price whatever appears under Seen in logs without a model price, and use the AI Spend and AI Usage dashboards to see where the requests go.

    EACH WEEKexample
    • unpriced models priced✓
    • AI Spend dashboard reviewed✓
    • AI Usage dashboard reviewed✓

Budget policy

Cap AI spend with per-user budgets

Before Formal forwards an LLM request, a request rule can read input.user.spend, the sender’s usage in three UTC windows (spend object). This policy from the spend docs blocks a user after $50 in a day or $500 in a month of USD-priced models. The amounts are the docs’ example values; pick your own.

  • today starts at 00:00 UTC today, this_week at 00:00 UTC on Monday, this_month at 00:00 UTC on the first of the month
  • Each window holds cost, a map from currency code to amount, and token_count, the input and output tokens of every request with cache and reasoning tokens included
  • Capping tokens is a comparison on that count, such as input.user.spend.this_week.token_count >= 5000000, and it includes models you haven’t priced
  • The object.get is there because a currency is absent from cost until the user spends in it
  • Spend refreshes every few minutes, so an agent firing requests in parallel can go slightly past the budget before the block lands. Plan for a small overshoot
Control LLM spend ›
llm-budget.regorego
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
}

Capabilities

Price, cap and gate LLM spend

What happens to a request between the agent and the provider, and what you see afterwards.

LLM prices

Price each model where it's served

Formal has no built-in price list. In LLM Prices you add a price per model, and for known models the dialog fills in the author and the USD list prices, which you then check against what your contract says (LLM spend docs). The author made the model and the provider serves it: Anthropic authors Claude, and both Anthropic and Cursor serve it.

  • Defaults: leave the provider on All providers and the price is the author’s default everywhere. A Cursor price replaces that default for Claude requests that go through Cursor, cache and reasoning prices included
  • Aliases: cover the names a model gets requested under, such as opus-4.1 in Cursor
  • Matching: a price applies when the requested model name contains the price’s name or one of its aliases, and the longest match wins. Any price for the request’s provider beats the author’s defaults, even with a shorter match
  • On the log: price_source says which price matched: provider, author or none
llm prices · control planeexample
admin> which price matches opus-4.1 via cursor
priceprovideraliases
claude-opusAll providers
claude-opuscursoropus-4.1
2 rows · policy price_source providerALLOWED
Per-user budgets

Block a user once the day's budget is spent

The request rule reads the sender’s spend before Formal forwards the request. Past the budget, the agent gets your message instead of a model reply. A budget applies to a Formal user, and everything about who that user is comes along in the same input.

  • Groups: input.user.groups and input.user.type (human or machine) sit next to the spend, so one policy can give a group a higher daily number or give machine users their own budget. The docs have no example for that, but it’s the budget comparison with a group check in front
  • Request rule only: spend exists only in the request rule. The response rule sees input.llm.cost, the estimated cost of that one request, which is useful in logs and too late to stop anything
  • No user, no budget: a request with no Formal user, such as an app calling a Connector without X-Formal-User-* headers, has no spend for the rule to read, so the budget stays silent
policy · llm budgetexample
todayclaude-cli for mayaclaude-sonnet-4-6, under budgetALLOWED
todayclaude-cli for mayaDaily LLM budget of $50 reachedBLOCKED
Dashboards

AI usage monitoring dashboards

Formal’s dashboard templates, under Observability, are built from proxied traffic and only read data. Templates have fixed layouts: click Edit and Formal saves your changes as a new dashboard, where you add widgets with a Source, a Group By field and a Visualization (AI usage docs).

  • AI Spend: the request volume behind spend, by model, provider and client
  • AI Usage: who uses AI, with which clients and models, and how much
  • Model Adoption: how quickly people move to new models and providers, which is also when a price tends to be missing
  • Cost per person: the log itself is the record: each LLM log carries the user, model, provider, cost and currency, and log integrations forward it to Datadog, Splunk or S3
AI Usage templateexample
security team> requests by user, client and model
userclientmodel
maya@acme.comclaude-cliclaude-sonnet-4-6
leo@acme.comcursoropus-4.1
2 rowsALLOWED
API

Token prices as code

Prices live in the Control Plane, and core.v1.LLMPricingService lists, sets and deletes them (Set LLM token price). When a provider contract changes, a script can update every affected model in one run instead of someone editing prices by hand.

  • Fields: provider (* for the author’s default), author, model, aliases and currency
  • Per-million prices: inputPerMillion, outputPerMillion, cacheReadPerMillion, cacheWritePerMillion and reasoningPerMillion
llm pricing apiexample
pricing scriptset price: provider *, author anthropic, model claude-opusALLOWED
pricing scriptset price: provider cursor, model claude-opus, aliases opus-4.1ALLOWED

Price table

LLM cost tracking per user, model and provider

Cost starts with a price table you own. Each price is set per million tokens, with its own currency, USD by default.

  • Input, output

    Base prices per million tokens.

    Base
  • Cache read

    Input tokens served from the prompt cache. Coding agents resend the conversation on every turn, so much of their input comes from the cache, and with no cache read price those tokens are charged at the full input rate in your estimate.

    Advanced
  • Cache write

    Input tokens written to the prompt cache.

    Advanced
  • Reasoning

    Output tokens spent on reasoning.

    Advanced
  • Currency

    Each price has its own currency, and Formal never converts: euros and dollars are totalled separately, and earlier logs keep the currency they were recorded with.

    Per price
  • A missing cost

    To check a price, send one request and open its log: the LLM section shows cost and currency. A missing cost means the model matched no price, and the model then shows up under Seen in logs without a model price, the list of models requested in the last 30 days that nothing prices.

    Unpriced

Empty advanced fields fall back to the base price.

Unpriced models

Block models nobody has priced

A model without a price has no cost on its logs, so it never adds to anyone’s cost window. When a provider ships a new model and an agent switches to it, a dollar budget quietly stops counting. Either cap token_count, which counts every model, or block unpriced models outright with this rule from the docs.

  • input.llm.priced is false when no price matches the request
  • We’d run this rule in Dry-run for a week first and price whatever lands under Seen in logs without a model price. Switched on cold, it blocks every model nobody has got to yet, including ones people use daily
  • An unpriced model is often one nobody reviewed, too. Shadow AI detection covers the MCP servers and skills agents add on their own
LLM spend docs ›
block-unpriced-models.regorego
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
}

Request path

Why enforce spend on the request path?

Enforcing spend on the request path means the budget check runs before the request reaches the model provider, with the sender’s identity attached. The Endpoint knows a Claude Code, Cursor or Codex request from the agent’s signed binary (on Linux, from its process name), and a forgotten script on a server goes through a Connector with the user in its headers; our Claude Code and AI coding agent security pages cover what else a policy sees on a laptop. The request stage that reads spend is the one that rewrites prompts for AI data loss prevention. OWASP files runaway usage under LLM10:2025 Unbounded Consumption, including denial of wallet, and lists user quotas among the mitigations. Formal’s Insights review new LLM logs every 15 minutes and flag runaway token or request usage under LLM10 without blocking anything: a finding for the pattern your budget numbers didn’t anticipate. Our LLM security guide maps the other nine categories.

Invoice reportRequest-path check
When it actsAfter the money is spentBefore the next request reaches the provider
What it namesThe API key that spent the money last monthThe Formal user who sent the request, and on the Endpoint the agent
What it can stopNothing: it reportsThe next request from one engineer's Claude Code session once that engineer's budget is gone, on the same day the loop started
Where it's recordedUsage exports, split by keyEvery block, rewrite and cost in the same log, under the same person

The limits

What Formal's spend tracking doesn't cover

Formal works from your price table, so its totals are an estimate. Keep the provider invoice as the record of what you paid.

  • Seat and plan fees

    They never cross the proxy, so they aren’t in the estimate, and neither is anything a provider bills for traffic Formal didn’t see.

    Not included
  • Routing and fallback

    Formal doesn’t route a request to a cheaper model or fall back to another one when a budget runs out; the request is blocked with your message.

    Not supported
  • Currency conversion

    There’s no currency conversion: each currency is totalled on its own.

    Not supported
  • Team or agent totals

    Formal doesn’t document a shared total for a team or an agent, so a team limit today means a per-person limit for everyone in the group.

    Per person only
  • Windows laptops

    The Endpoint has no Transparent Mode on Windows yet, so their LLM traffic isn’t captured (Windows docs). A server-side app on any OS can call a Connector instead, and its requests are priced the same way.

    Not yet

Works with

Works with the agents and apps that spend tokens

The same prices on every machine that sends LLM traffic. On macOS and Linux, Transparent Mode hands Claude Code and Cursor traffic to the Endpoint, which logs each request’s cost under the developer’s Formal user. In Kubernetes, the Endpoint runs next to the workload as a native sidecar, and the Pod’s LLM requests count toward the identity that sidecar signs in as. Server-side apps call a Connector and send X-Formal-User-* headers, so each request has a Formal user to charge.

Monitor AI usage ›

Deployment

Budgets on the identity provider and IaC you already run

Where budgets are enforced: on the device or in your VPC.

Formal Endpoint on laptops

Transparent Mode on macOS and Linux; roll it out to Macs with your MDM. Existing clients keep working without a base URL change (docs).

Formal Endpoint in pods and tasks

A native sidecar in Kubernetes Pods, injected by label with the Helm chart, and a sidecar container in Amazon ECS tasks (docs).

Formal Connector in your VPC

Linux on AMD64 or ARM64: AWS ECS Fargate behind a Network Load Balancer, or Helm on EKS, GKE, AKS or on-premises Kubernetes. In production, run 2 or more nodes behind a load balancer (docs).

Your identity provider

People sign in with SSO, and SCIM keeps users and groups in sync. Policies use the groups you already manage.

Logs in your SIEM

Log integrations forward Formal logs to AWS S3, Google Cloud Storage, Azure Blob Storage, Datadog, Splunk or Panther.

Policy as code, audited controls

Policies are written in Rego. Manage Formal with Terraform, Pulumi or the Kubernetes Operator. Formal holds SOC 2 Type II and ISO/IEC 27001; the reports are on security.formal.ai.

In production

Technologies secured
10+
Resources secured
1,000+
Logs per week
40M+
Sessions per week
600K+
“Without Formal, we couldn't have agents with access to sensitive data. With Formal, because we are able to inspect all traffic and enforce controls, we are able to provide a safe environment for the agents to get the job done.”
Travis McPeak
Travis McPeak
Head of Security, Cursor
Read the Cursor story ›

FAQ

Questions about AI cost management

Buying and fit

How do you manage AI costs?

Price usage where it happens, assign it to a person, and set a limit that acts before the bill does. For LLM traffic that means a price per model and provider, a cost on every request under the Formal user who sent it, and a daily or monthly budget in a request policy. Check the list of unpriced models every week: a model with no price adds nothing to anyone’s budget.

What is the best AI cost tracking tool?

The one that sees the traffic your money goes through. Ask four questions. Does it attribute each request to a person or agent rather than to an API key? Can it block a request at a budget, or only report on it? Can it price the same model differently per provider? Does it see coding agents on laptops as well as the apps you build? AI spend management tools for SaaS licenses and expenses answer a different question, which is what you pay per seat.

Product

How do I track Claude Code costs per developer?

Run Claude Code through the Formal Endpoint. On macOS and Linux, Transparent Mode intercepts its traffic to the Anthropic API with no base URL change, and each log carries the developer’s Formal user and the cost estimated from your Anthropic prices. A request policy can then cap each developer’s daily spend. Setup is on the Claude Code integration page. Claude Code cost tracking on Windows laptops isn’t available yet.

Can I cap tokens instead of dollars?

Yes. Each spend window has a token_count: input plus output tokens, cache and reasoning tokens included, across every model, priced or not. Token usage tracking needs no price table, so a weekly token cap is a reasonable first budget while you’re still setting prices.

What happens when a user hits the budget?

Formal blocks the request before it reaches the provider and returns your custom message, such as “Daily LLM budget of $50 reached”. The block lifts when the window resets: 00:00 UTC for a daily budget, Monday for a weekly one, the 1st of the month for a monthly one. For an exception, an admin can grant a time-bound policy suspension, the mechanism behind just-in-time access.

Does Formal count ChatGPT, Claude or Cursor seat subscriptions?

No. Seat and plan fees never cross the proxy, so they aren’t in the estimate, and neither is anything a provider bills for traffic Formal didn’t see. On a flat-rate plan, the token cost still compares usage across people even though it isn’t what you pay, and a token budget still limits how much one person can use.

Put a budget on every LLM request

Token prices you own, spend under each person, and a block when the budget runs out.