LLM Inference Cost Optimization: Time-of-Day Billing and Prompt Caching in Practice
After an LLM API changes its pricing, you cannot scale your budget down by the same percentage as any single unit price drop. One request may contain both cache-hit and cache-miss input, output is billed separately, and if the billing rules distinguish peak from off-peak windows, the same task can cost different amounts depending on when it runs. This guide shows how to break down the bill, run a worked example to decide which lever to pull, schedule work across price windows without hurting delivery, and validate caching and output optimizations before keeping them. The core metric to track is cost per successfully delivered task, not average price per call.
1. Break Down the Bill: Account for Input and Output Separately
Cached input tokens are the tokens the applicable billing rules actually classify as cache hits — not the length of the whole request. Take document summarization: many requests may share one long task description, but each request carries a different document. Even if the shared portion qualifies for reuse, the changing body may still be billed as uncached input. So “how many requests hit the cache” tells you nothing about the bill. Cost analysis should focus on the input token hit rate: cached input tokens as a share of all input tokens.
When all three unit prices use the same billing unit, the cost of a single window is:
Window cost = (cached input tokens × cached price + uncached input tokens × uncached price + output tokens × output price) ÷ tokens per billing unit
For bills that span windows, calculate each price tier separately and then sum them. If a single request crosses a price switch point, do not assume it is billed by start time, completion time, or per-segment usage — follow the applicable billing documentation and the settlement details. The time zone used by your scheduler must also match the billing reference, especially how weekdays, weekends, and window boundaries are handled.
2. Use One Worked Example to Decide What to Optimize
The following is a demonstration only, with all prices set per million tokens: off-peak cached input, uncached input, and output are 0.02, 1, and 4 respectively, and assume peak prices are double the off-peak values. These numbers illustrate the calculation and are not general pricing.
Assume a batch consumes 10 million input tokens, of which 6 million are cache hits, and generates 2 million output tokens. Run entirely off-peak, the cost is:
| Billing item | Usage | Demo price (per million tokens) | Cost |
|---|---|---|---|
| Cached input | 6M | 0.02 | 0.12 |
| Uncached input | 4M | 1 | 4.00 |
| Output | 2M | 4 | 8.00 |
| Total | — | — | 12.12 |
The same usage billed entirely at peak prices comes to 24.24. In this bill, output accounts for roughly 66% of the off-peak total. Raising the cache hit rate from 60% to 80% with unchanged input and output volume saves 1.96 off-peak; cutting output by 25% alone saves 2. The savings are close, but the implementation costs differ: the first requires reviewing how input is organized and what the actual hit conditions are, while the second requires verifying that results are still complete and usable. Doing this math first prevents you from investing in the change with the most visible unit-price drop but the smallest effect on total cost.
3. Time-Shifted Scheduling: Deferring Work Only When Delivery Allows It
Real-time Q&A, interactive assistance, and online moderation are generally poor candidates for queuing in exchange for a lower price, because the wait itself degrades the user experience. Backfilling historical data, generating test samples, and summarizing existing documents can be scheduled by price window if delayed delivery is acceptable. The deciding factor is not whether a task is offline, but whether its deadline allows waiting and whether the low-price window has enough capacity to clear the backlog.
A practical approach is to record a deadline, an estimated processing time, and an allowed retry count for each task. The scheduler compares expected queue time, execution time, and a safety margin to decide whether the task can finish within the next low-price window. Tasks that can make it go into a pending queue; tasks that cannot run earlier according to business priority. Do not rely on average duration alone: long documents, concurrency limits, and retries all stretch processing time, so critical tasks should reserve margin based on the historical duration distribution.
When a low-price window opens, do not release the entire backlog at once. A burst of requests can trigger rate limiting, and continuous retries then consume the window and may even push tasks into the next price tier. Release work gradually within the allowed concurrency, use bounded exponential backoff for short-lived failures, and give each task a unique identifier to avoid duplicate submission after a timeout. If a timeout occurs and you cannot confirm whether the previous request completed, check any available task status or usage records before deciding to rerun — a caller not receiving a result does not mean the earlier request consumed no tokens.
4. Validate Both Caching and Output Optimizations
Keep reusable input stable. For caches that identify reuse by a common prefix, put the fixed task description, output constraints, and general examples first, and the user input or document body after. If timestamps, request IDs, or similar content do not need to participate in inference, keep them in call logs instead of inserting them into the input prefix every time. Actual hits still depend on the applicable caching rules and request structure; do not estimate savings just because the text looks similar. When splitting or reordering context, do not drop information needed for permission checks and result accuracy in pursuit of a higher hit rate.
Cut output that downstream consumers never use. Batch extraction usually needs field values, missing-value markers, and necessary evidence locations — not a paragraph of explanation on every record. First establish which fields the consumer of the result actually uses, then remove redundant explanation. For long-document summarization or complex analysis, simply lowering the output limit is risky: truncated output produces missing fields or broken structure and may force the system to re-request. Before rollout, compare output token volume, parse success rate, and human review pass rate before and after on the same representative sample set, and confirm the savings were not bought with rework.
5. Measure Success with Cost per Successful Task
After rollout, record cached input tokens, uncached input tokens, output tokens, failure and retry counts, and whether each task was delivered on time, broken down by business, task type, and billing window. Estimate cost from these details and reconcile against the actual bill periodically. When you find a discrepancy, check the billing unit, time zone and price boundaries, cache classification, and whether failed requests generated billable usage first.
Ultimately, make the average cost of each task that succeeds and is delivered on time the core metric, not the average price per call. A single request may get cheaper, but if retries increase, results need rework, or delivery slips, total business cost can rise. In practice, start with batch tasks that have loose timing requirements and easy-to-verify results, trial time-shifted scheduling and input/output optimization separately, keep the changes that genuinely lower cost per successful task without hurting quality or delivery time, and then expand the scope gradually.
