LLM Cost Optimization: A Practical Guide to Reducing Token Spend

    A practical guide to optimizing large language model costs in production. Learn how input, output, and cache-read pricing work, how to calculate per-call expenses with a reusable estimation script, and which engineering practices reduce token spend without sacrificing quality.

    1. Where the Problem Starts: Why Experience Eventually Shows Up on the Bill

    When teams evaluate a large language model, they usually look at answer quality, time to first token, and long-context performance first. Those dimensions matter. But once a model enters a production pipeline and gets called repeatedly by scripts, CI jobs, customer support systems, and coding assistants, something else usually decides whether it stays: whether the cost per task is predictable and compressible.

    The most useful detail in the source material is not the marketing claim but a discount label in the pricing notes. It points to a fact that is easy to miss: the cost of an LLM is not a fixed constant. It is a variable determined by billing unit × usage structure × time window. Understanding that is worth more than memorizing any specific price.

    2. The Cost Model: Breaking the Bill into Three Computable Parts

    Mainstream LLM services bill by token, quoted per million tokens. Cost typically has three components:

    • Input cost: prompts, system instructions, retrieved context, and conversation history;
    • Output cost: generated text, code, and structured results;
    • Cache read cost: the charge for reading a repeated prefix (such as a fixed system prompt or a long document) after a cache hit, usually well below first-time input.

    As a formula:

    1. cost per call = (input tokens / 1e6) * input price
    2. + (output tokens / 1e6) * output price
    3. + (cached tokens / 1e6) * cache read price

    This model has strong explanatory power: cost reduction has only three paths — reduce input, compress output, and raise the cache hit rate. Any claim that “switching models saves money” is crude unless it lands on one of these three.

    3. Running the Numbers: Mid-Size Codebase Analysis

    Assume a typical task: ask the model to analyze a mid-size project codebase, with about 500,000 input tokens of context and about 20,000 output tokens of change suggestions and sample code. Suppose the list price is 0.8 per million input tokens and 2.8 per million output tokens, with a limited-time discount of 50%, i.e. 0.4 and 1.4.

    1. Before discount: 0.5*0.8 + 0.02*2.8 = 0.400 + 0.056 = 0.456 per call
    2. After discount: 0.5*0.4 + 0.02*1.4 = 0.200 + 0.028 = 0.228 per call

    The conclusion is straightforward: the cost is cut in half, and the input component accounts for roughly 88% of the total, which makes it the main target for optimization. At dozens of calls per day and thousands per month, the savings grow from pocket change into a meaningful infrastructure budget.

    Now add caching: if 400,000 of the 500,000 input tokens are repeated project conventions and architecture notes, and the cache read price is typically only 20%–30% of the input price, that portion of the spend can drop by another order of magnitude.

    4. A Reusable Cost Estimation Script

    Turning the estimate into code means you can produce numbers during solution review instead of after the bill arrives:

    1. def estimate(input_tok, output_tok, cache_tok=0,
    2. in_price=0.8, out_price=2.8, cache_price=0.23,
    3. discount=1.0, calls=1):
    4. fresh = max(input_tok - cache_tok, 0)
    5. per_call = (fresh * in_price + cache_tok * cache_price
    6. + output_tok * out_price) / 1_000_000
    7. return per_call * discount, per_call * discount * calls
    8. # 500K input, 20K output, 400K cache hits, 50% discount, 1000 calls per month
    9. print(estimate(500_000, 20_000, 400_000, discount=0.5, calls=1000))

    5. An Engineering Checklist for Lower Cost

    1. Stabilize the prefix: keep the system prompt, tool descriptions, and specification documents fixed at the very front of the request to maximize cache hits;
    2. Trim context: use retrieval instead of feeding everything in, recalling only the fragments relevant to the current question;
    3. Constrain output: use structured formats and explicit length limits to suppress redundant generation, since output is usually priced higher than input;
    4. Tiered routing: send simple tasks to a lightweight model or rule-based logic, and escalate only for complex reasoning;
    5. Batching and async: run offline jobs off-peak, with rate limiting and retry backoff;
    6. Observability: monitor and alert on token usage, cache hit rate, and cost per task, and set budget thresholds;
    7. Window management: a limited-time discount is a time-sensitive variable, so factor the discount period into capacity and budget planning rather than assuming it permanently.

    6. Conclusion

    To judge whether an LLM is a good fit, measure quality, latency, and cost as a triangle at the same time. On the cost side, the point is not to remember a price but to master the decomposition: separate input, output, and cache, quantify their share, and use a script to move the estimate into the design phase. Do that, and you can quickly tell whether a call is worth it no matter how prices move.