Sub topic 3
How Input and Output Tokens Are Priced
This chapter assumes you can read basic Python, know what a token is, and have seen JSON usage data from an LLM API. It covers how one request is billed. Later chapters build on this to cost whole agent loops.
Two meters, two rates
Providers count the tokens you send and the tokens the model generates separately, and charge a different rate for each.
- Input tokens are everything in the request: system prompt, tool definitions, conversation history, tool results, and your new message.
- Output tokens are everything the model generates: the reply text, tool call arguments, and in many APIs its reasoning.
Prices are usually quoted in USD per million tokens. Output tokens typically cost several times more than input tokens, because generating each one requires a separate pass through the model.
The cost formula
The cost of one request is the input tokens times the input rate, plus the output tokens times the output rate.
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 input side costs 20,000 × 3.0 = 60,000 units, and the output side costs 1,000 × 15.0 = 15,000 units. Dividing the total of 75,000 by one million gives $0.075.
Output is only 5% of the tokens here, but it is 20% of the cost. Input still dominates, because coding agents send far more than they generate.
Where the numbers come from
Take the counts from the usage field in the API response, not from your own estimate. Field names differ by provider, but most report input tokens, output tokens, and cache-related counts separately. Tokenizers differ between models, so the same text can produce different counts. A prompt that costs X tokens on one model may cost more or less on another.
Cached tokens are a third category. Most providers bill input tokens that were read from a prompt cache at a fraction of the normal input rate. The exact fraction varies by provider, so take it from their documentation. A later chapter covers caching in detail.
Common errors
- Using one rate for all tokens. This underestimates the cost of long replies and overestimates the cost of long prompts.
- Mixing units. Rates are per million tokens. A rate of 3.0 does not mean $3 per thousand.
- Ignoring hidden output. In many APIs, reasoning tokens count as output tokens even if you never see them in the reply.
- Hardcoding prices. Rates change and differ by model. Keep them in a config, not scattered through code.
- Comparing models by input rate alone. A model with a cheap input rate can still cost more if it writes longer replies or needs more attempts.
Why this matters for agents
A single request is cheap. The formula matters because an agent applies it on every turn, each time with a larger input. The next chapters use request_cost as the building block for that.
Exercise
Using the placeholder prices above (input 3.0, output 15.0 USD per million tokens), what does a request with 50,000 input tokens and 4,000 output tokens cost? Which side of the bill is larger?
Show answer
Input: 50,000 × 3.0 = 150,000. Output: 4,000 × 15.0 = 60,000. The total is 210,000 / 1,000,000 = $0.21. The input side is larger ($0.15 versus $0.06), even though each output token costs five times as much.