LLM API Billing and Task-Level Cost Evaluation
At scale, LLM API spend is driven less by list price per million tokens than by input mix, cache hits, output length, and tool loops. This article explains how to split usage into observable token types, compare models on a quality–cost plane, and keep unit task cost under control.
After LLM APIs move from experiments to production traffic, procurement and architecture rarely hinge on “price per million tokens” alone. Prompt length, whether context can be reused, verbose outputs, and multi-round tool calls can make the real bill diverge sharply from the published unit price. A more reliable approach is to break billing into an observable usage structure, then plot evaluation metrics on a quality–cost plane so you can compare what it actually costs to finish a task.
Why the three token types cannot share one price story
Cloud LLM APIs commonly split usage into input, cached input, and output. Input is the context actually sent on that request. Cached input is a reusable prefix—system prompt or stable knowledge—hit on the server or gateway. Output is generated tokens. Unit prices usually differ: generation is heavier, so output is often the most expensive; a cache hit on a reusable prefix is typically far cheaper than fresh input.
Comparing two model generations or two SKUs by input price alone is misleading. If a new SKU is 20% cheaper on both input and output but cached input is halved, the real winners are long system prompts, stable knowledge prefixes, and multi-turn chats whose prefix barely changes. If the workload is almost always a fresh short prompt and a long generation (batch long-form writing, file-level code completion), the bill is output-driven and cache discounts barely show up.
Instrument the gateway or SDK: for every request, record new input tokens, cache-hit tokens, output tokens, whether a prefix cache hit occurred, and task type. Without those three quantities, a “20% price cut” has no baseline.
Why cheaper tokens do not mean a cheaper task
Unit price is vendor language; task cost is business language. A task might be a completed support ticket, a code review, or a RAG answer. Roughly:
Task cost ≈ new input × input price + cache hits × cache price + output × output price + extra usage from retries and tool round-trips
A stronger model may finish the same goal with a shorter chain of thought or fewer tool rounds, so output tokens fall and task cost drops even if the SKU looks “premium.” It may also “explain more,” inflate output, and erase the unit-price discount. Emphasizing cost per task means moving evaluation from static benchmark scores to an efficiency curve that includes usage.
Practical approach: freeze the same task sample set (sampled from real logs, not only public quizzes). For each sample, record success, human or automatic score, and the three token counts. Put actual (or SKU-estimated) spend on the x-axis and quality on the y-axis to get a scatter or Pareto front. Compare median task cost at a quality threshold, or pass rate at a fixed budget. Do not report average score or average unit price in isolation.
Cached input: largest discount, most dependent on engineering shape
Cache price is often much lower than ordinary input because it reflects reused compute or a storage hit. Capturing that tier depends on whether prompts are stable and prefixable. System role, tool specs, company rules, and fixed retrieved chunks should sit at the front of context and stay byte-stable. Do not splice timestamps, random IDs, or unordered retrieval results that shatter the prefix.
Implementation notes: version system prompts so cache invalidates only on change; sort retrieval by document ID before concatenation to cut noise; in multi-turn chat, split immutable persona and knowledge from the variable user turn. Monitor not only hit rate but misses caused by prefix jitter; otherwise a 60% cache discount is only a price list.
Cache is not free. Low hit rate still bills as new input. Over-long prefixes “for caching” raise the input bill on every miss. Cap prefix length and sample: after adding a fixed knowledge block, do savings from higher hit rate cover the extra input on misses?
SKU tiers: high-capability vs. high-efficiency for different task mixes
The same model family often ships a stronger SKU and a cheaper SKU. The strong tier fits hard reasoning, long tool chains, and low-tolerance work; the cheap tier fits classification, extraction, short replies, and high QPS. Wrong patterns: send all traffic to the strongest SKU, or force the cheap SKU through complex work and paper over failures with retries—retries double input and output and blow up task cost.
Routing can stay simple: a light classifier or rules (intent, need for code, context over a threshold) split traffic; uncertain samples go cheap first and upgrade if quality is below a threshold. The upgrade path must count tokens from the first failure, or the scorecard wins while the money loses.
If a tier gains capability at an unchanged unit price, that helps procurement, but still validate on the task curve: longer outputs can raise task cost even if the price did not move. Cover a week or two of live traffic after a release, not only an offline set.
A workflow that aligns evaluation with finance
- Define the task and success criteria. Use scripts where scoring is automatic; for subjective work use sampled humans plus a calibrated auto-judge to avoid circular evaluation.
- Collect the three usage dimensions and cache hits; aggregate P50/P90 spend by task type, not only the mean.
- Draw the quality–cost curve, mark the acceptable quality floor, and read cost at that level—not cost at the highest score.
- Ablate: disable cache, shorten the system prompt, cap max output; watch quality and the bill to find the most elastic knobs.
- Keep unit prices as configuration, not hardcoded, so you can replay historical usage for what-if SKU switches.
On the cloud, object storage can archive prompts and eval sets; logging and alerting carry the three token metrics; a container platform can isolate inference traffic by queue and SKU. These are generic capabilities, not tied to a single model brand.
Takeaway for architects
Cheaper list prices on input and output only change the quote structure. A large cache discount signals that the vendor is rewarding stable, reusable prefixes. What you actually manage is cost per task. Bind billing dimensions, cache engineering, SKU routing, and the quality–cost curve so you can follow model capability without losing the budget. For applications on Baidu AI Cloud, the same usage observability and task-level accounting belong in the gateway—not in a month-end invoice teardown.
