LLM Token Metering and Cache-Based Billing: How Inference Costs Are Calculated
This article explains the technical logic behind large language model token metering and cache-based billing. It breaks down the three components of per-turn cost, shows how prefix reuse and KV caching reduce long-conversation expenses, and covers time-of-day pricing. It also outlines a three-layer observability approach for displaying costs and a practical checklist for developers to optimize spending.
1. From “Free Chat” to Metered Consumption: An Inevitable Evolution
Once large language models moved into large-scale applications, a previously vague question became unavoidable: what does a single natural-language conversation actually consume?
The answer is not “one request,” but a finer-grained unit of computation. The industry commonly uses the token as the unit of measurement: both input and output text are split into tokens, and the model’s compute usage, GPU memory footprint, and response latency all correlate strongly with token count. As a result, chat activity is naturally decomposed into measurable, priceable resource consumption.
After a team with a quantitative-finance background launched a conversational service for everyday users, it began applying the same “metering mindset” on the consumer side: once a response finishes, users see the total tokens consumed in that turn, the portion served from cache, and the equivalent amount converted from the API price. No fee is charged at that moment, but the accounting has already started—marking the shift of LLM services from a “black-box experience” to a “transparent metering” stage.
For developers, the real value of this change is that cost becomes attributable, reproducible, and optimizable for the first time.
2. The Three Components of a Single Turn’s Cost
Under mainstream billing models, one turn is typically split into three items:
- Input (cache miss): context that must be recomputed for this request;
- Input (cache hit): context already computed earlier and directly reusable;
- Output: the tokens the model generates in this turn.
A schematic bill structure can be abstracted as:
Cost per turn =input miss tokens × input unit price+ input hit tokens × cache unit price # usually well below the input unit price+ output tokens × output unit price
It is worth stressing that the output unit price is generally higher than the input unit price. The reason is that the generation stage is autoregressive decoding, token by token, where each step depends on all preceding context and cannot be parallelized as fully as the prefill stage, resulting in lower compute efficiency.
3. Cache Hits: The Key to Amortizing Long-Conversation Costs
3.1 Why Long Conversations Get More Expensive
In multi-turn conversations, each time the user sends a new message, the model usually has to process the conversation history along with it. The longer the conversation, the more historical content must be reprocessed each turn, and cost rises linearly. Without any optimization, the marginal cost of a conversation spanning a hundred turns would become prohibitively high.
3.2 Prefix Reuse and KV Caching
The common industry practice is to cache the key-value states required by the attention mechanism (the KV cache). Its core premise is that a language model’s internal representation of a piece of text depends only on that text and its prefix, not on any subsequent content. Therefore, as long as the prefix is exactly the same, its computation results can be reused.
In engineering terms, this usually requires a “strict prefix match”: once anything in the history changes (for example, editing the system prompt or inserting a timestamp), the cache is invalidated starting from the point of change:
Original prefix: [system prompt][turn 1][turn 2]...[turn N]After timestamp edit: [system prompt][timestamp][turn 1]... → miss starts at the timestamp
3.3 Why Cache Hits Cut Prices Significantly
A cache hit means those tokens do not need to repeat the expensive prefill computation and only incur the lighter overhead of reading and concatenation. As a result, the unit price for the hit portion can be significantly lower than that of an input miss, forming a natural “long-conversation discount.” This also explains a counterintuitive point from the original text: the same greeting may cost less in a long conversation than in a new session—because it hits an existing prefix.
4. Time-of-Day Pricing: Writing Compute Supply and Demand into the Price
Inference compute supply shows a clear tidal pattern. To encourage off-peak usage and improve overall resource utilization, the industry commonly introduces peak/off-peak time-of-day pricing, with unit prices rising during peak hours and dropping during idle hours.
Accordingly, the actual cost of a turn depends on four variables:
- What was said (input length and content);
- What was answered (output length);
- How much was discussed before (the size of the reusable prefix);
- When it was discussed (the price coefficient of the time slot).
Pseudocode makes this more intuitive:
def cost(inp_miss, inp_hit, out, peak: bool):k = 2.0 if peak else 1.0 # time-slot coefficient, illustrative onlyreturn (inp_miss * P_IN + inp_hit * P_CACHE + out * P_OUT) * k
5. Building the Bill into the Product: Three Layers of Observability
To show users “what this turn is worth,” at least three layers of capability are needed:
- Collection layer: instrument the inference pipeline to record input/output token counts, cache-hit volume, and time-slot identifiers;
- Calculation layer: convert everything through a configurable price table, keeping prices decoupled from code for easy adjustment;
- Presentation layer: display at both session and cumulative granularity to help users build a cost intuition.
{"input_tokens": 60,"cached_tokens": 0,"output_tokens": 24,"price_tier": "off_peak","equivalent_cost": 0.000156}
6. A Cost-Optimization Checklist for Developers
- Stabilize the prefix: keep the system prompt and opening structure fixed, avoid putting volatile information up front, and maximize cache hits;
- Control history: apply summary compression or a sliding window to very long sessions to curb context bloat;
- Constrain output: use explicit format and length requirements to reduce wasted generation;
- Schedule off-peak: move batch and offline tasks to idle hours;
- Unify metering: build a session-level cost dashboard with budget alerts so optimization has a basis.
7. Conclusion
Splitting chat into tokens, splitting tokens into hits and misses, and then layering on a time-slot coefficient turns the cost of LLM services from a muddled account into an explainable engineering metric. For developers, understanding this metering logic not only leads to more cost-effective calling strategies but also enables cost-visualization capabilities that are genuinely user-friendly. In the general practices of platforms such as Baidu AI Cloud, the observability system built around token metering, cache reuse, and monitoring alerts is becoming a foundational capability for LLM application cost management.
