LLM Inference Cost Optimization: A Practical Guide to Cache Billing and Model Tiering

    This article explains why unchanged list prices can still lead to shifting LLM bills, and how engineering teams can take control of inference costs. It breaks down modern billing structures, the technical mechanics of prefix caching and its exact hit conditions, and how to restructure prompts to maximize cache reuse. It then covers routing tasks to the right model tier based on error cost rather than perceived difficulty, a two-stage draft-and-review pattern, and a formula for decomposing costs into actionable variables. A final checklist helps teams turn observability, routing, and budget guardrails into a repeatable cost optimization workflow.

    1. Background: Why “the price didn’t change” doesn’t mean “the cost didn’t change”

    Over the past year, major LLM providers have adjusted their pricing more and more frequently. Yet when many teams review their bills, they notice a counterintuitive pattern: the price list looks unchanged, but actual spending has clearly shifted. The reason usually isn’t the unit price itself, but the billing structure.

    The industry commonly breaks the cost of a single call into several parts: input tokens, output tokens, input tokens that hit the cache, and separate tiers that some providers set for long context or batch asynchronous tasks. What really determines your bill is the mix of these components in your workload.

    A typical observation from recent releases captures this well: when a new-generation model launched, input and output prices stayed at the tier of $2 and $10 per million tokens, while the cache price was pushed even lower, and high-complexity coding and terminal-operation tasks were steered toward a higher-capability tier. Abstracted out, this reflects two parallel industry trends:

    • Trend 1: Cache-side price cuts encourage reuse; full-price input on cache misses becomes the main cost driver.
    • Trend 2: Capability tiering, where simple tasks go to a low-cost tier and complex reasoning goes to a high-cost tier.

    For developers, this means the focus of cost optimization shifts from “pick a cheaper model” to “design a calling architecture that makes requests hit the cache as much as possible and lands each task on the right tier.”

    2. How cache billing works and what it takes to get a hit

    2.1 What prefix caching actually caches

    LLM inference has two phases: prefill and decode. During prefill, all input tokens go through the attention computation to generate the corresponding key-value cache (KV Cache); during decode, this cache is reused to produce the output token by token. Prefix caching means retaining the KV computation results for an “exactly identical prefix token sequence” on the server side for a period of time. If the next request shares the same prefix, that repeated computation is skipped.

    This leads to the hard conditions for a cache hit:

    1. The prefix must match token by token exactly, including whitespace, line breaks, punctuation, and capitalization;
    2. A hit is the longest common prefix counted from the beginning; any change in the middle invalidates everything after it;
    3. The cache usually has a time-to-live window and needs to be warmed up again after a long idle period;
    4. Only the input side can be reused; the output side cannot be cached.

    2.2 An example: what changes break the cache

    1. Request A prefix:
    2. [System prompt v3]
    3. [Tool definitions: search, run_sql]
    4. [Knowledge base snippet #128]
    5. Request B prefix:
    6. [System prompt v3]
    7. [Tool definitions: search, run_sql, send_mail] # one tool appended
    8. [Knowledge base snippet #128]

    Request B changes at the tool definitions, so the knowledge base snippet that follows cannot hit the cache even if its content is identical. The engineering fix is to place volatile content last: put the stable system prompt and tool definitions first, and user input, timestamps, random IDs, and retrieval results last.

    2.3 Restructuring prompts to improve hit rates

    1. [Stable layer] System role, output format constraints, safety policy
    2. [Semi-stable layer] Tool/function signatures, field dictionary, glossary
    3. [Session layer] Multi-turn history (append-only, never modify past content)
    4. [Volatile layer] Current user question, retrieved snippets, timestamps, trace_id

    Supporting engineering habits include: version-controlling prompts and avoiding mixing multiple versions within the same canary cycle; moving high-entropy fields like timestamps and request IDs to the end; using fixed chunking and fixed ordering for long documents to avoid prefix changes caused by retrieval-order jitter; and treating “cache-hit token ratio” as a first-class metric in monitoring rather than only looking at average latency.

    3. Model tiering: routing tasks to the right capability level

    3.1 The criterion isn’t “how hard it is,” but “how expensive a mistake is”

    A common mistake in practice is tiering by subjective difficulty. A more robust criterion is error cost and verifiability:

    Task characteristic Recommended tier Reason
    Classification, extraction, format conversion Low-cost tier Results are automatically verifiable, error cost is low
    Summarization, rewriting, routine Q&A Low-cost tier High semantic tolerance
    Multi-file refactoring, complex defect localization High-cost tier High error cost, long chains
    Long terminal/UI operation flows High-cost tier Strong dependencies between steps, high rollback cost

    3.2 Implementation of the routing layer

    Routing should not be scattered as if-else statements across business code, but consolidated into a standalone component that takes task characteristics as input and outputs the target tier and a budget cap:

    1. def route(task):
    2. if task.auto_checkable and task.risk == "low":
    3. return Tier.PRIMARY # low-cost tier
    4. if task.steps > 8 or task.rollback_cost == "high":
    5. return Tier.ADVANCED # high-cost tier
    6. return Tier.PRIMARY

    Three key design points: first, routing decisions must be observable, logged with a trace_id for later bill attribution; second, set budget guardrails that trip a circuit breaker when a single task exceeds its token or call-count limit; third, keep a fallback path so that when the high-cost tier is unavailable, requests fall back to the low-cost tier and results are marked as pending review, rather than failing outright.

    3.3 A two-tier approach: try first, escalate later

    For long-flow tasks, you can adopt a two-stage pattern of “low-cost tier drafts + high-cost tier reviews.” The first stage uses a low-cost model to produce a plan and patch draft; the second stage runs high-cost review only on high-risk segments. Compared with using the high-cost tier for the entire flow, this pattern significantly compresses costs in most scenarios while concentrating expensive compute on the parts that genuinely need deep reasoning.

    4. Cost estimation: breaking the bill into optimizable variables

    Don’t just compare unit prices. Use the decomposition below for estimation:

    1. cost per call = input tokens × input price × (1 - hit rate × cache discount)
    2. + output tokens × output price
    3. + retry count × cost per call

    The three actionable levers, in order, are: raise the hit rate, compress output length, and reduce the retry rate. Output length is often underestimated—asking the model to give the conclusion first and the rationale after, limiting redundant explanation, and using fixed templates for structured output often saves more than switching models. The retry rate, meanwhile, is strongly tied to prompt stability and idempotent tool-call design.

    It’s worth emphasizing that specific unit prices, cache discount ratios, and tier divisions should follow the official billing documentation of your chosen service for the current period; this article does not cite any unconfirmed figures.

    5. Implementation checklist

    1. Establish prompt versioning and change auditing, and freeze the prefix within a canary cycle.
    2. Reorder context from “stable layer → volatile layer,” moving high-entropy fields to the end.
    3. Bring cache hit rate, token distribution, and retry rate into the monitoring and alerting dashboard.
    4. Extract a standalone routing component and tier by error cost rather than subjective difficulty.
    5. Set token and call-count budget guardrails plus a fallback path for each task.
    6. Do a weekly bill attribution to pinpoint whether the hit rate dropped or tier drift occurred.
    7. If you use relevant Baidu AI Cloud capabilities, you can combine them with general cloud product capabilities such as its monitoring, alerting, and logging services to close the observability loop described above.

    6. Conclusion

    The way new-generation models are released—“list price unchanged, structure improved”—effectively hands optimization power back to the engineering side. Whoever can organize context into a stable prefix, route tasks to the right tier, and decompose cost into attributable variables will secure predictable cost savings without sacrificing quality. The hard part isn’t model selection, but the calling architecture itself.