Understanding LLM API Pricing: How Token Billing Logic Determines Your Real Costs
A practical breakdown of how large language model API pricing works, why a higher per-token price does not always mean a higher bill, and how input, output, and cache-hit billing differ. The article explains the three-part billing formula, compares subscription plans with pay-as-you-go usage, and outlines engineering practices for controlling token costs.
1. The Phenomenon: A 5x Unit Price Does Not Mean a 5x Bill
Recently, flagship model APIs have seen substantial price increases over the previous generation. Output unit prices have risen to several times the previous level, while input and cache-hit prices sit at different magnitudes. The typical developer reaction is “we can no longer afford this,” but that reaction misreads the billing model.
To judge the real cost change, you have to go back to the billing formula:
Total cost = input tokens × input unit price+ output tokens × output unit price+ cache-hit tokens × cache unit price
The unit price increase is completely different for each of the three variables, and the token mix varies enormously across workloads. In a long-context, high-reuse code repository Q&A scenario, cache hits may account for more than 70% of tokens. In a short-prompt, long-generation writing scenario, output may account for more than 80%. Faced with the same price announcement, the former sees a limited cost increase, while the latter may genuinely double. So “5x” is a conclusion about unit price, not about the bill.
2. Breaking Down the Three-Part Billing Model
1. Input Side: Paying for Context Length
The input unit price is usually the lowest, but it is the only component that grows linearly with context. When you stuff an entire document or an entire code repository into the prompt, input tokens quickly become the main cost source.
2. Output Side: The Most Expensive Tier
Output uses autoregressive generation, which has higher computational density than input, so its unit price is typically several times the input price. Any task that “generates long text” naturally tilts cost toward output.
3. Cache Hits: An Undervalued Cost Lever
When multiple requests share the same prefix (system prompt, tool definitions, long documents), the server side can reuse the already computed intermediate state, and these tokens are billed at a very low price. The cache unit price is often a fraction of the input unit price, which is exactly why long-context scenarios can still control costs during a price-increase cycle.
Illustrative code:
def estimate_cost(in_tok, out_tok, cache_tok, p_in, p_out, p_cache):return in_tok * p_in + out_tok * p_out + cache_tok * p_cache# Keeping the stable prefix cached can significantly lower total costmessages = [{"role": "system", "content": STABLE_PROMPT}] + dynamic_turns
3. Subscription vs. Pay-as-You-Go: How to Choose
Two paths are common in the industry: an AI coding subscription with a fixed monthly fee in exchange for a quota, and pay-as-you-go billing on an open platform where you pay for what you use.
- Subscription: costs are predictable, suitable for high-frequency, stable daily coding with little single-user variance; the risk is that unused quota becomes a sunk cost, and the quota deduction rules (per request, per token, whether input and output are distinguished) all differ.
- Pay-as-you-go: suitable for teams with large traffic fluctuations, a need for elastic scaling, or a need for canary comparisons across multiple models; the risk is that without budget guardrails, spending can easily get out of control.
The decision rule can be simplified to one line: choose a subscription when average monthly usage is stable and close to the plan’s quota ceiling; choose pay-as-you-go when usage fluctuates heavily or you are still in a validation phase.
4. Four Engineering Practices for Cost Governance
- Prefix stabilization: keep the system prompt, tool schemas, and reference material fixed at the very front of the request to maximize cache hits.
- Context trimming: use retrieval to pull in relevant snippets instead of stuffing in everything.
- Output constraints: set clear length limits and structured output requirements to avoid uncontrolled generation.
- Observability: instrument token usage by business dimension in logging services and monitoring alerts, and set up budget threshold alerts.
If you use Baidu AI Cloud capabilities, you can combine its model services with general-purpose logging and monitoring products to build the metering and alerting pipeline described above.
5. Conclusion
A price increase is a signal, not a verdict. What really determines the bill is the token mix and cache utilization. Only by aligning the billing formula, workload characteristics, and governance practices can you keep costs under control during a price-increase cycle.
