# Billing

## Tokens

The API spends the same token balance as the site. These docs use two kinds of tokens:

- **balance tokens**: what the money on your account is counted in. Catalog prices, `usage.cost_tokens`, `GET /v1/balance` and key limits are in them;
- **model tokens**: units of text: `prompt_tokens`, `completion_tokens`, `max_tokens`.

A text model's price is how many balance tokens 1M model tokens cost. A media price is how many balance tokens a generation with these parameters costs. Top up in [plans](/plan).

## Text models: hold and charge

1. **Hold.** When a request is admitted, the balance reserves its largest possible price: the input plus `max_tokens` of output. Without `max_tokens`, the model's output limit is used.
2. **Generation.** The model cannot go past that limit: we pass it the same `max_tokens`.
3. **Charge.** After the answer the real price by `usage` is charged and the rest of the hold is released at once.

A hold is not spend, but held tokens are not available to other requests. Hence the rule: **set `max_tokens` to what the task needs**. Every request then holds less and more requests fit in parallel. `GET /v1/balance` shows `available_tokens` (free) and `held_tokens` (held by running requests).

## When the balance is short

- The balance does not cover the input plus 1024 output tokens: `402 insufficient_balance`.
- `max_tokens` is set, but the balance does not cover that much output: `402 insufficient_balance`. The message and `error.details.affordable_max_tokens` say how much output the balance pays for right now:

```json
{
  "error": {
    "type": "billing_error",
    "code": "insufficient_balance",
    "message": "The balance does not cover max_tokens=32000: it pays for up to 6120 output tokens. Lower max_tokens or top up.",
    "param": "max_tokens",
    "request_id": "req_01JE...",
    "doc_url": "https://unitool.ai/developers/errors#insufficient_balance",
    "details": { "required_tokens": 41.3, "available_tokens": 8.1, "affordable_max_tokens": 6120 }
  }
}
```

- `max_tokens` is omitted and the balance does not cover the model's limit: the request runs with a lower limit. The answer has `x-unitool-output-capped: true`; if the answer hits that limit, `finish_reason` is `length`.
- The money is there but held by running requests: `429 balance_held` with `Retry-After`. Retry when they finish.
- A key limit works the same way: `402 key_spend_limit_reached`, with `affordable_max_tokens` when `max_tokens` is the reason.

## Disconnects

Closing the connection or aborting a stream stops the generation at once. You are charged for what the model produced: the input and the output up to the cut. If the model did not report its exact usage, it is counted from the generated text and the receipt has `estimated: true`.

## When you do not pay

- A request refused before it starts (400, 401, 402, 403, 404, 429): nothing is held or charged.
- The model failed before answering: the hold is released, the charge is 0.
- We stopped the request before the first token (a service restart, the 15-minute limit): the charge is 0, the answer is `503 overloaded`, the request can be retried.

## Partial answers

If an answer breaks after text has started (a model failure, the 15-minute request limit, a service restart), only the generated part is charged:

- streamed: a `data: {"error": {...}}` event with the sum in `error.details.charged_tokens`;
- not streamed: `200` with the part that is ready, `finish_reason: "length"` and the `x-unitool-incomplete` header (`upstream_error`, `deadline` or `shutdown`).

## Media

A generation's price is known in advance: `POST /v1/quotes` shows it, and `max_cost_tokens` prevents charging more. A generation that fails (model failure or moderation, timeout) returns its tokens in full. So does a cancel before the start. If the model returns fewer variants than ordered, the full ordered price is charged. A key's spend limit counts generations that are still running too.

## Receipts

Every request has a receipt. Its id comes in the `x-request-id` header and in error bodies.

```bash
curl https://api.unitool.ai/v1/requests/req_01JE... -H "Authorization: Bearer $UNITOOL_API_KEY"
```

```json
{
  "object": "request",
  "id": "req_01JE...",
  "created": 1764331200,
  "endpoint": "/v1/chat/completions",
  "model": "claude-sonnet-5-5",
  "status": "ok",
  "error_code": null,
  "streamed": true,
  "estimated": false,
  "usage": {
    "prompt_tokens": 1200, "completion_tokens": 350, "total_tokens": 1550,
    "prompt_tokens_details": { "cached_tokens": 1024, "cache_write_5m_tokens": 0, "cache_write_1h_tokens": 0 },
    "completion_tokens_details": { "reasoning_tokens": 0 },
    "cost_tokens": 0.62
  },
  "client_request_id": null
}
```

`status`: `ok`, `client_closed` (the connection was closed), `error`, `canceled`. `GET /v1/requests?limit=20&cursor=...` lists receipts newest first; the next page is at `next_cursor`. A receipt appears after the charge: a request that is still running has none yet (`404 request_not_found`).

## Spend by day

`GET /v1/usage` is the account's spend by day and model, all keys together. It needs the `read` scope.

```bash
curl "https://api.unitool.ai/v1/usage?from=2026-11-01&to=2026-11-28" -H "Authorization: Bearer $UNITOOL_API_KEY"
```

| Parameter | Meaning |
|---|---|
| `from`, `to` | Days as `YYYY-MM-DD`, Moscow time, inclusive. Default: the last 30 days up to today; at most 366 days |
| `model` | Only this model |
| `key` | Only this key: its id from `GET /v1/key` (a key of the same account) |
| `format` | `json` (default) or `csv`, a file with the same columns |

An impossible date (such as `2026-02-30`), `from` after `to` or a period over 366 days is `400 invalid_parameter`.

```json
{
  "object": "list",
  "timezone": "Europe/Moscow",
  "from": "2026-11-01",
  "to": "2026-11-28",
  "data": [
    { "date": "2026-11-28", "model": "claude-sonnet-5-5", "requests": 412, "errors": 3, "input_tokens": 1820400, "cached_tokens": 1204000,
      "output_tokens": 240100, "reasoning_tokens": 0, "outputs": 0, "cost_tokens": 812.4 }
  ],
  "totals": { "requests": 412, "cost_tokens": 812.4 }
}
```

Rows go from the newest day back, one per day and model. `outputs` is the number of result files of generations, `cost_tokens` the balance tokens charged. Data lags about 2 minutes. Requests refused before they started (limits, a short balance, request errors) are not in it: nothing was paid for them.
