Sub topic 7
How providers price tokens
This chapter covers how providers turn token counts into a bill. It assumes you can read basic Python, have called an LLM through a chat or completion API, and are comfortable with simple arithmetic and unit conversion. It builds on the earlier chapters on what a token is and on the context window.
The basic pricing model
Most hosted LLM providers charge per token. You pay for what the model reads and for what it writes, and you do not pay for time spent connected. Three features of this model matter for everything that follows.
- Input and output are priced separately. Input tokens are the prompt you send. Output tokens are what the model generates.
- Prices are quoted per million tokens, usually written as "dollars per 1M tokens". A price of $3 per million input tokens means each input token costs $0.000003.
- Output tokens cost more than input tokens. A ratio of 3x to 5x is common. Generating text is sequential and uses more compute than reading a prompt, and providers price that difference in.
The formula for one call is:
cost = (input_tokens × input_price + output_tokens × output_price) / 1,000,000Here both prices are in dollars per million tokens. The division converts the result to dollars.
The prices in this chapter are placeholders chosen so the arithmetic is easy to follow. They are not real rates. Always read the current price list of the provider you use, because prices change and differ by model.
A worked example
Take a call with 2,000 input tokens and 500 output tokens, priced at $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 same calculation in Python, together with a check that the request 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.0135Output is only 20% of the tokens in this call (500 of 2,500), yet it is 7,500 of the 13,500 units of cost, or about 56% of the bill. Token share and cost share can differ a lot. When you reason about where money goes, use cost share.
What you are billed for
The billed input is everything the model receives, not only the last message you typed. That includes:
- the system prompt
- the full conversation history you resend
- tool or function definitions
- documents, retrieved passages, and examples you place in the prompt
- the new user message
Images, audio, and files are converted to tokens as well, using provider-specific rules. A single image may count as hundreds or thousands of tokens.
The billed output includes every token the model generates, including text you never show to a user. Some models also produce reasoning tokens that are billed as output even when they are not returned. A later chapter covers those in detail. For now, remember that the output you see can be smaller than the output you pay for.
You are generally billed for tokens the model processed, even if the result is not useful. A response that is cut off at the output limit is still billed. A call that returns an error before processing usually costs nothing, but check your provider's rules for timeouts and cancelled streams, because they vary.
Why the prices differ between models
Providers sell several models at different price points, often called tiers. The pattern is consistent:
| Tier | Typical use | Relative price |
|---|---|---|
| Small or fast | Classification, extraction, simple chat | Lowest |
| Mid-range | General assistants, coding help, summarization | Medium |
| Large or frontier | Hard reasoning, long multi-step tasks | Highest |
Price gaps between tiers can reach 10x to 50x per token. That makes model choice the largest single lever on cost. A task that a small model handles well should not run on the largest one. Later chapters return to this under cost reduction.
Price is not the only difference. Models also vary in context window size, maximum output length, speed, and quality. The cheapest model per token is not always the cheapest per finished task. If a small model needs three attempts and the larger one succeeds in one, the comparison depends on the total tokens of all attempts.
Other price adjustments
Beyond the base input and output rates, providers add pricing rules. Names and details differ, so treat the list below as categories to look for in a price list, not as a guarantee.
- Cached input. Input tokens that the provider has recently processed and stored can be billed at a lower rate. Some providers charge extra to write to the cache. The chapter on prompt caching covers this.
- Batch processing. Requests submitted for asynchronous processing, with results returned later, are often discounted. The trade-off is latency, since results may take hours.
- Long-context surcharges. Some models charge a higher per-token rate when the prompt exceeds a size threshold. If your prompt crosses the threshold, the higher rate may apply to the whole prompt, not only the tokens above it. Read the exact rule.
- Tool or feature fees. Built-in tools such as web search or code execution may carry their own charges on top of tokens.
- Priority or provisioned capacity. Some providers sell faster service or reserved throughput at a different price structure, such as a fixed hourly fee.
Common mistakes
Several errors come up repeatedly when people first estimate cost.
- Counting only the new message. Because history is resent, input cost for a chat grows with each turn. The next chapters quantify this. A cost estimate that uses only the latest message undercounts badly.
- Mixing units. Prices are per million tokens. Dividing by 1,000 or forgetting to divide produces answers off by a factor of 1,000 or 1,000,000. Check the order of magnitude: a typical single call should cost a fraction of a cent to a few cents.
- Using one price for both directions. Applying the input price to output tokens understates the bill by the output multiple.
- Assuming word counts equal token counts. English text averages somewhat more than one token per word, and code, other languages, and unusual text can use many more. Count with the right tokenizer.
- Using stale prices. Hard-coded prices in a script become wrong when the provider updates its list. Keep prices in configuration with a date, not scattered through code.
- Ignoring the maximum output setting when budgeting. Worst-case cost uses the maximum output you allow, not the typical output you expect.
Budgeting with a worst case
Because output length is set by the model and not by you, you cannot know the exact cost of a call before it runs. You can, however, compute an upper bound. Input tokens are known once you count them. Output is capped by the maximum output setting. So:
def worst_case_cost(input_tokens, max_output_tokens,
input_price_per_million, output_price_per_million):
return (
input_tokens * input_price_per_million
+ max_output_tokens * output_price_per_million
) / 1_000_000
# Placeholder prices, as before.
print(f"${worst_case_cost(2000, 4000, 3.0, 15.0):.4f}") # $0.0660With the output cap raised to 4,000 tokens, the upper bound is $0.0660, nearly five times the $0.0135 of the earlier example. The expected cost is usually lower, because most replies stop before the cap. The upper bound is still the number to use when you need a guarantee, for example when setting a spending limit for a batch job that makes thousands of calls.
Scaling up
Per-call cost becomes meaningful at volume. Multiply the per-call cost by the number of calls:
calls_per_day = 50_000
cost_per_call = 0.0135 # from the worked example
print(f"${calls_per_day * cost_per_call:,.2f} per day") # $675.00 per dayAt 50,000 calls a day, a call that costs about a cent and a half becomes $675 a day, or about $20,000 a month. This is why teams look at the input to output ratio, model tier, caching, and batch discounts once traffic grows. Each of these changes a term in the formula, and you can see which one dominates by computing the cost share of input and output separately for your own workload.
A workload with long prompts and short answers, such as classification over long documents, is dominated by input cost. A workload with short prompts and long answers, such as drafting, is dominated by output cost. The right optimization depends on which case you are in.
Key points
- Providers bill separately for input and output tokens, quoted per million tokens.
- Output tokens cost several times more than input tokens.
- Cost = (input × input price + output × output price) / 1,000,000.
- You pay for the whole prompt on every call, including system prompt, history, and tool definitions.
- Model tier is usually the largest cost lever. Cached input, batch discounts, and long-context rules adjust the base price.
- Use the maximum output setting to compute a worst-case bound.
Exercise
A call sends 12,000 input tokens and the model replies with 800 output tokens. The input price is $2 per million tokens and the output price is $10 per million tokens. What is the cost of the call, and what share of the cost comes from output?
Show answer
Input: 12,000 × 2 = 24,000. Output: 800 × 10 = 8,000. The sum is 32,000, and dividing by 1,000,000 gives $0.032. Output accounts for 8,000 / 32,000 = 25% of the cost, even though it is only about 6% of the tokens (800 of 12,800). This workload is dominated by input cost.
Exercise
You plan a batch job of 20,000 calls. Each prompt has 1,500 input tokens and you set the maximum output to 600 tokens. Prices are $3 per million input tokens and $15 per million output tokens. What is the worst-case total cost?
Show answer
Per call, input is 1,500 × 3 = 4,500 and output at the cap is 600 × 15 = 9,000. The sum is 13,500, so one call costs at most $0.0135. For 20,000 calls the worst case is 20,000 × 0.0135 = $270. The real cost will be lower if replies usually stop before 600 tokens.