Cost Engineering for LLM APIs: Why Unchanged Token Prices Can Still Raise Your Bill

    This article explains why stable per-token pricing does not guarantee a stable bill when a new-generation LLM API is released. It breaks the total cost into a computable formula covering input tokens, output tokens, cache hit rate, and retries, then shows why cost per task is a more business-relevant metric than unit price. The article also covers practical ways to control output inflation through structured output, length budgets, sampling parameters, summarization, and result caching, along with architecture patterns that keep prefixes stable so prompt caching actually works. It closes with guidance on where upgraded model capabilities pay off and a checklist for model selection and cost governance.

    1. The Phenomenon: Unchanged Unit Price Does Not Mean an Unchanged Bill

    A cloud provider recently opened API access to a new-generation general-purpose LLM. The official message was that capabilities had improved significantly while the billing unit price stayed at the previous generation’s level: input is priced per million tokens, output costs more than input, and input tokens that hit the cache receive a substantial discount (effectively less than 80% of the normal rate for repeated context). At the same time, a third-party evaluation suite raised its composite intelligence index from the low fifties to around sixty, putting it on par with another model in the same tier.

    Many teams relax a little when they see “no price increase,” but the real bill is determined by four variables, not by unit price alone:

    1. Input token volume: prompts, system instructions, retrieved snippets, and accumulated conversation history;
    2. Output token volume: how much the model “wants to say,” which is the least controllable item;
    3. Cache hit rate: whether repeated prefixes are recognized by the billing system and discounted;
    4. Cost per task: the average total cost of completing one business task.

    One key point from the original material deserves emphasis: the new-generation model uses about 20% more output tokens to complete the same task than the previous generation. Unit price stays flat, output grows, and the cost per task can actually rise. This is the classic “output inflation” problem, and it is the part most easily overlooked in cost engineering.

    2. Turning the Bill into a Computable Formula

    Before integrating any model, teams should write down their own cost model instead of only reading the price list:

    1. cost per task =
    2. input tokens × input unit price
    3. + output tokens × output unit price
    4. + (1 - cache hit rate) × cacheable input tokens × input unit price
    5. + retry count × cost per attempt

    Here, “cacheable input tokens” refers to the stable prefix that repeats across a session, such as the system prompt, tool definitions, and long documents. The essence of the cache discount is to turn repeated billing of that portion into a one-time charge.

    An illustrative comparison (numbers are only for demonstrating the method and do not represent any specific product): if a task uses 10,000 input tokens and 2,000 output tokens, cost is dominated almost entirely by the output unit price when the cache hit rate is zero; when the hit rate for the stable prefix rises above 70%, input-side spending can drop by more than half. This also explains why long-context and multi-turn agent scenarios are unusually sensitive to cache design.

    3. Cost per Task: A Metric Closer to the Business Than Unit Price

    Evaluation organizations often use “cost per task” to compare models side by side: two models in the same sixty-point tier may cost about $0.68 per task and $0.84 per task respectively, a gap of nearly 20%, while a higher-priced closed-source flagship can exceed $1 per task. The value of this metric is that it ties model capability to the tokens required to close a business loop, making it closer to a financial view than unit price.

    Implementation guidance:

    • Build a task set of 20 to 50 real business samples, run the end-to-end flow, and measure average token consumption;
    • Record success rate and manual rework rate at the same time, so saving tokens does not compromise quality and trigger repeat calls;
    • Make cost per task a monitored metric, placed on the same dashboard as latency and failure rate.

    4. Governing Output Inflation: From Prompts to Decoding Parameters

    More output tokens usually do not mean the model is verbose; they mean the task definition is vague. Actionable controls include:

    1. Structured output constraints: use a fixed schema to limit fields and reduce free-form generation;
    2. Explicit length budgets: state in the instruction “no more than 300 words” or “output JSON only”;
    3. Sampling parameter tuning: moderately reduce randomness to cut repetition and looping;
    4. Segmented generation and summary compression: extract key points from long documents before reasoning;
    5. Result caching: cache results for idempotent requests and skip repeated calls entirely.

    Illustrative code (pseudocode, conveying the idea only):

    1. def call_model(prompt, schema, max_output_tokens):
    2. cached = result_cache.get(prompt)
    3. if cached:
    4. return cached
    5. resp = client.chat(
    6. model=MODEL_NAME,
    7. messages=build_messages(prompt),
    8. response_format={"type": "json_schema", "schema": schema},
    9. max_tokens=max_output_tokens,
    10. )
    11. result = parse(resp)
    12. result_cache.set(prompt, result, ttl=3600)
    13. return result

    5. Cache-Friendly Architecture Design

    For the cache discount to materialize, the prefix must be stable. Practical points:

    • Put the system prompt and tool descriptions first, and version them, so frequent edits do not invalidate the cache;
    • Place volatile content such as timestamps, random IDs, and user nicknames later;
    • Use a rolling window and summaries for conversation history to control context length;
    • Schedule batch jobs grouped by prefix to raise the hit rate for identical prefixes;
    • Observe the cache hit rate separately in monitoring and treat it as a first-class metric.

    6. Where Capability Upgrades Should Be Applied

    The new generation focuses on three areas: coding and code comprehension, defensive cybersecurity analysis, and long-horizon agent tasks. The corresponding engineering benefits are:

    • Coding: lower rework rates for code completion, unit test generation, and defect localization, indirectly reducing the number of calls;
    • Security: initial triage of logs and alerts, and summarization of vulnerability descriptions, which fall within defensive analysis;
    • Agents: higher success rates for multi-step tool calls and long workflows, reducing mid-run failures and reruns.

    Note that stronger capabilities often come with longer reasoning output, which conflicts with cost goals, so the output budgets and task-set evaluation described above must be applied alongside them.

    7. Selection and Cost Governance Checklist

    • Build a three-dimensional evaluation table of unit price, cost per task, and success rate;
    • Run a canary with your own task set, validating on small traffic before scaling up;
    • Enable caching for stable prefixes and continuously observe the hit rate;
    • Set hard output budgets and schema constraints;
    • Configure budget alerts and cost attribution by tenant or business line;
    • Keep multi-model routing available, splitting traffic by task difficulty so simple tasks use a lower-cost option.

    8. Conclusion

    As first-tier models converge on the intelligence index, competition shifts from “who is smarter” to “who is cheaper at the same level of intelligence.” For developers, the real moat is not securing access to a particular model, but owning a reusable cost engineering method: manage tokens as a resource, treat caching as an architectural constraint, and use cost per task as a business metric. If you use Baidu AI Cloud capabilities, it is also advisable to build an observability and scheduling foundation on general-purpose categories (such as log services, monitoring and alerting, object storage, and container platforms), so cost optimization is backed by data.