Area 01 · LLM · Concept 13 of 20

Token Pricing

Input, output and cached tokens cost different amounts. Know your cost per call.

11–20 · Production levelPhase 1 · Weeks 3–6≈ 45 min

The idea

APIs bill per million tokens, with separate prices for input, output, cache writes and cache reads. Output tokens typically cost several times more than input tokens because generating them is slower. Bigger models cost more per token than smaller ones from the same family, often by an order of magnitude.

Cost per call = input tokens × input price + output tokens × output price (+ cache terms). The API response tells you exactly how many tokens were used, so you can log real cost per request instead of estimating.

Product-level cost is per feature and per user, not per call. An agent that makes 12 calls per task, each resending a growing history, can cost far more than a one-shot chatbot on the same model.

Why it matters

Unit economics decide whether an AI feature can ship. You need cost per request in your logs from the first prototype.

Words to know

  • MTokOne million tokens, the usual pricing unit.

Try it without code

Price a feature on paper

  1. Open your provider’s pricing page.
  2. Estimate a support bot: 1,500 input tokens, 300 output tokens, 20,000 calls a day.
  3. Work out monthly cost for a large and a small model.

Use it in code

Turn the usage object into a cost line. Keep prices in config, since they change.

python
PRICES = {  # USD per million tokens - check your provider's pricing page
    "claude-sonnet-5-5": {"in": 3.00, "out": 15.00, "cache_read": 0.30, "cache_write": 3.75},
    "claude-haiku-5-5": {"in": 1.00, "out": 5.00, "cache_read": 0.10, "cache_write": 1.25},
}

def cost(model, usage):
    p = PRICES[model]
    return (usage.input_tokens * p["in"]
            + usage.output_tokens * p["out"]
            + (usage.cache_read_input_tokens or 0) * p["cache_read"]
            + (usage.cache_creation_input_tokens or 0) * p["cache_write"]) / 1_000_000

# after any call:  print(f"USD {cost(model, msg.usage):.5f}")
PRACTICE · TICK IT AFTER THIS

Add a cost line to every reply in your chatbot and a running session total.

Common mistakes

  • Hard-coding prices and never updating them.
  • Ignoring the cost of retries and agent loops.
  • Only tracking total spend, not cost per feature.

Go deeper