# Token Cost

> The Token Cost is the price of using an AI model, calculated by the number of input and output tokens.

## How is the cost of an LLM API call calculated?

The way most AI APIs bill usage: a price per million input tokens and a higher price per million output tokens. Long contexts, agent loops and reasoning tokens increase costs, while caching and smaller models reduce them.

- Topic: Quality & Operations
- Type: concept

## Example

Wrong:

```js
const tokens = text.length / 4;
const cost = tokens * PRICE_PER_TOKEN;
```

Right:

```js
const { input_tokens, output_tokens } = response.usage;
const cost =
  (input_tokens / 1e6) * INPUT_PRICE_PER_M +
  (output_tokens / 1e6) * OUTPUT_PRICE_PER_M;
```

Real token counts from the API response with separate input and output prices give the actual cost, not a rough guess.

## Common mistakes

- Forgetting that output tokens usually cost more than input tokens.
- Resending the whole chat history every turn without trimming it.

## Key points

- Cost is calculated from the input and output token counts in the API response, not estimated from characters.
- A maximum output length is set, so one request cannot produce unlimited output.
- Repeated large prefixes use prompt caching where the API supports it.

## Interview questions

### Why do output tokens cost more than input tokens?

Generating each output token takes a separate step of the model, while input tokens are processed together, so output needs more compute.

### How can I reduce LLM API costs?

Trim chat history, cache repeated prompt prefixes, set a maximum output length, and use a smaller model for simple tasks.

### Do reasoning tokens count toward cost?

Yes. With reasoning models, internal reasoning tokens are usually billed as output tokens, even when they are not shown.

## Related cards

- [Token](https://wonderium.dev/ai/token.md)
- [Tokenizer](https://wonderium.dev/ai/tokenizer.md)
- [Prompt Caching](https://wonderium.dev/ai/prompt-caching.md)
- [Max Tokens](https://wonderium.dev/ai/max-tokens.md)
- [Rate Limits](https://wonderium.dev/ai/rate-limits.md)

---

- Level: [Artificial Intelligence](https://wonderium.dev/ai.md)
- HTML version: https://wonderium.dev/ai/token-cost
- Last updated: 2026-11-25
