LLM API Cost Optimization: A Practical Guide to Caching, Scheduling, and Output Control
As LLM API prices continue to fall, engineering teams can turn lower unit prices into real savings only by optimizing around the billing structure. This guide breaks down the three input/output price dimensions and the peak-hour multiplier used by major LLM APIs, then presents three non-conflicting cost levers: raising the cache hit rate through stable prefixes and versioning, shifting offline workloads out of peak hours with queue-based scheduling and cost-aware routing, and trimming redundant input and output tokens. It also shows how to manage cost as an SLO with budgets, hit-rate targets, and weekly billing reviews.
1. What a Price Cut Really Signals
A cloud provider’s LLM platform recently announced a price reduction for its lightweight inference model series, covering all three billing items: input cache hits, input cache misses, and output. The adjustment also keeps a time-based pricing structure in which peak-hour prices are twice the off-peak prices. Such moves are no longer unusual in the industry, but for engineering teams the number that matters is not the new unit price. What matters is the trend it confirms: inference cost is shifting from “priced per model” to “priced by resource utilization.”
Understanding this is what turns a price cut into business value. If you only benefit passively when unit prices drop, your cost curve still rises linearly with call volume. If you actively optimize the architecture around the billing structure, the cost curve can grow far more slowly than the business.
2. Breaking Down the Billing: Three Unit Prices and One Multiplier
Most mainstream LLM API pricing can be abstracted into three dimensions:
- Input cache hit price: charged for input when a repeated prefix (such as a system prompt, a long document, or few-shot examples) is reused from the server-side cache. This is usually significantly lower than the miss price.
- Input cache miss price: charged for the full input on the first request or after the cache expires.
- Output price: charged per generated token, generally higher than the input side.
On top of these sits a time-of-day multiplier: peak hours cost twice as much as off-peak hours. In the public adjustment cited above, the three off-peak prices dropped to 0.02, 1, and 4 yuan, and peak-hour prices are the same values multiplied by two. The same request can therefore produce a bill that differs by a factor of two depending only on when it is sent.
This yields a basic cost formula:
cost per call = hit price × hit tokens + miss price × miss tokens + output price × output tokensdaily cost = Σ (cost per call × time-of-day multiplier)
The engineering implication is straightforward: improving the cache hit rate, compressing output length, and scheduling calls off-peak are three cost levers that do not conflict with one another.
3. Lever One: Make the Cache Hit Rate a Core Metric
Cache hits are not a favor the server does for you; they are an architectural design that requires active cooperation from the client. The key points:
- Prefix stability: Put the system prompt, tool descriptions, and fixed examples at the very front of the request, and do not insert volatile content such as timestamps, random IDs, or user nicknames. Otherwise the prefix hash changes and the whole cache entry is invalidated.
- Segmented assembly: Concatenate a stable segment, a conversation segment, and a retrieval segment in that order. The longer and earlier the stable segment, the higher the hit rate.
- Invalidation governance: Give the cache a version number and bump the version whenever the prompt changes, so old and new versions do not mix and cause repeated misses.
- Observability: Log the hit and miss token counts and the estimated cost for every request in your logging service, and set a lower bound on the hit rate with monitoring alerts.
Illustrative pseudocode:
def build_prompt(system_v, retrieved, history, query):# system_v is a controlled version number, bumped on every changestable = f"[SYS v{system_v}]" + SYSTEM_TEMPLATEreturn stable + serialize(retrieved) + serialize(history) + query
4. Lever Two: Schedule Requests Out of Peak Hours
Time-based pricing is essentially a price signal that guides load balancing. Practices that work in production:
- Task tiering: Move all offline batch work (report generation, log summarization, bulk labeling, offline evaluation) into off-peak hours; keep online interactions in peak hours and pay for the experience.
- Queue-based peak shaving: Use a message queue to absorb bursts, and let consumers decide their consumption rate based on the current time-of-day multiplier, slowing down at peak and speeding up off-peak.
- Cost-aware routing: Maintain a simple time-of-day check and select a model tier per task (a lightweight model for simple tasks, a larger model only for complex ones).
- Fallback plan: During peak hours, serve non-critical paths from cached results or a smaller model to avoid bill spikes.
if is_peak(now) and task.priority == "offline":enqueue_delayed(task, until=next_off_peak())
5. Lever Three: Cut Output and Redundant Input
The output price is usually the highest, so controlling generation length gives the most direct savings: restrict structured output fields, set a reasonable maximum generation length, and use layered summarization (coarse first, then fine) for summarization tasks instead of generating a long document in one shot. On the input side, deduplicate retrieved fragments, simplify templates, and trim history. Replace the habit of stuffing the context with taking only what is needed.
6. Closing the Cost Governance Loop
Manage cost as an SLO: set a cost budget per ten thousand requests, a cache hit rate target, and a cap on the share of calls made during peak hours; configure threshold notifications in monitoring and alerting; and review the ratio of the bill to business growth every week. When that ratio keeps falling, the architecture optimization is working. If it stays flat or rises, go back through the three levers one by one.
7. Conclusion
Price adjustments are external variables; the cost structure is what you control internally. Make cache hit rate, time-of-day scheduling, and output compression engineering standards, and any price cut will be amplified into real profit margin. Without that, even the lowest unit price cannot fill the gap left by careless calls. Teams that want to further simplify inference deployment and elastic scaling can also evaluate general cloud capabilities from Baidu AI Cloud (such as container platforms, message queues, logging services, and monitoring and alerting) to carry the scheduling and observability designs described above.
