Sub topic 9
Estimating what a call costs
This chapter assumes you can read basic Python, have called an LLM through a chat or completion API, and are comfortable with simple arithmetic. It builds on the earlier chapters about tokens, per-token pricing, and context windows.
The cost formula
Providers bill input tokens and output tokens at separate rates. Prices are quoted in dollars per million tokens. The cost of one call is:
cost = (input_tokens × input_price + output_tokens × output_price) / 1,000,000Output tokens usually cost several times more than input tokens, so a short answer to a long prompt can still be cheap, and a long answer to a short prompt can be expensive.
A worked example
Suppose a call sends 2,000 input tokens and gets back 500 output tokens. The made-up prices are $3 per million input tokens and $15 per million output tokens.
- Input: 2,000 × 3 = 6,000
- Output: 500 × 15 = 7,500
- Sum: 13,500
- Divide by 1,000,000: $0.0135
The code below does the same arithmetic. It also checks that the prompt plus the maximum output fits the context window.
def fits_in_window(input_tokens, max_output_tokens, context_window):
return input_tokens + max_output_tokens <= context_window
def estimate_cost(input_tokens, output_tokens,
input_price_per_million, output_price_per_million):
return (
input_tokens * input_price_per_million
+ output_tokens * output_price_per_million
) / 1_000_000
# Placeholder prices in dollars per million tokens, not real rates.
cost = estimate_cost(2000, 500, 3.0, 15.0)
print(fits_in_window(2000, 500, 8000)) # True
print(f"${cost:.4f}") # $0.0135A tenth of a cent sounds trivial. The number matters when you multiply it by volume. At 100,000 calls a day, $0.0135 per call is $1,350 a day.
Where the inputs come from
You can fill in the formula before a call or after it.
- Before the call: count the prompt with a tokenizer library, and use your
max_tokenssetting as the worst-case output. This gives an upper bound. - After the call: read the usage data in the API response. It reports the tokens the provider actually billed, so prefer it for accounting.
Tokenizer counts are exact only for the matching model family. For other models, treat them as estimates.
Estimating a whole conversation
A chat does not cost the same on every turn. Each call resends the system prompt and the full history, so input grows with every turn. The function below simulates this.
def total_input_tokens(system_tokens, user_tokens, reply_tokens, turns):
total = 0
history = system_tokens
for _ in range(turns):
history += user_tokens
total += history # the whole history is sent as input on every call
history += reply_tokens
return total
print(total_input_tokens(500, 100, 300, 10)) # 24000With a 500-token system prompt, 100-token user messages, and 300-token replies, ten turns bill 24,000 input tokens. The user and the model wrote only 4,000 new tokens in that time. The other 20,000 are the same text sent again.
Output cost is easier to predict. It is the sum of the replies, here 10 × 300 = 3,000 tokens. At the placeholder prices:
- Input: 24,000 × 3 / 1,000,000 = $0.072
- Output: 3,000 × 15 / 1,000,000 = $0.045
- Total: $0.117
In this chat, input is the larger share, and it grows faster than output as the chat gets longer.
Common errors
- Forgetting the system prompt. It counts as input on every call.
- Pricing only the new message. The history is billed again each turn.
- Using one price for both directions. Input and output rates differ.
- Ignoring hidden output. Some models produce reasoning tokens that are billed as output but may not appear in the reply. A later chapter covers them.
- Mixing units. Prices are per million tokens, not per thousand.
- Treating
max_tokensas the expected output. It is a ceiling. Use it for a worst-case bound, and use observed averages for a typical case.
Exercise
A call sends 10,000 input tokens and receives 1,000 output tokens. Input costs $2 per million tokens and output costs $10 per million tokens. What does the call cost, and what share of the cost is input?
Show answer
Input: 10,000 × 2 / 1,000,000 = $0.02. Output: 1,000 × 10 / 1,000,000 = $0.01. The total is $0.03. Input is $0.02 of $0.03, about 67%, even though output tokens cost five times as much each.
Exercise
Using totalinputtokens, a chat has a 1,000-token system prompt, 200-token user messages, and 400-token replies. How many input tokens are billed over 3 turns?
Show answer
Turn 1 sends 1,000 + 200 = 1,200. After the reply, history is 1,600. Turn 2 sends 1,800, and the history becomes 2,200 after the reply. Turn 3 sends 2,400. The total is 1,200 + 1,800 + 2,400 = 5,400 input tokens.