LLM API Cost Optimization: A Practical Guide to Token Pricing, Caching, and Model Routing
As LLM API prices drop, the real savings come from the widening price gap between input, cached input, and output tokens. This guide explains how to build an attributable token cost model, raise cache hit rates through static prompt prefixes, route simple requests to lighter model tiers, control output token growth, and lock in savings with budget guardrails and offline evaluation. It also covers how to choose between asynchronous batch and streaming calls, and why unit prices should stay configurable as vendors change pricing and model versions.
1. What the New Price Structure Means Technically
A cloud provider recently revised the billing structure of its LLM service alongside a version update. For the flagship tier, input and output unit prices dropped by roughly 20% versus the previous generation, while cached input dropped by about 60%, landing at the order of $8 per million tokens, $0.40, and $40 respectively. The lite tier kept the previous generation’s pricing: $2 per million input tokens, $0.10 for cached input, and $10 for output.
The real value of these numbers for developers is not that things got “cheaper.” It is that the price gradient across the three token classes has been pulled wide open again. Cached input now costs 1/20 of regular input and 1/100 of output. Between tiers, the input price gap is about 4x, and the output gap is also about 4x. A price gradient is optimization headroom: cost governance is essentially about reallocating traffic along that gradient.
Note that specific unit prices shift with vendor strategy and contract terms. The figures here only illustrate the structural relationship. The durable methodology is to break the bill down into attributable token-class metrics, then design a separate optimization approach for each class.
2. Start With an Attributable Token Cost Model
Most cost optimization efforts fail because teams only look at a single total on the bill. The first step is making cost decomposable:
# Illustrative: attribute the cost of a single request by token typedef estimate_cost(u_in, u_cached, u_out, price):return (u_in * price["input"] +u_cached * price["cached"] +u_out * price["output"]) / 1_000_000# Suggested structured log fieldslog = {"trace_id": "req-8f3a","model_tier": "flagship", # flagship / lite"prompt_tokens": 4200,"cached_tokens": 3800, # portion served from cache"completion_tokens": 260,"cache_hit_ratio": 0.90,"cost_usd": 0.0616,}
Only three metrics really matter: cache hit ratio, output token share, and average cost per request. Feed them into your logging service and monitoring/alerting stack, tagged by business line, scenario, and model tier, and you can pinpoint which path is burning money.
3. Cache Hit Ratio: The Biggest Cost Lever Right Now
With cached input at 1/20 the price of regular input, stabilizing the prompt prefix pays off far more than compressing copy. The general principle of caching: when the request prefix is byte-for-byte identical, the computed result for that segment can be reused, so any dynamic content placed at the front breaks reuse.
Practical points:
- Make the prefix static: keep system instructions, role definitions, tool descriptions, and a few examples fixed at the very front; move timestamps, user IDs, and random values to the tail of the prompt.
- Control prefix rotation frequency: changing the system prompt often invalidates the cache repeatedly. Version it and route changes through a release process rather than hot-editing.
- Structured assembly: use a template engine to guarantee byte-level stable serialization, avoiding prefix mismatches caused by dict iteration order or whitespace differences.
[static] system role + output format constraints + domain knowledge ← target: high reuse[semi-static] conversation summary, retrieved reference documents[dynamic] current user input, timestamp, session ID ← variables last
When combining with retrieval-augmented generation, sort knowledge base documents by content hash before assembly. This keeps the same set of documents in the same order across requests, and typically lifts the hit ratio from single-digit percentages to a much higher level.
4. Model Tier Routing: Send Simple Requests to the Lite Tier
The lite and flagship tiers differ by about 4x on both input and output, so routing gains scale linearly. Rules you can actually ship:
- Intent classification up front: use the lite tier or a small classifier to judge task difficulty first (extraction, rewriting, and classification go to lite; multi-step reasoning, code generation, and long-document analysis go to flagship).
- Confidence fallback: have the lite tier attach a self-assessment or rule-based check; if it fails, escalate to the flagship tier and retry, forming a “cheap first, upgrade on demand” funnel.
- Cache before model choice: a cache-hit flagship request can cost less than a cache-miss lite request, so routing must take cache state as an input variable.
def route(task, cache_state):if cache_state.hit_ratio > 0.8:return "flagship" # cheaper once cache is amortizedreturn "lite" if task.difficulty <= 2 else "flagship"
5. Output Token Governance and Call Shape
Output is priced at more than 10x input and cached input, making it the main amplifier on the bill. Controls include: explicitly constraining output structure and length, asking for the conclusion before the supporting detail, splitting long-form generation into incremental multi-turn output so it can be terminated early, and archiving reusable generations to object storage to avoid regenerating them.
On call shape, non-real-time workloads usually get better pricing through asynchronous batch APIs, at the cost of higher throughput latency. Real-time interaction uses streaming output to improve experience and to make it easy to cut off once a budget threshold is reached. Mix the two by scenario rather than standardizing globally.
6. Turn Optimization Into Engineering Mechanisms
One-off tuning decays as the business grows, so it needs to be mechanized:
- Budget guardrails: set token quotas and alert thresholds per business line, with automatic downgrade to the lite tier on abnormal growth.
- Offline evaluation set: before every prompt or routing change, compare quality and cost on a fixed evaluation set to avoid “cutting cost into a quality incident.”
- A/B and canary: validate new strategies on small traffic while watching cache hit ratio and average cost per request.
- Regular review: prices and model versions keep changing, so unit prices in the cost model should be configurable, not hard-coded.
7. Conclusion
The dividend from lower prices flows only to teams that can articulate their token structure and turn caching and routing into mechanisms. The recommended priority is clear: fix the cache prefix first, then add model tier routing, then close the loop with budget monitoring and evaluation. After those three steps, unit business cost typically drops meaningfully while service quality stays within an acceptable range.
