Sub topic 1
How tokens turn into cost
Prerequisites
This chapter assumes you can read basic Python, have called an LLM chat API before, and know what a prompt, a system message, and a sampling setting such as temperature are. You also need a rough idea of how HTTP APIs return JSON. No other background is needed.
Why tokens are the unit of cost
A token is a chunk of text that the model reads or writes. It can be a whole short word, part of a longer word, or a punctuation mark. Providers do not charge per request or per word. They charge per token, and they usually quote prices per million tokens.
A single request has two kinds of tokens, and they have different prices:
- Input tokens: everything you send. This covers the system message, the conversation history, retrieved documents, and the user's question.
- Output tokens: everything the model generates in reply.
Output tokens usually cost several times more than input tokens, because generating each one takes a separate step through the model. The cost of one request is:
cost = (input_tokens × input_price + output_tokens × output_price) / 1,000,000Prices are per million tokens, so you divide by 1,000,000 at the end.
Estimating cost before you send
You can count input tokens locally with a tokenizer library. The example below uses tiktoken and placeholder prices. Replace the prices with your provider's current numbers.
import tiktoken
# Placeholder prices in USD per million tokens. Replace with your provider's current prices.
PRICE_PER_MTOK = {"input": 1.00, "output": 4.00}
def estimate_cost(prompt: str, expected_output_tokens: int) -> float:
enc = tiktoken.get_encoding("cl100k_base")
input_tokens = len(enc.encode(prompt))
return (
input_tokens * PRICE_PER_MTOK["input"]
+ expected_output_tokens * PRICE_PER_MTOK["output"]
) / 1_000_000
print(estimate_cost("Summarize this paragraph in two sentences.", 120))The input count is exact only if the tokenizer matches your model. The output count is a guess, so you pass in the size you expect. In this example the prompt is about 8 tokens, so input costs almost nothing. The 120 output tokens at the higher price make up nearly all of the cost.
Where the money actually goes
Per-request cost looks tiny, so the total only matters at volume. Multiply by requests per day to see the real figure.
| Scenario | Input tokens | Output tokens | Requests per day |
|---|---|---|---|
| Short chat answer | 300 | 150 | 10,000 |
| Document summary | 6,000 | 300 | 10,000 |
| Chat with long history | 4,000 | 150 | 10,000 |
Two patterns follow from the table.
- Long inputs dominate when the prompt is large. The summary case sends 20 times more input than the chat case, so trimming the prompt has a large effect there.
- History grows with every turn. If you resend the full conversation each time, input tokens grow with each turn, and the total cost of a conversation grows faster than its length.
Output tokens cost more per token but are often fewer. Check both columns before deciding which one to reduce.
Exercises
Exercise
A request has 2,000 input tokens and 500 output tokens. Input costs $1.00 per million tokens and output costs $4.00 per million tokens. What is the cost of one request, and which side contributes more?
Show answer
Input: 2,000 × 1.00 / 1,000,000 = $0.002. Output: 500 × 4.00 / 1,000,000 = $0.002. The total is $0.004, and both sides contribute equally. Output has four times fewer tokens but costs four times more per token.
Exercise
Why can the local result of estimate_cost differ from your bill?
Show answer
The local tokenizer may not match the model's tokenizer, the output length is only a guess, and hidden tokens such as tool definitions or reasoning tokens may be billed. The usage field in the response is the authoritative count.