Skip to content
mentoor

Topic 1 of 5 · 4 min read

Counting tokens and cost per request

Goal: Calculate the cost of a request from its usage fields and the price table, including cached and reasoning tokens.

The cost of one API call is tokens times price, and the response already tells you the tokens: read the usage fields, multiply by the right rate for each kind, and add them up.

Check yourself · 1 of 3

A response reports 2,000 input tokens and 300 output tokens. Input costs $2 per million and output $10 per million. What does the call cost?

Choose one answer

Why it matters

Providers bill per million tokens, and output costs several times more than input. A feature that looks cheap in a demo can cost real money at 50,000 requests a day. You cannot cut a cost you have not measured, so accounting comes before every other trick in this skill.

The four kinds of token

  • Input tokens: everything you send, including the system prompt, tool definitions, history and documents.
  • Output tokens: what the model writes back. Usually priced at several times the input rate.
  • Cached tokens: input served from a prompt cache. Reads are billed at a fraction of the input price; writes cost a bit more than normal input. The next topic covers this.
  • Reasoning tokens: hidden thinking some models do before answering. They are billed as output even when you never see them.

How to do it

  1. Log the usage object of every response: input, output, cache write, cache read and, where reported, reasoning tokens. Field names differ by provider, so check the docs.
  2. Keep a price table in config, one row per model, with the date you copied it. Prices change.
  3. Compute cost per request: for each token kind, tokens divided by 1,000,000 times its rate. Sum them.
  4. Tag each request with the feature that made it, and sum per feature per day.
  5. Estimate before you send when it matters. Claude offers a token counting endpoint and OpenAI publishes the tiktoken library, so you can price a prompt before paying for it.

Before and after

Weak: "Our chatbot costs about a cent per message." Nobody measured it, and it hides that one feature is 80 % of the bill.

Fixed: a table with one row per feature showing requests per day, average input tokens, average output tokens and daily cost. The row with the largest daily cost is where you work first.

What goes wrong

  • Counting only input and forgetting that output is priced higher.
  • Ignoring reasoning tokens, so the bill is larger than visible text suggests.
  • Averaging across features, which hides the one expensive path.
  • Using a stale price table after a provider changes its rates.

Examples

Pricing one request by hand

Illustrative prices: $3 per million input tokens, $15 per million output tokens.

Token kind Tokens Rate per million Cost
Input 4,000 $3 $0.0120
Output 500 $15 $0.0075
Total $0.0195

At 20,000 requests a day that is about $390 a day. A 25 % cut on the output side saves less than the same cut on input, so check which side dominates before optimising.

A usage log that supports decisions

const res = await client.messages.create({ model, max_tokens: 400, messages });
logUsage({
  feature: "ticket-summary",
  model,
  input: res.usage.input_tokens,
  output: res.usage.output_tokens,
  cacheRead: res.usage.cache_read_input_tokens ?? 0,
  cacheWrite: res.usage.cache_creation_input_tokens ?? 0,
});

One row per call, with the feature name, lets you group cost by feature later.

Key points

  • Cost per request is the sum, over each token kind, of tokens divided by one million and multiplied by that kind's price.
  • Output tokens are priced higher than input tokens, so a long answer can cost more than a long prompt.
  • Reasoning tokens are billed as output even though the final text does not show them.
  • Log the usage fields of every response and tag each request with the feature that made it.
  • Keep prices in a dated config table, because providers change them.

Common mistakes

  • Estimating cost from input length alone and ignoring the higher output rate.
  • Forgetting hidden reasoning tokens when a thinking model is switched on.
  • Tracking one monthly total with no per-feature breakdown.
  • Hard-coding prices in code with no date, then trusting them a year later.

Check yourself · 2 of 3

A thinking model returns a two-line answer, yet the output token count is 3,000. What explains it?

Choose one answer

Check yourself · 3 of 3

Your team wants to know which product feature drives the monthly AI bill. What is the most useful change to the logging?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic