LLM API Cost Optimization: Practices for Time-Based Pricing and Off-Peak Scheduling

    This article examines how time-based, tiered pricing for large language model APIs changes LLM cost optimization. It breaks down the pricing structure across peak and off-peak windows and across cached input, uncached input, and output tokens, then presents a reusable cost formula with three tunable variables: cache hit rate, peak-hour traffic share, and output size. It walks through practical steps in priority order—shifting deferrable workloads out of peak hours, engineering stable prefixes to raise cache hit rates, constraining output length and format, and consolidating model calls behind a unified gateway with usage attribution, budgets, and observability. The goal is a measurable, sustainable token cost reduction loop.

    1. What the Price Change Actually Signals

    A cloud provider recently announced a new billing strategy for its LLM inference service, introducing a dual-track price with separate peak-hour and off-peak rates and an overall reduction of nearly 97.5%. This reduction does not apply uniformly to all call types. The difference is structural: cached input tokens see the largest cut, uncached input tokens come next, and output tokens see a comparatively limited reduction.

    Based on public information, the new price is quoted per million tokens, with separate rates for off-peak and peak windows. Peak hours are limited to roughly 7 hours across the first and second halves of the workday; all remaining time, including the full weekend, is billed at the off-peak rate. In relative terms, the peak price is about twice the off-peak price. Compared with the previous single flat rate, cached input, uncached input, and output drop by roughly 60%, 33%, and 11% respectively.

    Three conclusions matter for developers:

    • The reduction is both time-based and type-based, not a flat discount across all hours.
    • Cache hits are the most incentivized path, with the widest price gap.
    • Output token pricing is the most rigid, so shortening output yields the most direct savings.

    2. Cost Model: Do the Math Before Optimizing

    Any optimization should rest on a reusable cost formula. Let the number of input tokens be $I$, output tokens be $O$, cache hit rate be $h\in[0,1]$, and the share of calls during peak hours be $p$. The expected cost of a single request can be written as:

    1. Cost = I * [ h * P_in_hit + (1-h) * P_in_miss ] + O * P_out
    2. where P_* takes the off-peak or peak price, weighted by (1-p) and p respectively

    This equation exposes three tunable variables: hit rate $h$, peak share $p$, and output size $O$. The engineering order should be: first reduce $p$ (shift off-peak), then raise $h$ (caching), and finally compress $O$ (output constraints). Off-peak changes usually touch only the scheduling layer, so they have the smallest blast radius and the fastest payoff. Caching requires changing how requests are constructed. Output compression may affect business results and needs careful evaluation.

    A simplified example (illustrative numbers, not any provider’s actual pricing): if a workload averages 200 million input tokens and 50 million output tokens per day, and the cache hit rate rises from 0.2 to 0.6 while the peak share falls from 0.6 to 0.2, then under a “peak is twice off-peak” structure the total cost can drop to roughly one third of the original. The dividend from structural price changes is realized mainly by adapting how you call the API.

    3. Off-Peak Scheduling: Move Deferrable Work Out of Peak Hours

    Peak hours cover only weekday daytime, which means a large amount of non-real-time work can naturally be shifted. Typical movable scenarios include offline document summarization, batch log and ticket classification, overnight data cleaning and label generation, model evaluation and regression testing, and incremental knowledge base rebuilding.

    For scheduling, a “task tiering + queue delay” mechanism is recommended:

    1. # Example: choose an execution window based on latency sensitivity
    2. DELAY_TOLERANT = {"offline_summary", "log_classify", "kb_reindex"}
    3. def submit(task):
    4. if task.name in DELAY_TOLERANT:
    5. queue = low_priority_queue # consumed only during off-peak hours
    6. else:
    7. queue = realtime_queue # allowed to run during peak hours
    8. queue.put(task)

    Combined with delayed delivery in a message queue and elastic scaling on a container platform, consumer capacity can scale up automatically during off-peak hours. Note that off-peak does not mean unlimited backlog: set a maximum wait time and backlog alerts for the delayed queue, so that a month-end release does not create a new “false peak.”

    4. Cache Hits: The Biggest Cut, and the Most Engineering-Intensive

    Cached input sees the largest reduction because it amortizes reusable prefix computation. To hit the cache reliably, the request side must follow a “stable prefix” principle: place invariant content such as system prompts, tool descriptions, few-shot examples, and long documents at the front, byte-for-byte identical, and put timestamps, random IDs, and user-specific fields at the end.

    1. [ Stable prefix: system instructions + tool schema + few-shot + long context ]
    2. [ Variable suffix: current user question + session state + timestamp ]

    Engineering essentials include: put prompt templates under version control and review, and never concatenate random values into templates; use a fixed chunking strategy for long documents to avoid shifting split boundaries; during canary releases, running two templates in parallel causes the hit rate to plummet, so isolate traffic. Also build a hit-rate dashboard and monitor and alert on hit rate as a service metric on par with latency and error rate.

    5. Output Governance: The Only Lever Where Pricing Is Most Rigid

    Output tokens see the smallest reduction, which means “generate less” is a deterministic win. Viable measures include: explicitly constrain output format and length, replace free text with structured output, split long-form generation into segmented tasks with a leading summary, and switch classification and extraction tasks to short-label output. Example:

    1. resp = client.chat(
    2. messages=[{"role": "user", "content": prompt}],
    3. response_format={"type": "json_object"}, # structured constraint to reduce redundant phrasing
    4. max_tokens=512,
    5. )

    A reminder: compressing output should be gated by validation on an evaluation set, so that saving money does not sacrifice accuracy and create hidden costs.

    6. Architecture and Governance: Making Cost Sustainable

    It is advisable to abstract model calls behind a unified gateway layer that centralizes routing, fallback, caching, and metering: select different model tiers by task value, sending simple requests to smaller models and complex reasoning to larger models; establish token usage attribution and produce bills by business line, environment, and interface; bind cost budgets to rate-limiting policies so that exceeding a threshold automatically triggers fallback or queuing. The observability system should cover call volume, token distribution, hit rate, time-of-day distribution, and failed retries. Retries are often the main source of runaway bills and need caps and backoff.

    7. Conclusion

    Time-based pricing turns “when to call” into a cost variable as important as “how to call.” Developers should complete four steps in order—off-peak scheduling, prefix cache改造, output constraints, and unified gateway governance—and continuously validate with the cost formula and dashboards. If you use Baidu AI Cloud services, the same general methods apply, and you can build a quantifiable token cost optimization loop through monitoring, alerting, and logging services.