Sub topic 3
How Input and Output Tokens Are Priced
This chapter explains how a provider turns token counts into a bill. It assumes you can read basic Python and JSON, and that you know what a token and a context window are from the earlier chapters. By the end you should be able to compute the cost of one request from its usage data, and to see why the same request can cost very different amounts depending on how its tokens split between input and output.
The basic pricing model
Providers bill per token, and they quote prices per million tokens (often written MTok). Every request has at least two billed quantities:
- Input tokens are everything you send: the system prompt, tool definitions, conversation history, file contents, and tool results.
- Output tokens are everything the model generates: its reply text, tool calls, and on many models its reasoning.
Each quantity has its own rate, and output usually costs several times more per token than input. A common ratio is 4x to 5x, but it varies by model and provider. Output is more expensive because the model produces it one token at a time, with a full forward pass for each, while input tokens can be processed in parallel.
The cost of one request is:
cost = input_tokens × input_rate + output_tokens × output_rateHere is the function as code. Prices are USD per million tokens.
def request_cost(usage, price):
"""Cost in USD of one request. Prices are USD per million tokens."""
return (
usage["input_tokens"] * price["input"]
+ usage["output_tokens"] * price["output"]
) / 1_000_000
# Placeholder prices, not real rates. Use your provider's current price list.
price = {"input": 3.0, "output": 15.0}
usage = {"input_tokens": 20_000, "output_tokens": 1_000}
print(request_cost(usage, price)) # 0.075The 20,000 input tokens cost $0.060 and the 1,000 output tokens cost $0.015. Input has 20 times as many tokens here, so it is 80% of the bill even though each output token costs 5 times as much.
Which side dominates depends on the workload
Neither side is always the larger cost. The split depends on what the request does.
| Workload | Typical input | Typical output | Dominant cost |
|---|---|---|---|
| Agent turn that reads files and calls a tool | 40,000 | 500 | Input |
| Summarizing a long log | 30,000 | 400 | Input |
| Generating a new module from a short spec | 2,000 | 4,000 | Output |
| Answering a short question | 1,000 | 300 | Roughly balanced |
You can check the extremes with the placeholder prices above. A 40,000-token input with 500 output tokens costs 40,000 × 3 + 500 × 15 = 127,500, which is $0.1275 per request. Output is under 6% of that. A 2,000-token input with 4,000 output tokens costs 2,000 × 3 + 4,000 × 15 = 66,000, which is $0.066. Output is over 90% of that.
Coding agents sit mostly in the first row. They read a lot and write little on each turn, because the loop resends the whole context every time. So for agents, input volume usually decides the bill, and the lower input rate is not much comfort. Later chapters cover how the loop multiplies input cost and how caching reduces it.
The shape of a bill
The flow from a single request to a charge looks like this.
The key point is that the provider's usage record, not your own estimate, is what you are billed on. Your local token counter can disagree with the provider's, because each model family has its own tokenizer. Treat local counts as estimates and the usage field in the API response as the source of truth.
More than two categories
The two-rate model is a simplification. Real usage records often include more categories, each with its own rate:
- Cache reads are input tokens served from a prompt cache. They are billed at a fraction of the normal input rate. The exact fraction depends on the provider.
- Cache writes are input tokens stored in the cache for the first time. Some providers charge a premium over the normal input rate for these.
- Reasoning tokens are tokens the model generates while thinking before it answers. Most providers bill them at the output rate, even if you never see their text.
- Batch or flex tiers can discount both rates in exchange for slower, asynchronous responses. They suit offline work and rarely suit an interactive agent.
Field names differ across providers, so check the documentation for yours. The structure of the calculation stays the same: count tokens in each category, multiply by that category's rate, and add.
Here is a version of the cost function that handles cache reads. It assumes the usage record reports uncached input and cache reads as separate counts, which is how some providers report them. Others report total input and cached input, in which case you subtract first.
def request_cost_detailed(usage, price):
"""Cost in USD for a request with cached input. Prices are USD per million tokens."""
return (
usage["input_tokens"] * price["input"]
+ usage["cache_read_tokens"] * price["cache_read"]
+ usage["output_tokens"] * price["output"]
) / 1_000_000
# Placeholder prices: cache reads cost 10% of the normal input rate.
price = {"input": 3.0, "cache_read": 0.3, "output": 15.0}
usage = {
"input_tokens": 5_000, # uncached input
"cache_read_tokens": 35_000, # served from cache
"output_tokens": 1_000,
}
print(round(request_cost_detailed(usage, price), 4)) # 0.0405Without the cache, the same 40,000 input tokens would cost 40,000 × 3 = 120,000 for input alone, and the request would total 135,000, or $0.135. With the cache it is $0.0405, which is 70% less. The saving comes entirely from the input side.
Reasoning tokens are hidden output
Reasoning tokens deserve a warning here because they break a common assumption. You might expect that the output tokens you are billed for equal the length of the reply you see. With a reasoning model, the billed output count includes the thinking the model did first.
For example, a reply that shows 300 tokens of text might report 4,300 output tokens, of which 4,000 were reasoning. At the placeholder output rate of $15 per million, that is $0.0645 instead of $0.0045. The hidden part is over 90% of the cost.
A later chapter covers when extra thinking is worth paying for. For now, remember that reasoning raises the output side of the formula and that you can't see it in the text.
Common errors when estimating cost
These mistakes show up repeatedly when people predict spend.
- Treating tokens as words. A token is often less than a word, and code tokenizes into more tokens per character than prose does. Estimate from token counts, not word counts.
- Using one rate for everything. Applying the input rate to output tokens understates the output side by the ratio, which is several times. The reverse error overstates input.
- Ignoring the resent context. An agent request is not just the new message. It includes the system prompt, tool definitions, and history, and all of it is billed as input on every turn.
- Comparing prices across providers by name. Two models can tokenize the same text into different counts, so equal per-token prices do not mean equal cost for the same job.
- Forgetting minimums and extras. Some features, such as web search tools or code execution, carry separate charges that are not token-based. Check the price list for these.
Estimating before you run
You can estimate a request's cost before sending it using three numbers: input tokens, expected output tokens, and the rates. Input is known in advance, since you can count it with a tokenizer or the provider's token-counting endpoint. Output is not known, but you can bound it with the maximum output setting, which gives a worst-case figure.
The worst case for the first example, if the model used all of a 4,000-token output limit, would be 20,000 × 3 + 4,000 × 15 = 120,000, which is $0.12. The expected case is lower, since most replies end before the limit. Setting a sensible output limit is the simplest way to cap the more expensive side of the bill.
Exercise
Using the placeholder prices of $3 per million input tokens and $15 per million output tokens, a request has 8,000 input tokens and 2,000 output tokens. What does it cost, and which side is the larger share of the cost?
Show answer
Input: 8,000 × 3 = 24,000. Output: 2,000 × 15 = 30,000. The total is 54,000 divided by 1,000,000, which is $0.054. Output is the larger share, at about 56% ($0.030 of $0.054), even though it has only a quarter as many tokens as input.
Exercise
A usage record reports 10,000 uncached input tokens, 30,000 cache read tokens, and 600 output tokens. The prices are $3 input, $0.30 cache read, and $15 output, all per million tokens. Calculate the cost, and say how much a request with no caching would have cost.
Show answer
With caching: 10,000 × 3 = 30,000; 30,000 × 0.3 = 9,000; 600 × 15 = 9,000. The total is 48,000 divided by 1,000,000, which is $0.048. Without caching, all 40,000 input tokens bill at the input rate: 40,000 × 3 = 120,000, plus 9,000 for output, giving 129,000 or $0.129. Caching cut the cost by about 63%.