What are tokens?
Worked math: counting tokens and cost
A useful cost estimate separates everything sent to the model from everything the model generates, then applies the correct rate to each side.
Before you start
Why this matters
A dashboard says a request used 3,200 tokens. Is that expensive? The number alone is incomplete. You need to know how many were input tokens, how many were output tokens, whether cached input has a different rate, which model handled the request, and how often the request runs. A tiny per-call cost can become material at high volume; a large one-time analysis may be negligible.
1Learn the idea
Read
Separate prompt and completion
Providers commonly report at least two categories:
- Input or prompt tokens: the sequence supplied to the model, including instructions, messages, retrieved text, and formatting added by the API.
- Output or completion tokens: tokens generated in the response, including tool-call arguments or hidden reasoning categories when a provider explicitly bills them that way.
Terminology and billing details vary, so consult the model’s current pricing and usage schema. Do not assume every provider counts cached input, images, audio, reasoning, or batch requests identically.
For ordinary text pricing, the basic equation is:
request cost =
(input tokens / 1,000,000 × input price per million)
+ (output tokens / 1,000,000 × output price per million)
Dividing by one million is essential when rates are quoted “per million tokens.” Keep enough decimal places during calculation; rounding each request too early can distort a monthly estimate.
Read
A complete request calculation
Assume an illustrative model charges $0.40 per million input tokens and $1.60 per million output tokens. These are teaching numbers, not a current price claim. One support request reports:
- 2,400 input tokens;
- 600 output tokens.
Input cost:
2,400 / 1,000,000 × $0.40 = $0.00096
Output cost:
600 / 1,000,000 × $1.60 = $0.00096
Total:
$0.00096 + $0.00096 = $0.00192
Although output is only one quarter of the token count, it costs the same in this example because its unit rate is four times higher. This is why reporting only “3,000 total tokens” hides useful information.
If the API reports 2,400 input and 600 output, prefer those measured values over a count from the visible user message. The request may include system instructions, message wrappers, and other content that the UI does not display.
Read
Scale from one call to a month
Suppose the feature handles 80,000 requests each weekday for 22 weekdays. Monthly requests are:
80,000 × 22 = 1,760,000 requests
At $0.00192 each, the projected model charge is:
1,760,000 × $0.00192 = $3,379.20
Now include variation. If 10% of requests are complex and use twice the input and output, a simple average is:
90% × $0.00192 + 10% × $0.00384
= $0.002112 per request
Monthly cost becomes:
1,760,000 × $0.002112 = $3,717.12
For planning, use a distribution from real traffic rather than one “typical” request. Long-tail cases—large documents, long conversations, repeated agent steps—can dominate spend.
Read
Account for repeated calls
One user action may trigger several model calls. An agent might classify a request, retrieve data, draft a tool call, inspect the result, and write a final response. If each step resends most of the history, input tokens accumulate.
Consider a three-call workflow:
Call 1: 1,000 input + 100 output
Call 2: 1,450 input + 180 output
Call 3: 2,000 input + 450 output
Total: 4,450 input + 730 output
Do not estimate this workflow as “the final prompt is 2,000 tokens.” Billing occurs per call. The earlier 2,450 input tokens were also processed. Retries, fallbacks to another model, evaluation calls, and failed tool loops belong in the total.
Track cost per successful user task, not only per API call. A cheap call that causes multiple retries can be more expensive than one reliable call.
Read
Budgets, caps, and output headroom
An output cap limits how many tokens the model may generate; it does not reserve free space or guarantee that length. Set it high enough for a complete required answer but low enough to prevent uncontrolled output. If a valid response sometimes needs 500 output tokens, a cap of 100 may create truncated, unusable results and retries.
Build budgets by component. For a target of 6,000 input tokens, you might allocate:
- 500 to stable instructions;
- 800 to recent history;
- 3,800 to retrieved evidence;
- 400 to the current request;
- 500 as input variance headroom.
Then reserve output capacity according to the model’s context rules and your response requirement. Budget alerts should use measured usage. Log model, route, input tokens, output tokens, request status, and cost category so you can find the feature driving spend.