LLM API Pricing Breakdown: How Input, Output, and Cached Tokens Affect Cost Optimization

    Most teams compare LLM APIs using a single number — price per thousand tokens — and miss the billing logic behind it. Mainstream services split charges across three dimensions: input tokens, output tokens, and cache-hit tokens. This article explains what each dimension means, why price tiers exist and how they map to capability levels, how to build a quantitative cost estimate for your own workload, and four practical levers for cutting spend: maximizing cache hits, routing by task difficulty, trimming input and constraining output, and abstracting the model access layer. Code samples in Python illustrate the estimation logic and a pluggable client design.

    Introduction: Why the Pricing Structure Is Worth Studying

    When integrating an LLM API, many teams focus on a single number — how much a thousand tokens costs — and overlook the billing structure behind it. In practice, mainstream LLM services break charges into three dimensions: input tokens, output tokens, and cache-hit tokens. Understanding the differences between them is the prerequisite for any realistic cost assessment or architecture decision.

    This matters most when a new generation of models arrives with a noticeable price increase — for example, an output-side price several times higher than the previous generation. Switching blindly or refusing to switch are both poor engineering decisions. The right approach is to go back to the pricing itself, understand the relationship between price, capability, and value, and then decide in the context of your own workload.

    1. The Three Billing Dimensions of an LLM API

    1.1 Input Tokens

    Input tokens cover everything sent to the model: the system prompt, conversation history, the user’s current message, and any retrieved content stitched into the request for RAG. Input tokens are usually billed at a lower unit price because they represent the “read” phase of model processing.

    1.2 Output Tokens

    Output tokens are what the model generates and returns. Generation is autoregressive token-by-token decoding, which costs significantly more compute than input processing. As a result, the output unit price is often several times the input unit price. This is the key to reading the price ladder: when a generation of models “doubles output pricing,” the applications hit hardest are those that produce long, generation-heavy responses.

    1.3 Cache-Hit Tokens

    Cache hits are a billing item that has become common in recent years. When repeated requests reuse the same prefix — a fixed system prompt, a long document context — the provider charges far less than the normal input rate for the portion served from cache. The cache-hit price is frequently one tenth of the standard input price or lower, making it the core lever for reducing cost in long-context, multi-turn scenarios.

    2. Where the Price Ladder Comes From: Pricing Aligned to Capability

    Models from the same vendor are typically priced in tiers. A common pattern is that a new high-end model is priced in line with the industry’s leading high-end models. The logic here is not simply “a price increase,” but a revaluation after capability alignment.

    When a model reaches a given capability level in reasoning, long-context handling, and complex task completion, its price naturally moves toward comparable products in the same tier. So when you see a new generation priced clearly above its predecessor, the more relevant questions are:

    • Did the model’s capability jump correspondingly?
    • Is the price competitive within the same capability tier?
    • Does your workload actually need that capability tier?

    Put differently, what a price is aligned to determines its value reference frame. Running a simple classification task on a high-end model is a clear waste of money; forcing a cheap model to handle complex reasoning can shift the cost into quality problems and rework.

    3. Cost Assessment: Building a Quantifiable Model

    A rational selection decision has to rest on something measurable. Build a token consumption profile for each type of workload, then estimate monthly cost along these lines:

    1. # Illustrative cost estimation logic (price unit: currency per 1K tokens; replace with actual quotes)
    2. def estimate_monthly_cost(
    3. input_price, # input unit price
    4. output_price, # output unit price
    5. cache_price, # cache-hit unit price
    6. req_count, # number of requests per month
    7. avg_input_tokens, # average input tokens
    8. avg_output_tokens, # average output tokens
    9. cache_hit_ratio # cache hit ratio (0~1)
    10. ):
    11. # Input tokens served from cache are billed at the cache price;
    12. # the rest are billed at the normal input price
    13. cached_input = avg_input_tokens * cache_hit_ratio
    14. normal_input = avg_input_tokens * (1 - cache_hit_ratio)
    15. cost_per_req = (
    16. normal_input / 1000 * input_price
    17. + cached_input / 1000 * cache_price
    18. + avg_output_tokens / 1000 * output_price
    19. )
    20. return cost_per_req * req_count

    With this model, a team can quickly compare real spend across model tiers and caching strategies instead of being misled by surface-level unit prices.

    4. Four Practical Paths to Lower Cost

    4.1 Maximize Cache Hits

    Put stable content — system prompts, tool definitions, long documents, few-shot examples — at the front of the request and keep it byte-for-byte identical so it can be cached. The more stable the prefix and the more often it is reused, the larger the savings from the cache price.

    4.2 Tier Model Selection by Task Difficulty

    Do not send every request to the most expensive model. Build a routing layer that hands simple tasks (formatting, classification, extraction) to a lightweight model and reserves complex reasoning and high-quality generation for the high-end model. This is one of the most effective cost controls available.

    4.3 Trim Input and Constrain Output

    • Input side: remove redundant context and avoid stuffing unrelated content into the prompt.
    • Output side: explicitly constrain answer length and format to reduce unnecessary generated tokens, which directly cuts the most expensive item.

    4.4 Abstract a Pluggable Model Access Layer

    Architecturally, hide model calls behind a unified abstract interface so business code is not tightly coupled to one specific model. When a model raises prices or is superseded, you can switch quickly and validate with a canary rollout instead of making large-scale changes.

    1. # Illustrative abstraction layer: makes switching and canary testing across models easier
    2. class LLMClient:
    3. def complete(self, prompt, **kwargs):
    4. raise NotImplementedError
    5. class RoutedLLMClient(LLMClient):
    6. def __init__(self, light_model, heavy_model, router):
    7. self.light_model = light_model
    8. self.heavy_model = heavy_model
    9. self.router = router # decides which model to use based on task complexity
    10. def complete(self, prompt, **kwargs):
    11. model = self.heavy_model if self.router.is_complex(prompt) else self.light_model
    12. return model.complete(prompt, **kwargs)

    5. Conclusion: Let Pricing Return to a Value Judgment

    Faced with a price adjustment in a new model generation, rather than arguing about the absolute percentage increase, build a three-layer judgment framework: first look at the billing structure (input / output / cache), then at the value reference frame within the relevant capability tier, and finally at a cost estimate based on your own workload’s real token profile.

    Large model service platforms such as Baidu AI Cloud’s Qianfan typically offer multiple model tiers, caching mechanisms, and usage monitoring. Developers can use general cloud capabilities like monitoring alerts and log analysis to keep tuning their call strategy. Ultimately, model pricing is a question of value rather than a question of numbers — pick the right tier, use caching well, and keep output under control, and you can strike a sustainable balance between quality and cost.