Lower Unit Prices Don't Always Lower Your LLM Bill: A Practical Guide to Token Cost Accounting
This article explains why lower per-token prices do not automatically reduce LLM spending. It covers the three parts of token billing, the reasons total cost can rise after a price cut, and how to estimate the cost of each successful task. It also outlines model tiering, engineering optimizations, monitoring, and a checklist for controlling spend.
1. Unit Price Is Not Cost
LLM services usually bill by token. A unit price table makes it easy to compare providers, but it only shows the price of one token. It does not show what a task actually costs. The number of input tokens, output tokens, and cache hits in a task together determine the bill. When you choose a model, focus on the cost per task rather than the price per token.
2. The Three Components of Billing
- Input tokens: These include the system prompt, conversation history, retrieved document snippets, and tool results. In multi-turn conversations and AI agent scenarios, input tokens usually make up most of the total consumption.
- Output tokens: These include the model’s responses, tool call arguments, and reasoning content that is billed as output. In most pricing plans, the output price is noticeably higher than the input price.
- Caching: When the prefix of a request matches a previous request, the input may be billed at the cache-hit price, which is significantly lower than the cache-miss price. The cache hit rate depends on whether the prompt structure stays stable.
3. Why Total Cost Can Rise When Unit Prices Fall
After a model price cut, teams often make more calls or allow longer context windows. As a result, total spending can increase instead of decrease. Common causes include:
- Input growth: Teams add more retrieved results or history to improve accuracy, and input tokens can multiply.
- Multi-step calls: An agent may repeat planning, execution, and verification within one task, so the number of calls far exceeds that of a single-turn Q&A.
- Longer outputs: More capable models may give more detailed explanations or reasoning, which increases output tokens.
- Retry amplification: When a failed task is rerun in full, the cost grows linearly with each failure.
For this reason, the core metric should be the cost per successful task, not the cost per call. Failed calls also incur charges, so they must be included in total cost.
4. Building a Cost Estimation Model
Use the following formula to estimate the cost of a single task:
Cost per task = (cached input tokens × cache-hit price + uncached input tokens × cache-miss price + output tokens × output price) × number of rounds ÷ task success rate
Example code (the parameters are demo values; replace them with the provider’s published prices):
def task_cost(cached_in: int,uncached_in: int,out_tokens: int,rounds: int,success_rate: float,price_cached: float,price_uncached: float,price_out: float,) -> float:"""Estimate the cost of each successful task from prices per million tokens.The unit matches the price unit in the pricing table."""if not 0 < success_rate <= 1:raise ValueError("success_rate must be in the range (0, 1]")per_round = (cached_in * price_cached+ uncached_in * price_uncached+ out_tokens * price_out) / 1_000_000return per_round * rounds / success_rate
This formula is a simplified model. In multi-round scenarios, the context grows with each round, so calculate the input for each round separately and then add them up. For budget forecasts that require higher accuracy, use actual call logs to calculate costs round by round.
5. Tiering Models by Task
Not every task needs the strongest model. Group tasks by difficulty and error tolerance:
- High-value complex tasks, such as complex code changes or in-depth analysis reports: Use the most capable model, accept the higher unit price, and set a limit on the number of calls.
- Routine tasks, such as format conversion, text summarization, and content classification: Use a lightweight model and prioritize response speed and cost.
- Batch and offline tasks: Prefer batch interfaces or low-priority queues, and run them during off-peak hours.
A routing layer can select a model automatically based on task type, input length, and historical failure rate. Test the routing rules regularly against a test set so that outdated rules do not reduce quality.
6. Engineering Optimizations
- Keep the prompt prefix stable: Place fixed system instructions at the start of the request and dynamic content after them to improve the cache hit rate.
- Control context length: Use rolling summaries for conversation history, truncate retrieval results by relevance, and prevent unlimited context growth.
- Constrain output: Control output tokens with explicit output format requirements and maximum output length parameters.
- Set budget and round limits: Define the maximum number of calls and the maximum spend for each agent task. When a limit is exceeded, stop the task and hand it to a human.
- Distinguish error types: Retry only transient errors, such as network timeouts. For content errors, adjust the prompt or parameters first, and then run the task again.
7. Monitoring and Evaluation Loop
Track the following metrics and aggregate them by day or by version:
- Average input and output tokens per call
- Cache hit rate
- Average cost per task
- Task success rate
- Cost per successful task (the core metric)
You can collect this data through a logging service, and set thresholds in your monitoring and alerting system. For example, trigger a notification when daily cost exceeds a set percentage of the budget.
Whenever you change the model or modify a prompt, compare quality and cost on the same batch of test samples. Release the change only when both metrics meet their targets. Base quality evaluation on your own business samples. Public leaderboard scores can serve as a reference, but they cannot replace validation with your own data.
8. Implementation Checklist
- Have you counted the cost of failed calls?
- Have you calculated cache-hit and cache-miss input tokens separately?
- Have you defined model tiers and routing rules for different tasks?
- Have you set budget alerts and call count limits?
- Have you validated the change in quality with your own test set?
Whether you use Baidu AI Cloud or a self-hosted inference environment, these methods apply. The key is to break down the bill at the task level and use measurable data to guide model selection and optimization.
