Track token usage like you'd track any other cost center
LLM cost surprises usually trace back to no per-feature token budget, not to a single expensive call. Attribute usage before optimizing it.
When an engineering organization integrates generative AI into a platform, the initial focus is almost entirely on prompt engineering and model accuracy. The infrastructure team provisions an API key, the application starts making network calls, and the first few vendor invoices are usually negligible.
Then, the feature is fully rolled out across the multi-tenant user base. The next monthly invoice arrives, and the cloud budget is completely overrun. The immediate reaction from leadership is usually a mandate to start aggressively shrinking context windows or to switch to a cheaper, less capable model. But trying to optimize a language model implementation without granular telemetry is a guaranteed way to degrade the core product experience.
The Aggregate Billing Trap The fundamental problem is that external AI providers bill in aggregate. If you simply pass a single API key into your backend services, you receive a bill at the end of the month that just states you consumed three billion tokens.
You have absolutely no idea if those tokens were spent on a critical business function like clinical document summarization, or if a poorly written background job got caught in a retry loop and spent three days asking a model to classify malformed data. It is the exact same architectural anti-pattern as deploying fifty distinct microservices into a single cloud account without implementing resource tagging. You cannot optimize a distributed system when you lack visibility into its unit economics.
Enforcing Telemetry at the Gateway You have to treat token consumption exactly like database read capacity or network bandwidth. It is a strict infrastructure constraint that requires rigorous accounting.
To solve this, you must intercept every outbound LLM request at the API gateway layer or within a dedicated proxy service. Before the payload is forwarded to the external provider, the gateway must inject custom headers or metadata tags. At a bare minimum, every single request must be tagged with the specific tenant identifier, the environment, and the exact application feature generating the prompt.
When you export this telemetry into a system like Elasticsearch or ClickHouse, the aggregate bill stops being a mystery. You can pull up a dashboard and instantly see that your user-facing chat interface is highly efficient, but a backend data extraction pipeline is consuming eighty percent of your total budget because it is passing the exact same redundant context into every single prompt.
Establishing Feature Budgets Once you have accurate attribution, you can implement programmatic cost controls. In a multi-tenant enterprise system, this means defining a strict token budget per customer tier and per feature.
If a specific tenant exceeds their allocated token budget for a non-critical feature, the API gateway can automatically rate limit their requests. Alternatively, you can configure the routing logic to dynamically fall back to a smaller, cheaper open source model for the remainder of the billing cycle.
Generative AI is not an abstract research and development expense. It is just another raw compute resource. If you do not instrument and track it like a standard cost center, you will pay for that lack of engineering discipline on every single invoice.