Understanding LLM API Pricing: A Practical Guide to Cost Optimization

    This article breaks down how large language model API billing actually works and why the listed per-token price rarely matches the final bill. It explains the three billing components—cache-hit input, cache-miss input, and output—along with their typical price gaps, presents a simple cost model with worked examples, and details four practical optimization techniques: prompt layering with a stable prefix, context governance, output constraints, and observability with cost regression. It also covers common mistakes that quietly inflate spending. The guidance applies to general LLM services and AI coding tool integrations, and can be implemented with general-purpose capabilities in relevant Baidu AI Cloud products.

    1. Why the Unit Price Is Not Your Bill

    When estimating LLM API costs, the most common mistake developers make is looking only at the output price. The actual cost of a single call has three parts: input that hits the cache, input that misses the cache, and the output the model generates. A common pricing structure in the industry is roughly ¥1.3 per million tokens for cache-hit input, ¥13 per million tokens for cache-miss input, and ¥68 per million tokens for output. Putting these three numbers side by side leads to two important conclusions.

    First, there is an roughly fivefold price gap between input and output, making output the most expensive part. Second, the gap between cache hits and cache misses is roughly tenfold, which means how often your context is reused often matters more to the bill than differences in the model’s own capabilities.

    So the first principle of cost optimization is: reduce output first, then improve the cache hit rate, and only then shrink the volume of cache-miss input. Reverse that order and the payoff drops sharply.

    2. Precise Definitions of the Three Billing Items

    2.1 Cache-Hit Input

    This is the prompt prefix of the current request that exactly matches content recently cached. The server can reuse the intermediate computation results directly, with no need to re-encode. It is the cheapest component and the main cost source for long-context scenarios and fixed system prompts.

    2.2 Cache-Miss Input

    This is the part that must be recomputed, including a system prompt seen for the first time, changing user input, and retrieved document fragments stitched into the context. It is mid-priced, but it easily spirals out of control as the context grows.

    2.3 Output

    This is the part the model generates, billed per generated token at the highest unit price. Long chains of thought, verbose summaries, and repeated explanations all scale this cost linearly.

    It is worth stressing that caching typically matches on the longest common prefix and is subject to lifecycle and capacity limits. Any tiny change inside the prefix degrades everything after it into a cache miss.

    3. Cost Model: Doing the Math

    Let a single call have (I) input tokens and (O) output tokens, with a cache hit rate of (h). The cost per call can be approximated as:

    1. Cost = (h * I) * 1.3/1e6
    2. + ((1 - h) * I) * 13/1e6
    3. + O * 68/1e6

    Plugging in a typical scenario: 8,000 input tokens and 800 output tokens.

    • No cache (h=0): 0.104 + 0.0544 ≈ ¥0.158
    • High hit rate (h=0.9): 0.0094 + 0.0104 + 0.0544 ≈ ¥0.074

    The same business logic costs roughly twice as much purely because of a different cache hit rate; and if the output is cut in half, the cost drops by more than another 30%. This shows the optimization focus should be on “prefix stability” and “output length control.”

    4. Four Actionable Optimization Techniques

    4.1 Prompt Layering and Prefix Fixing

    Split the prompt into a “stable layer—semi-stable layer—dynamic layer”: put the system role and rules first and keep them unchanged over the long term; place tool descriptions and few-shot examples next; put the user question and retrieved results last. Example:

    1. [Stable layer] You are a rigorous technical assistant and follow the output rules below... (unchanged long-term)
    2. [Semi-stable layer] Available tool list and invocation constraints... (changes only on version updates)
    3. [Dynamic layer] User question + retrieved fragments + timestamp... (changes every time)

    4.2 Context Governance

    For multi-turn conversations, keep only the necessary history; for long documents, segment and summarize first before passing them in. Move volatile information (timestamps, random IDs) to the end of the prompt to avoid polluting the prefix.

    4.3 Output Constraints

    Replace “be as detailed as possible” with explicit word counts, structure, and field requirements; for structured scenarios, constrain the output to JSON directly to cut explanatory text. Example:

    1. resp = client.chat(
    2. messages=[{"role": "user", "content": prompt}],
    3. response_format={"type": "json_object"},
    4. max_tokens=600,
    5. )

    4.4 Observability and Regression

    Set up call logging that records input/output tokens, hit rate, and cost per call, with dashboards broken down by business line. Run a cost regression on every prompt change to prevent “better results, double the bill.”

    5. Common Pitfalls

    • Assuming caching applies automatically to all requests: a mismatched prefix invalidates it.
    • Compressing only the input while ignoring the output: output has the highest unit price, so the savings get canceled out.
    • Putting dynamic content at the front: a single timestamp change forces the entire context to be recomputed.
    • Overlooking retries and stream interruptions: failed retries are billed again, so idempotency and backoff are needed.

    6. Conclusion

    The cost of an LLM API is not determined by the unit price alone, but by “input structure + cache hit rate + output length” together. Make the prompt a stable prefix, subtract from the context, turn the output into a constrained structured result, and pair that with token-level observability and regression—this brings the cost per unit of business down to a reasonable range without sacrificing quality. The approach applies to a wide range of LLM services and AI coding tool integrations, and can also be implemented around general-purpose capability categories in relevant Baidu AI Cloud products.