Sub topic 7
Estimating cost from token prices
This chapter assumes you know what a token is, have called a hosted model API, and can read basic Python. You will turn token prices into a per-request and monthly cost estimate before you commit to a model.
How hosted APIs charge
Hosted APIs bill by tokens, not by request. Providers usually quote a price per million tokens, and they set separate prices for input tokens (your prompt) and output tokens (the model's reply). Output tokens usually cost more than input tokens.
The cost of one request is:
cost = input_tokens / 1,000,000 * input_price + output_tokens / 1,000,000 * output_priceA cost function
def request_cost(input_tokens: int, output_tokens: int,
price_in_per_million: float, price_out_per_million: float) -> float:
return (input_tokens / 1_000_000) * price_in_per_million + \
(output_tokens / 1_000_000) * price_out_per_million
# Example with made-up prices: $1.00 per million input tokens, $4.00 per million output tokens
per_request = request_cost(2_000, 300, 1.00, 4.00)
monthly = per_request * 50_000 # 50,000 requests per month
print(f"per request: ${per_request:.4f}, per month: ${monthly:.2f}")
# per request: $0.0032, per month: $160.00The prices are placeholders. Always read the current price from your provider's pricing page.
Here the input is 2,000 tokens and costs $0.002. The output is 300 tokens and costs $0.0012. Multiplying the total by 50,000 requests gives $160 per month.
Where the input tokens come from
Input tokens are often larger than you expect. A request usually includes more than the user's question:
- The system prompt, which is sent on every call.
- Retrieved documents or examples you add as context.
- The conversation history, which grows with every turn in a chat.
This links to the context window. A larger window lets you send more text, but each token you send is billed. If you resend a long history on every turn, the cost of a conversation grows faster than the number of turns.
Comparing candidates
Run the same estimate for each candidate model with the same token counts. A cheaper model may need longer prompts or retries, which changes the total. Compare cost together with the pass rate from your own evaluation, not on price alone.
Common errors
- Counting only input tokens and forgetting the output.
- Mixing up price per thousand tokens and price per million tokens.
- Using average token counts when a few very long requests dominate the bill.
- Ignoring history growth in chat applications.
- Forgetting that self-hosting replaces token fees with hardware and operations costs, which the build-or-buy chapter covers.
Exercise
Your app sends 1,500 input tokens and receives 200 output tokens per request. The prices are $0.50 per million input tokens and $2.00 per million output tokens. You expect 100,000 requests per month. What is the monthly cost?
Show answer
Input cost per request is 1,500 / 1,000,000 0.50 = $0.00075. Output cost is 200 / 1,000,000 2.00 = $0.0004. The total per request is $0.00115. Multiplied by 100,000 requests, the monthly cost is $115.
Exercise
A chat app resends the full conversation on each turn. Why does the input cost per turn grow, and name one way to limit it.
Show answer
Each turn adds new messages to the history, so the input contains all earlier turns plus the new one, and you pay for all of them again. You can limit it by trimming or summarizing old turns, or by capping the history length.