A Practical Guide to LLM API Cost Optimization with Tiered Pricing

    This guide breaks down the three-tier pricing model used by modern LLM APIs—regular input, cached input, and output tokens—into a reusable cost model. It explains what drives cache hit rates, compares the cost structure of repeated knowledge-base queries versus one-off long-document processing, and outlines six engineering practices such as prompt template versioning, retrieval-first pipelines, output budget limits, asynchronous batching, three-part observability, and model routing. It also highlights common pitfalls to help teams control inference bills through architecture rather than unit price alone.

    1. Background: Pricing Structures Have Changed

    A new class of model pricing has emerged in the industry. A recent general-purpose LLM cut its API price by roughly 20% compared to the previous generation, while splitting the fee into three tiers: regular input, cache-hit input, and output, each priced per million tokens. The spread between tiers can reach one to two orders of magnitude. Cached input typically costs only a few percent of regular input, while output often costs several times more than input.

    This highlights a long-underestimated fact: in inference workloads, cost is no longer determined by “how many times you called the API,” but by “how many tokens are repeated and how many are generated.” If you still budget based on the intuition of “one request, one price,” your actual bill can easily deviate from expectations by several times.

    This article does not review any specific product. Instead, it abstracts this three-tier billing into a general cost model and provides a practical optimization methodology.

    2. Breaking the Bill into Three Variables

    Let I be the number of regular input tokens per request, H the number of cache-hit input tokens, and O the number of output tokens, with corresponding unit prices p_i, p_h, and p_o:

    1. Cost per request C = I * p_i + H * p_h + O * p_o

    Using a common industry scale of input 8, cache hit 0.4, and output 40 (per million tokens), the weight difference across the three tiers becomes clear:

    • Regular input: 8
    • Cache-hit input: 0.4 (about 5% of regular input)
    • Output: 40 (about 5x regular input)

    This leads to three engineering conclusions:

    1. Output is the most expensive part. When generation length is uncontrolled, output cost can account for more than half of the total bill. Compressing output is often more effective than compressing input.
    2. Caching is the only “discount coupon.” Only stable, reusable prefixes can hit the cache. Improving the hit rate reduces cost linearly with no side effects.
    3. Input and cache must be accounted for separately. Lumping both into a single “input cost” hides the real optimization space.

    3. What Determines the Cache Hit Rate

    Cache implementations vary across the industry, but they generally follow the basic principle of “prefix matching”: the system fingerprints the beginning of a request, and if it matches the prefix of a recent request, it reuses the already-computed intermediate state. This yields the key constraints for improving hit rates:

    • The prefix must be byte-for-byte stable. Dynamic content such as timestamps, random IDs, or usernames placed at the very front of the system prompt will invalidate the entire prefix.
    • Order matters. Put volatile content at the end, and stable instructions, specifications, and reference material at the beginning.
    • An expiration window exists. Caches typically have a lifetime. Requesting again after a long idle period requires recomputation, so “batching” is not always cheaper.
    • Granularity must be moderate. A shared prefix that is too short yields limited benefit, while content that is too long and exclusive cannot be reused by others.

    A recommended layered prompt structure:

    1. [Layer 1: Globally stable] Role definition, output format constraints, glossary, safety policy
    2. [Layer 2: Stable within a task family] Fixed instructions and a few examples for a class of tasks
    3. [Layer 3: Stable within a session] History and intermediate conclusions shared across turns
    4. [Layer 4: Volatile per request] Current user input, timestamp, request ID

    4. Cost Structure of Two Typical Workloads

    The original text offers a practical observation: repeatedly processing the same material has a completely different cost structure from reading a new set of material every time. Quantifying this:

    Scenario A: Repeated Q&A over a knowledge base. A 200,000-token document is reused across 500 queries. If the document sits in a stable prefix and fully hits the cache, its input cost is charged at 0.4; if it misses, it is charged at 8—a 20x difference.

    Scenario B: One-off long-document processing. Every input is a new document, so the hit rate is naturally near zero, and cost falls almost entirely on regular input and output. The only effective measures here are reducing input redundancy and output length.

    The strategies are entirely different: A focuses on placing reusable content up front and keeping it fixed; B focuses on retrieval instead of full-context feeding, meaning recalling the most relevant fragments first and then passing them to the model.

    5. Six Practical Engineering Practices

    1. Version your prompt templates. Manage prefixes with a template ID, and treat any change as a new version to avoid “one word changed, full cache invalidated” going unnoticed.
    2. Retrieve first, chunk long documents. Use vector retrieval or keyword recall to compress candidates down to the necessary fragments. Input tokens often drop by an order of magnitude.
    3. Enforce a hard output budget. Explicitly cap the maximum generation length in request parameters, and prefer constrained formats for structured output to reduce useless elaboration.
    4. Batch and go asynchronous. Send latency-insensitive offline tasks through an asynchronous queue, and use message queues and container platforms for elastic scaling to smooth out peak costs.
    5. Build three-part observability. Instrument separate metrics for regular input, cache-hit input, and output token counts. Combine with logging services and monitoring alerts, aggregate by business line, scenario, and template, and build a per-task cost dashboard.
    6. Route across model tiers. Send simple tasks to smaller models and escalate only complex ones, avoiding “every request uses the most expensive tier.”

    Illustrative accounting logic:

    1. def estimate_cost(i_tokens, h_tokens, o_tokens,
    2. p_i=8.0, p_h=0.4, p_o=40.0):
    3. # Unit price: per million tokens
    4. return (i_tokens * p_i + h_tokens * p_h + o_tokens * p_o) / 1_000_000

    6. Common Pitfalls

    • Watching unit price but ignoring structure. A 20% price cut means nothing if the hit rate drops from 60% to 10%—total cost can actually rise.
    • Treating the cache as strongly consistent storage. The cache is a performance and cost optimization; business correctness must not depend on it.
    • Ignoring the output side. Verbose reasoning traces and repeated explanations are the most easily amplified cost items.
    • Optimizing everything at once. Establish observability first, use data to locate the top-consuming scenarios, and then address them one by one.

    7. Conclusion

    Three-tier billing explicitly prices “repetition” and “generation,” which in effect guides developers toward writing more structured prompts and more restrained outputs. For teams, the correct order is: first unify the accounting standard, then optimize prefix stability and retrieval strategy, and finally close the loop with model tiering and output constraints. General-purpose capabilities from cloud vendors such as Baidu AI Cloud (object storage, message queues, container platforms, logging services, monitoring alerts) can serve as the foundation for this cost governance system, but what truly determines the bill is the architecture design itself.