LLM Cost Optimization: Technical Selection and Architecture Practices

    This article reframes large language model cost optimization around effective cost per unit of business output rather than subscription price. It breaks the bill into input, output, cache-read, and cache-write tokens, shows how rising cache-read prices amplify effective cost, and explains how to build observability first. It then presents a five-dimension selection matrix, a routing and budget-guard architecture, and a quarterly sensitivity analysis cadence. Readers will learn how to keep cost changes as configuration adjustments instead of capacity loss.

    1. Reframing the Problem: From “Which Plan Is Cheaper” to “Effective Cost per Unit”

    The original discussion centered on how the usable quota of a coding subscription plan changed before and after a model price adjustment: the token volume that a monthly plan used to support can shrink to roughly one fifth once the cache-hit price rises several times over. The essence of this is not a “price increase” but an amplified weight of the cache component in the cost structure.

    The right engineering question, therefore, is not to compare plan list prices, but to answer: for the same business output (fixing the same number of defects, generating the same volume of code), what is the effective cost per unit?

    2. Cost Model: Breaking the Bill into Computable Quantities

    The cost of a single call can be abstracted as:

    1. Cost = Pin * P_in + Pout * P_out + Cread * P_cache_read + Cwrite * P_cache_write

    Here Pin/Pout are input and output tokens that miss the cache, and Cread/Cwrite are cache-read and cache-write tokens. What engineering actually needs to monitor are three ratios:

    • Cache hit rate h = Cread / (Cread + Pin);
    • Output share r = P_out / (P_in + P_out);
    • Context reuse length (which determines how Cwrite is amortized).

    When the cache-read unit price rises k times, the amplification of the effective unit price is approximately:

    1. Amplification ≈ (1 - h) + h * k

    With h = 0.8 and k = 5, the amplification is about 4.2, consistent with the order of magnitude of “7 billion shrinking to 1.4 billion”. This shows that usage patterns with a high cache hit rate plus long repeated context prefixes are the most sensitive to cache pricing, while low-hit, short-context batch tasks are barely affected.

    3. Observability First: No Measurement, No Optimization

    Before discussing selection, establish a minimal observability loop that records input, output, cache read/write tokens, and latency for every request:

    1. record = {
    2. "model": model_name,
    3. "in": usage.prompt_tokens,
    4. "out": usage.completion_tokens,
    5. "cache_read": getattr(usage, "cache_read_tokens", 0),
    6. "cache_write": getattr(usage, "cache_write_tokens", 0),
    7. "latency_ms": elapsed_ms,
    8. "task_type": task_type, # chat / code / summary / embedding
    9. }

    Aggregating by task_type and then computing h and r yields a real cost heat map. In practice, most teams’ token consumption concentrates in a few long-context, repeated-prefix scenarios, and that is exactly the main battlefield for optimization.

    4. Selection Evaluation Matrix: Five Dimensions, Not One Price

    Dimension Focus How to Judge
    Effective unit price Cache read/write and input/output priced separately Plug in your own h and r
    Capability fit Code completion, long-document understanding, structured output Score with your own evaluation set
    Stability Rate-limit thresholds, concurrency caps, peak availability Load testing and canary observation
    Elasticity Whether hot switching across multiple models is supported Whether the routing layer is decoupled
    Compliance Invoicing, audit logs, data retention policy Confirmed by procurement and legal

    Key conclusion: price is only one cell in the matrix. When cache pricing fluctuates, an architecture with multi-model switchability has more risk resilience than any single low-price plan.

    5. Architecture in Practice: Routing, Fallback, and Budget Guardrails

    The recommended approach is to insert an abstraction gateway between the business and the models to hide provider differences:

    1. def route(task):
    2. if task.type == "code_completion":
    3. return cheap_model # high frequency, short output, prioritize low cost
    4. if task.type == "long_doc":
    5. return strong_model # low frequency, high value, prioritize capability
    6. return default_model
    7. class BudgetGuard:
    8. def __init__(self, daily_limit):
    9. self.limit = daily_limit
    10. self.used = 0
    11. def allow(self, est_tokens):
    12. return self.used + est_tokens <= self.limit

    Three supporting practices:

    1. Prefix stabilization: put the system prompt, tool definitions, and examples at the very front and avoid volatile concatenation such as timestamps, maximizing cache hits;
    2. Context trimming: summarize and compress conversation history to control Pin and Cwrite;
    3. Tiered fallback: when the budget is exhausted, automatically switch to a smaller model or shorter context so the business is not interrupted.

    6. Cost Sensitivity Analysis and Decision Cadence

    Run a sensitivity calculation once per quarter: take h ∈ {0.4, 0.7, 0.9} and cache multiplier k ∈ {1, 3, 5}, generate a cost matrix, and locate your own business’s “safe zone”. If the result shows that cost depends heavily on the cache component, actively reduce reliance on a single billing rule, for example by moving cacheable repetitive tasks to offline batch processing, or by introducing a local small model to handle high-frequency simple requests.

    On the procurement side, favor general cloud resource forms that can be invoiced, reconciled, and audited (such as container platforms, object storage, log services, and monitoring and alerting), so that cost can be attributed at the project level rather than staying at the individual subscription level.

    7. Conclusion

    Price adjustments are the norm; architectural elasticity is the moat. Once cost is decomposed into four computable components, selection is placed in a five-dimension matrix, and model calls are consolidated behind a unified gateway, any change in billing rules triggers a configuration adjustment rather than a passive loss of capacity.