A Stream’s Final Usage Record Settles the Bill
A streamed response arrives in pieces, but its bill is not a tally of visible pieces. Transport events describe delivery; provider usage records describe token consumption. Following those two tracks separately makes streaming costs easier to explain, especially when caching, reasoning, interruptions, or retries enter the picture.
This walkthrough follows one request through BestRelay, a quality-first API relay that forwards each request to the official model named in it, without substitution. Its coverage includes Claude, GPT, Gemini, DeepSeek, Kimi, GLM, Qwen, and Grok. The accounting principle remains the same across those families, even when their event formats differ.
The accepted request establishes the accounting context
Step one: record the requested model, the applicable rates, and identifiers that connect the client request to its upstream attempt. BestRelay uses pay-as-you-go billing based on token counts reported by the provider. A local tokenizer can support an estimate, but it does not replace those reported counts.
The input includes more than the last message a person typed. Instructions, conversation history, tool definitions, and supported nontext inputs can affect usage. Record the relevant pricing categories before streaming begins, rather than assuming every input token has the same price.
Opening events establish progress rather than final cost
Step two: inspect the opening events without treating them as a settled invoice. Depending on the provider and endpoint, early events may carry message metadata, initial usage, or no usage at all. An event acknowledging that generation started does not establish the final output count.
Some streaming interfaces require an explicit option to include usage. Confirm the behavior of the endpoint actually in use. Compatibility with an SDK does not guarantee that every provider exposes identical fields or delivers them at the same point.
Text deltas reveal delivery rather than token totals
Step three: let text deltas update the interface, not the billing ledger. One event can contain several tokens, a fragment of a word, or no user-visible text. Event count, character count, and network byte count therefore cannot reliably stand in for billable token usage.
Tool arguments and other generated content can also contribute to output usage. Some models report reasoning-related usage separately or within a broader output total. Interpret those fields according to the provider’s definitions; adding a reported subcategory to its inclusive total would count the same work twice.
Cache accounting requires separate categories and careful normalization
Step four: identify ordinary input, cache reads, and cache writes in the reported usage. BestRelay prices cache reads and writes separately. A reused prompt prefix can therefore have a different cost from newly processed input or a newly created cache entry.
Provider schemas differ. A cached-token field may be a subset of an input total, while another schema may expose distinct categories. Normalize these into nonoverlapping billable buckets before applying rates. Do not subtract cached tokens automatically, and do not charge them again merely because they appear in a detail field.
The terminal usage record enables charge reconciliation
Step five: reconcile the completed response against its authoritative usage record. Depending on the interface, this may arrive in a final event, a usage-bearing chunk, or another documented reporting mechanism. Multiply each nonoverlapping category by its applicable rate, then sum the category charges.
Keep displayed estimates distinct from settled amounts. BestRelay works with Claude Code, Codex, Gemini CLI, and any OpenAI SDK, but client-side displays can reflect different assumptions. Compare the underlying counts and rate categories before concluding that two displayed costs disagree.
Interrupted streams and retries need separate accounting
Step six: treat a dropped connection as an incomplete observation, not proof of zero usage. The provider may have processed input or generated output before delivery stopped. Preserve available identifiers and reconcile against any provider-reported usage subsequently available.
A retry is another attempt and may incur additional usage; it is not automatically a continuation of the first stream. Track attempts individually. BestRelay members can also earn a referral commission when people they invite top up, but that is separate from response-token accounting. Create a BestRelay account to get started.
Want to try the models discussed here? Sign up