Cost Engineering Practices Behind Large Model API Price Cuts

    This article breaks down how large model API pricing is structured across input, cached input, and output tokens, and why cache hit rate matters more than headline unit prices. It explains a per-request cost model, then walks through four cost optimization levers: splitting prompts into stable and variable layers, governing retrieved fragments in RAG pipelines, constraining output length, and instrumenting token usage. It also covers cache-friendly service design, common pitfalls, and how to compare quality and cost before and after optimization.

    1. Why a Single Price Change Affects the Whole Call Chain

    A cloud provider recently adjusted the billing for its large model service: the flagship tier cut input and output unit prices by roughly 20% each, while cached input prices dropped by about 60%, landing at approximately $8, $0.40, and $40 per million tokens for input, cached input, and output respectively. The lightweight tier kept the previous generation’s pricing, at roughly $2, $0.10, and $10 per million input, cached input, and output tokens.

    For developers, the numbers themselves matter less than the cost structure they expose: a single call’s bill is determined by three token categories, and the price gap between them can span one to two orders of magnitude. The focus of cost optimization is therefore shifting from “pick a cheaper model” to “organize context more precisely.”

    2. Decomposing the Bill: How the Three Token Categories Are Priced

    General large model billing usually splits a request into three parts:

    • Input tokens: all prompt content in the current request that is not reused. This has the highest unit price.
    • Cached input tokens: reusable context that exactly matches the prefix of previous requests and is served from cache. The unit price is significantly lower than regular input.
    • Output tokens: the generated portion. The unit price is usually higher than input, because inference requires more computation.

    This yields a basic estimation formula:

    1. cost per call = in_tokens/1e6 * P_in
    2. + cache_tokens/1e6 * P_cache
    3. + out_tokens/1e6 * P_out

    Every improvement in cache hit rate is equivalent to swapping the most expensive part of the bill for the cheapest one. When the cache price is only one twentieth of the regular input price, content that must be resent every turn — long system prompts, fixed knowledge blocks, few-shot examples — becomes the most worthwhile optimization target.

    3. Four Levers for Cost Optimization

    3.1 Context Layering and a Stable Prefix

    Split the prompt into a “stable layer” and a “variable layer”: the stable layer holds system instructions, tool descriptions, domain knowledge, and examples; the variable layer holds only the current user input. The stable layer must stay byte-for-byte identical (no timestamps, random IDs, or dynamically ordered injections) to be cacheable.

    1. [stable prefix] system role + tool schema + domain knowledge + few-shot
    2. [variable suffix] current user question + retrieved fragments

    3.2 Fragment Governance in Retrieval Augmentation

    Retrieved results are inherently unstable, so place them at the end of the prompt. Also deduplicate, truncate, and rank fragments by relevance instead of stuffing entire documents into the context. A lightweight tier model can handle recall and reranking first, then hand off generation to the flagship tier, forming a cost gradient.

    3.3 Output Length Constraints

    Output unit prices are often higher than input, so compressing output pays off directly. Tactics include: enforcing a structured format, requiring the conclusion before the supporting evidence, setting a maximum length, and splitting long-form generation into segments that reuse the cache from earlier text.

    3.4 Call Chain Observability

    No measurement, no optimization. Log the three token counts and cache status for every call, tagged by business scenario:

    1. record = {
    2. "scene": "code_review",
    3. "in_tokens": 5200,
    4. "cache_tokens": 4800,
    5. "out_tokens": 600,
    6. "cache_hit": True,
    7. }
    8. # cost per task = sum of each token segment * its unit price

    4. Engineering Practice: Cache-Friendly Service Design

    • A single prefix construction entry point: route all requests through one function that assembles the prompt, preventing prefix drift caused by each business line writing its own.
    • Concurrency and batching: schedule offline batch jobs together to raise the probability of prefix reuse.
    • Fallback strategy: during peak hours, route non-critical requests to the lightweight tier to protect core paths.
    • Capacity and rate limiting: set thresholds on token consumption with monitoring and alerting, and trip the circuit breaker when growth becomes abnormal.
    • Data-side coordination: publish knowledge base updates in versions, so frequent edits do not invalidate the cache wholesale.

    5. Common Pitfalls

    1. Watching unit price but ignoring structure: a 20% price cut can still leave a higher actual bill if the cache hit rate is zero.
    2. Putting dynamic content first: placing a session ID or the current time at the start of the prompt makes the entire prefix unreusable.
    3. Over-compressing input: removing necessary constraints raises output length and rework rate, increasing total cost.
    4. Missing regression evaluation: compare quality and cost on the same evaluation set before and after optimization, avoiding “saved money, lost quality.”

    6. Conclusion

    Price changes are external variables; cost structure is what you control. Split context into stable and variable layers, route tasks to different tiers by difficulty, and turn token usage into an observable metric — and you can keep pushing down cost per task without degrading quality. If you need more complete model services, elastic compute, and supporting general cloud capabilities such as object storage, log services, and monitoring and alerting to carry these practices, Baidu AI Cloud can provide relevant support.