# Cost and usage

See spend and tokens by agent, model, provider, tenant and tag, forecast the month, and set the prices spend is computed from.

## Read the numbers

Open **AI Gateway** › **Cost and usage**. The headline shows active agents, requests and spend for the period. The activity table lists calls with these columns: When, Agent, What, Tokens, Cost and Outcome.

| Question | Endpoint |
| --- | --- |
| Totals for the period (active agents, requests, spend) | `GET /api/usage/headline` |
| Spend by agent, model, provider, tenant, channel, principal, tool or tag | `GET /api/usage/breakdown?dimension=agent` |
| Over time | `GET /api/usage/timeseries` |
| This period against the last | `GET /api/usage/compare` |
| Where the month will land | `GET /api/usage/forecast` |
| Recent calls | `GET /api/usage/activity` |
| Per-agent, provider and model rollups as CSV (`group_by=tenant`, `channel`, `principal`, `ticket_id` or `tool` adds chargeback rows) | `GET /api/usage/export` |

> [!TIP]
> Tag agents as `key=value` (for example `team=servicedesk`), then use `dimension=tag&tag_key=team` for chargeback.

## What context optimisation saved

Four techniques make what an agent sends smaller. `GET /api/context/savings?days=30` reports what they saved together, in tokens and dollars, by technique and by agent:

| Technique | What it does |
|---|---|
| `compaction` | When a managed run outgrows its context budget, old tool results are shrunk and the oldest turns are summarised |
| `history_trim` | A chat's oldest exchanges are folded into a short extract to fit the model's window |
| `result_tables` | A tool's list of records is sent as a table: the same data in fewer tokens |
| `prompt_cache` | The provider reads input tokens from its prompt cache, at the cached rate |

The tokens saved are priced at the model's input rate. For the cache, the saving is the difference between the input rate and the cached rate. A model with no price shows tokens but no dollars. The figure is conservative: a trimmed prompt is counted once per call it was trimmed in, although a managed run re-sends its context on every step.

The first three are counted as they happen and written every minute. The cache is read from the same metering as the rest of this page. A tenant-scoped caller sees its own tenant's savings, without the cache line, because the metering is kept per agent. `TICKETIQ_CONTEXT_SAVINGS_LEDGER=false` stops the counting. The techniques themselves run either way.

## Prices

Cost is computed from a price per model (`GET /api/usage/rates`).

1. Set or change a price with `PUT /api/usage/rates/{model}` (optional `?provider=` and `effective_from`). Earlier prices are kept (`GET /api/usage/rates/history`).
2. If a price was wrong, see what a period would have cost at the current card with `GET /api/usage/rates/reprice?start=...`. This is read-only: it reports the difference beside the recorded spend and never rewrites recorded rows. On a multi-tenant instance only an owner may run it.
3. Tools can have a price too (`GET /api/usage/tool-rates`, `PUT /api/usage/tool-rates/{kind}/{name}`), for paid APIs behind MCP servers.

> [!WARNING]
> A model with no price records zero cost, so a budget can never stop it. Turn on `refuse_unpriced` on an agent's binding: while the agent has a USD budget, calls to a model with no price are refused.

## Next steps

- [Budgets](/docs/ai-gateway/budgets)
