LLM API Cost Engineering: How to Control Token Spend When Prices Stay Flat

    This article breaks down why LLM API bills can rise even when per-token prices remain unchanged, and how engineering teams can respond. It introduces a cost model based on input, output, cached input, and per-task usage, explains output inflation in stronger models, and shows how structured output, staged generation, stable cache prefixes, context trimming, tool-output compression, retry limits, and small-model routing reduce spend. It also covers per-scene cost metering and how to compare models with similar capability scores by per-task cost, cache friendliness, stability, and compliance.

    1. The Phenomenon: Flat Pricing Does Not Mean a Flat Bill

    A pricing pattern worth attention has recently appeared in the industry: a new-generation general-purpose LLM API delivers a significant capability jump while keeping the official per-token price identical to the previous generation. Input, output, and cached input pricing stay the same, and the discount for cache hits is roughly 20% off or better (equivalent to about 80% savings). At the same time, a third-party evaluation system raised its overall capability index from 53 for the previous generation to 60, putting it on par with other models in the same tier.

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

    1. Input tokens: context length, number of retrieved fragments, and number of prior conversation turns;
    2. Output tokens: how verbose the model is, multiplied by the more expensive output price;
    3. Cache hit rate: cached portions receive a large discount, while misses are billed at full price;
    4. Cost per task: total tokens and total cost consumed to complete one business task.

    One key detail in the source material: the new model consumes about 20% more output tokens than the previous generation to complete the same task. That means actual spending can still rise even when unit prices do not. This is the “output inflation” problem this article focuses on.

    2. Cost Model: Writing the Bill as a Formula

    Let the input price be p_in, the output price be p_out, the cached input price be p_cache, the input tokens of one request be I, the cached portion of those input tokens be I_h, and the output tokens be O. The cost of a single request is then:

    1. cost = (I - I_h) * p_in + I_h * p_cache + O * p_out

    As a typical order of magnitude (illustrative only, not any vendor’s actual pricing): input at about 1.4 CNY per million tokens, output at about 4.4 CNY per million tokens, and cached input at about 0.26 CNY per million tokens. The output price is roughly three times the input price, so output length is the most cost-sensitive factor.

    Now add the task perspective. If a task requires n requests on average, then:

    1. task_cost = n * cost

    The “cost per task” metric common in industry evaluations (for example, roughly 0.68, 0.84, and 1.23 USD per task for models in the same tier) is essentially the statistical result of the formula above under a real workload distribution. It reflects “who is actually cheaper” better than unit price, because it includes retries, tool-call rounds, and failed rollbacks.

    3. Output Inflation: Why Stronger Models Can Cost More

    More capable models tend to “say the whole thing”: longer reasoning chains, finer comments, longer test code. That is a quality gain and a cost burden. The answer is not to limit the model’s thinking but to constrain the output structure.

    3.1 Compress Redundancy with Structured Output

    Ask the model to return only the required fields and avoid natural-language padding:

    1. {
    2. "summary": "one-sentence conclusion",
    3. "changes": [{"file": "path", "action": "modify", "reason": "why"}],
    4. "risks": ["risk items"],
    5. "tests": ["test cases to add"]
    6. }

    3.2 Generate in Stages Instead of One Long Output

    Split “design, implement, test” into multiple short requests that each produce one small block. Shorter single outputs actually improve cache and context reuse.

    3.3 Set Output Limits and a Truncation Policy

    Enforce a hard cap on maximum output length on the business side, and summarize overlong output a second time instead of storing it as-is.

    4. Caching Strategy: Actually Capturing the Discount

    Cache-hit discounts are large (on the order of 80%), but hits are conditional: the prefix must be stable. In practice:

    • Put the system prompt, tool definitions, and long-lived conventions at the very front, with no dynamic concatenation;
    • Put timestamps, random IDs, and user-specific content at the end of the request;
    • Avoid inserting always-changing content such as the “current time” at the start of the prompt;
    • Add semantic-layer caching for frequent similar requests and reuse the result directly on a hit.

    An illustrative stable-prefix layout:

    1. [fixed] role definition + output format constraints + tool list + coding conventions
    2. [semi-fixed] repository structure summary (updated infrequently)
    3. [dynamic] current task description + relevant file fragments + timestamp

    As long as the prefix is byte-for-byte identical, the cache hit rate rises noticeably; a one-character difference invalidates the entire prefix.

    5. An Optimization Checklist for Agents and Coding Scenarios

    Long-running agent tasks and AI coding are cost black holes because of many rounds, long contexts, and frequent retries. Practical measures include:

    1. Context trimming: inject only the files and symbols relevant to the current subtask, not the whole repository;
    2. Tool-result compression: summarize logs and compiler output before feeding them back to the model;
    3. Fast failure convergence: set a maximum retry count and a fallback strategy to avoid token-burning infinite loops;
    4. Small-model routing: send lightweight tasks such as intent recognition and format validation to cheaper models or a rules engine;
    5. End-to-end metering: tag every request with a business label and measure cost per task by scenario instead of looking only at the total bill.

    A simple metering instrumentation example:

    1. record = {
    2. "scene": "code_review",
    3. "input_tokens": usage["prompt_tokens"],
    4. "cached_tokens": usage.get("cached_tokens", 0),
    5. "output_tokens": usage["completion_tokens"],
    6. "requests": 1,
    7. }
    8. # Aggregate by scene to get the cost per task for each business scenario

    6. Selection Method: Deciding on “Same Score, Different Price”

    When two models have similar overall capability indices (for example, both in the 60-point tier), the decision should go beyond the score and compare:

    • Cost per task: which is cheaper under the real workload distribution;
    • Output token consumption: which is more verbose;
    • Cache friendliness: whether prefix stability is well supported;
    • Stability and rate limits: availability at peak times and retry cost;
    • Ecosystem and compliance: whether data residency, auditing, and permission systems meet requirements.

    In a platform environment such as Baidu AI Cloud, these capabilities are typically offered as general categories: object storage for holding long-context material, message queues for peak shaving, container platforms for hosting agent runtimes, and logging plus monitoring and alerting for cost observability. Combining these general components is what forms a sustainable cost-governance loop.

    7. Conclusion

    “Flat unit price, much higher score” is good news, but real cost competitiveness shows up in cost per task. Developers need a task-level cost model and should focus on controlling output inflation, improving cache hits, compressing context, and constraining agent rounds. As intelligence levels converge, whoever drives down the cost of the same intelligence holds the initiative in the next stage.