The Illusion of Simple Per-Token Billing

The promise of billing for Large Language Model (LLM) usage is often presented as a straightforward per-token calculation. For developers and businesses integrating these powerful AI models, this simplicity is appealing. However, running a multi-provider LLM gateway in production reveals a far more complex reality. The metering of LLM usage, even when ostensibly per-token, involves numerous hidden components and provider-specific quirks that can quickly derail any naive billing approach. Getting this metering correct demands significant engineering effort, often exceeding the complexity of the proxy infrastructure itself.

The core issue is that a single LLM interaction rarely translates to a single, uniformly priced event. Instead, a single request can be broken down into as many as six distinct, differently priced components. These include the primary text input, any cached input that is reused from previous turns, the cost associated with writing new data to a cache, the output generated by the model, tokens consumed for internal reasoning processes, and any external tool usage such as web searches or image generations. Each of these components may carry its own unique rate. An attempt to consolidate these into a single "tokens x price" calculation per request is fundamentally flawed, as it fails to account for the granular pricing differences and the unique cost structure each provider imposes.

Diagram illustrating the six distinct components of an LLM request and their potential pricing

Scattered Usage Reporting: The Streaming Challenge

Another significant pitfall lies in how usage data, particularly for streaming responses, is reported. Many developers assume that the total usage for a request will be available in the final data stream chunk. This is a dangerous assumption. Providers like OpenAI, when configured to do so via `stream_options`, report input and cache-related tokens in the final chunk. However, other providers, such as Anthropic, scatter this information across different stages of the response. Input and cache tokens might be reported in an initial `message_start` event, while reasoning and output tokens appear in subsequent events or the final chunk. Relying solely on the last chunk means significant undercounting of actual token consumption.

This scattered reporting necessitates a robust system that can aggregate usage data from multiple points within a single request's lifecycle. Each identified component of a request must generate its own charge row, with its associated rate applied. The sum of these individual charge rows then forms the total cost for that specific request trace. This granular approach is essential for accuracy, especially when dealing with multiple LLM providers, each with its own API nuances and reporting mechanisms.

Provider Quirks and the Need for Abstraction

The problem is further compounded by the diverse ways LLM providers handle and report token usage. What might be a standard input token for one provider could be treated differently, or even broken down into sub-components, by another. Caching mechanisms, for instance, are not universally implemented or priced. Some providers might charge for cache writes, while others might only charge for cache reads or not at all. Tool usage, which can involve complex external API calls or generative tasks like image creation, adds another layer of cost that must be accurately metered and billed separately from pure text token generation.

Consider the difference between a simple text generation task and a task that requires the LLM to perform a web search, parse the results, and then generate a summary. The web search itself incurs costs (API calls, data transfer), and the subsequent processing of that data by the LLM consumes reasoning tokens. A billing system that only accounts for the final output tokens would be missing the costs associated with the intermediate steps. This requires a flexible abstraction layer that can map provider-specific events and costs to a unified billing schema.

Beyond Tokens: Reasoning and Tool Usage

The focus on