LLM API Cost Optimization: Lessons from a Price Cut and Four Levers to Lower Your Bill
This article breaks down what a recent LLM API price adjustment reveals about the structural decline in inference costs, and turns that insight into a practical cost model. It explains how to decompose a token-based bill into four controllable variables—cache hit rate, input size, output size, and peak/off-peak timing—and shows engineering techniques for each: keeping prompt prefixes stable to maximize prefix caching, scheduling deferrable batch workloads into off-peak windows, trimming context and constraining structured output, and building observability with the right metrics and budget guardrails. The goal is to help developers reduce spending through architecture rather than waiting for the next price cut.
1. The Signal: One Price Cut, A Structural Drop in Inference Cost
A cloud provider’s LLM platform recently announced new pricing for its lightweight model series. During off-peak hours, the input cache-hit price dropped from 0.05 to 0.02 yuan, the input cache-miss price from 1.5 to 1 yuan, and the output price from 4.5 to 4 yuan. Peak hours are billed at twice the off-peak rate, where peak is defined as weekdays 9:00–12:00 and 14:00–18:00, and all other times are off-peak.
The most telling detail is not “how much it dropped” but the structure of the cut: the cache-hit price fell by roughly 60%, far more than the roughly 11%–33% for cache misses and output. This suggests the main source of cost reduction is not simply better compute procurement, but the combined effect of higher cache reuse on the inference side, more efficient batch scheduling, and model structure optimization. The takeaway for developers: your bill is no longer determined only by “how many tokens you called,” but jointly by “when the call happened and how much cache it hit.”
2. Cost Model: Breaking the Bill into Four Optimizable Variables
For any token-billed LLM service, the cost of a single request can be abstracted as:
Cost = (Tin_hit * P_hit + Tin_miss * P_miss) + Tout * P_outwhere P_peak = k * P_offpeak (here k = 2)
This yields four variables you can act on:
- Cache hit rate (Tin_hit / Tin_total): determined by prefix stability;
- Input size (Tin_total): determined by context construction strategy;
- Output size (Tout): determined by generation length and structural constraints;
- Time distribution (peak/off-peak): determined by task scheduling.
The first three are controllable at the application layer; the fourth is controllable at the scheduling layer. Combined, they typically produce a several-fold difference in cost—without switching models or sacrificing quality.
3. Cache Hits: The Most Underrated Cost Lever
Mainstream inference services commonly use prefix caching: when a request’s input prefix exactly matches recent requests, those tokens can reuse already-computed intermediate state, so they are priced significantly lower. To capture this benefit, follow three engineering disciplines:
- Stable prefix: put the system prompt, tool definitions, examples, and fixed knowledge-base fragments at the very front, and do not vary them per request;
- Variables last: move volatile content such as timestamps, user IDs, and random numbers to the end of the prompt;
- Avoid prefix pollution: do not insert lines like “the current time is…” that change every time into the fixed prefix.
Illustrative bad and good examples:
# Bad: the prefix changes every time, so cache hits are nearly impossible[current time 2026-09-10 12:03:22] You are a customer service assistant... User question: ...# Good: stable prefix + trailing variablesYou are a customer service assistant. Follow these rules: <fixed, unchanging rules and examples>---User question: ... (variable part placed at the end)
In batch offline scenarios, you can also group tasks in the same batch by “same prefix,” sort them, and submit serially so adjacent requests share the cache. Hit rates often rise from single-digit percentages to a much higher level.
4. Peak/Off-Peak Scheduling: Move Deferrable Loads to the Valley
Time-of-use pricing is essentially a demand-side management tool: it uses price signals to push elastic workloads from peak to off-peak. Here, peak covers two weekday windows at double the price, meaning the same workload run off-peak saves about half the cost.
Actionable scheduling strategies include:
- Load tiering: split requests into online interaction (latency-sensitive) and offline batch (latency-insensitive);
- Queue-based peak shaving: send offline tasks to a message queue, with consumers dynamically adjusting throughput by the current period;
- Off-peak scaling: raise concurrency and batch size off-peak and automatically throttle during peak, avoiding paying for peak capacity year-round;
- Budget guardrails: set call quotas and alerts for peak hours to prevent abnormal traffic from inflating the bill.
# Pseudocode: dynamically adjust offline task concurrency by time perioddef worker_loop():while True:if in_off_peak(now()):concurrency = HIGH # scale up off-peakelse:concurrency = LOW # throttle during peakbatch = queue.fetch(n=concurrency)run_inferences(batch)
Note that peak/off-peak strategies apply only to deferrable loads; real-time paths serving end users should still guarantee latency, and cost optimization there should come from caching and context trimming.
5. Token Efficiency: Slim Inputs and Constrain Outputs
On the input side, common waste comes from “stuffing everything in”: passing an entire document, full conversation history, and redundant tool definitions at once. Improvements include retrieval-based context injection (pull only relevant fragments), summarizing and compressing conversation history, and trimming tool definitions on demand.
On the output side, clear structural constraints reduce wasted generation—for example, requiring JSON with defined fields so the model does not free-form and then need regex cleanup:
{"intent": "refund", "confidence": 0.0, "args": {"order_id": ""}}
Structured output not only lowers output tokens but also makes downstream parsing more stable, reducing retries and the double billing they cause.
6. Observability: No Measurement, No Optimization
Cost optimization must be built on an observability system. At minimum, collect: token usage and cost by business line/environment, cache hit rate, share of calls in peak vs. off-peak, average input/output length, and retry and failure rates. You can use general-purpose cloud logging, monitoring/alerting, and dashboard capabilities to collect and visualize these, and set daily/monthly budget threshold alerts.
A minimal viable telemetry structure:
request_id, biz_line, env, phase(peak/off_peak),in_tokens, cached_tokens, out_tokens, latency_ms, retry_cnt
With the cached_tokens and phase fields, you can answer the core question: “why did the bill go up?”
7. Implementation Notes on Baidu AI Cloud
When calling LLM services on Baidu AI Cloud, the same general methods apply: prefer inference interfaces with caching and keep prefixes stable; put batch tasks in a message queue to scale up off-peak; archive raw corpus in object storage and inject it via on-demand retrieval; build a cost dashboard and budget guardrails with logging and monitoring/alerting; and for latency-sensitive online paths, focus on leaner context and structured output rather than cutting model capability. These are general industry engineering practices; refer to the official documentation for specific billing and quotas.
8. Conclusion
The essence of a price cut is a partial pass-through of the cost dividend from improved inference efficiency. For developers, a more reliable way to save than “waiting for a price cut” is to master the cost structure: stable prefixes to raise cache hits, peak/off-peak scheduling to shift deferrable loads, input slimming and output constraints to compress tokens, and an observability system to hold the budget line. Get these four things solid, and how much your bill drops often depends not on the next pricing announcement but on the architecture itself.
