LLM API Pricing Explained: How Token Billing Works and How to Control Costs

    This article breaks down how token-based billing works across major LLM APIs, covering input tokens, output tokens, and cached input. It shows how to estimate monthly costs before choosing a model tier, then walks through four practical cost-reduction paths: stabilizing prompt prefixes, compressing context, constraining output length, and routing across model tiers. It also covers subscription quota discipline, observability and capacity planning, and common mistakes that quietly inflate LLM bills.

    1. Why Token-Based Billing Can Spiral Out of Control

    As LLM applications move from demos into production, cost structure often becomes a bottleneck before quality does. The reason is that billing is not based on the number of requests but on the number of tokens the model actually processes. On the input side, this includes system prompts, conversation history, retrieved documents, and the user’s question. On the output side, it is the full content the model generates. As context accumulates and more retrieved snippets are stuffed in, the token count per request grows linearly or even compounds, and the bill grows with it.

    One frequently overlooked fact: output tokens usually cost significantly more per token than input tokens, and in some service tiers the gap can be several times. This means “having the model write 500 more words” and “having the model read 500 more words” are not equivalent in cost. So the first step in cost governance is not cutting features, but establishing an observable usage structure.

    2. The Three Billing Components

    Billing items across mainstream LLM services generally fall into three categories. Actual prices are set by each provider per model tier and can change at any time, so always refer to the official pricing page. This article does not cite any specific numbers:

    Billing item What it covers Cost characteristic
    Input tokens Prompts, conversation history, retrieved documents Grows linearly with context length
    Output tokens Content generated by the model Highest unit price, and hard to predict
    Cached input tokens Repeated prefix portions billed at a lower rate Lowest unit price, depends on call patterns

    The cached-input item is essentially an economic incentive for prefix stability: when multiple requests share an identical prompt prefix, that portion can be reused and billed at a lower price. This leaves engineering teams with a clear optimization lever.

    3. Estimating Usage: Do the Math Before Picking a Tier

    Before evaluating any model tier, run an offline estimate first. As a rough guide, one token corresponds to roughly one Chinese character, while English is more token-efficient. The following pseudocode estimates monthly cost:

    1. # Illustrative cost estimate; read unit prices from the provider's official pricing page
    2. def estimate_monthly_cost(calls, in_tok, out_tok,
    3. p_in, p_out, p_cache, cache_ratio=0.0):
    4. in_cost = calls * in_tok * p_in / 1_000_000
    5. out_cost = calls * out_tok * p_out / 1_000_000
    6. cached = calls * in_tok * cache_ratio * p_cache / 1_000_000
    7. return in_cost + out_cost - cached

    The value of this estimate is that it exposes the real structure. If output dominates, the optimization focus should be controlling generation length. If input dominates, prioritize context trimming and cache reuse.

    4. Four Practical Cost-Reduction Paths

    1. Stabilize the prefix. Keep the system prompt, tool descriptions, and output format constraints fixed at the very front of the request with no dynamic concatenation, and move volatile content such as timestamps and user identifiers to the end. This can significantly improve cache hit rates.
    2. Compress context. For long documents, summarize in segments before passing them in, replacing the original text with a structured summary. In retrieval scenarios, add reranking and truncation thresholds instead of “stuffing the entire corpus into the prompt.”
    3. Constrain output length. Explicitly limit word count, number of items, and format, combined with structured output. This reduces token consumption and lowers downstream parsing costs.
    4. Route across model tiers. Classify tasks by difficulty: send simple classification, extraction, and rewriting to lighter tiers and complex reasoning to higher-capability tiers, using rules or a small model as a front-end discriminator.
    1. def route(task):
    2. return "light" if task["complexity"] <= 2 else "strong"

    5. Subscription Quota Discipline

    Some services offer monthly quotas or subscription plans. Their unit cost is usually lower than pay-as-you-go, but they introduce quota management and queuing constraints. The practical recommendation is to reserve elastic quota for interactive scenarios and schedule deferrable batch jobs (such as offline labeling or bulk summarization) during off-peak hours. Also build a quota dashboard that aggregates consumption by project rather than by individual, so a single task does not consume most of the monthly quota.

    6. Observability and Capacity Planning

    Cost optimization must be built on measurement. Collect input tokens, output tokens, cache hit ratio, time to first token, and retry counts for every call, and build dashboards broken down by business line with day-over-day alerts. For capacity planning, estimate resource needs by multiplying peak QPS by average token volume, then combine rate limiting and fallback strategies to ensure stability. If the application runs on a container platform, you can add cost attribution per call chain using monitoring, alerting, and logging services.

    7. Common Pitfalls

    • Looking only at unit price, not structure: a lower-priced tier can raise retry rates and end up costing more overall.
    • Over-pursuing long context: irrelevant content not only adds cost but also dilutes attention and lowers output quality.
    • Ignoring cache invalidation: a single dynamic variable in the prompt can make an entire prefix unusable for reuse.
    • Skipping regression evaluation: cost-reduction changes should be validated against a fixed evaluation set to avoid quality regressions.

    8. Conclusion

    Managing the cost of LLM calls comes down to turning an “uncontrollable bill” into “decomposable engineering metrics”: stabilize the prefix, compress context, constrain output, and route across tiers, supported by quota discipline and an observability system. In cloud environments such as Baidu AI Cloud, these methods can be implemented alongside common capabilities like object storage, message queues, container platforms, and monitoring and alerting, forming a sustainable, iterative cost-governance loop.