To prevent high-volume AI operations from erasing your margins, you must decouple subscription tiers from flat LLM access. By building an event-driven, multi-tenant metered billing engine, you can track token consumption down to the exact feature, user, and API call in real time, turning unpredictable AI overhead into predictable unit economics.
Building a successful B2B SaaS product used to mean enjoying gross margins of 80% or higher. Once your code was deployed, the cost of serving one extra database query or rendering a new dashboard was practically zero. AI changed that. Today, every search, document summary, and agentic workflow triggers an external LLM API call that costs real money.
If you bill your customers on a flat monthly subscription but pay your AI vendors per token, high-volume users will quickly erase your profits. A single customer running automated background tasks can quietly rack up thousands of dollars in API bills before your next billing cycle. To protect your B2B SaaS profit margins, you need to know exactly who spent what, down to the exact feature. Here is how to build a custom metered billing engine to trace token usage and regain control over your unit economics.
The Margin Crisis in AI-Powered B2B SaaS
Most SaaS founders start by hardcoding API keys and absorbing the initial costs of their AI features. This works during the prototype phase, but it breaks down rapidly in production. Without an integrated AI SaaS billing architecture, you face three primary financial risks:
- The Power-User Trap: Ten percent of your users might consume ninety percent of your LLM resources, turning profitable accounts into net-negative liabilities.
- Feature-Level Blindness: You cannot tell if your automated report generator is costing more to run than your customer support chatbot, making it impossible to price features accurately.
- Vendor Price Fluctuations: When model providers update their pricing or you switch to a more expensive frontier model, you have no way of dynamically adjusting your customer rates.
To solve this, you must transition from simple seat-based pricing to a hybrid model where users pay for a baseline subscription plus the precise volume of compute they consume.
Step 1: Designing the Event-Driven Telemetry Layer
A reliable multi-tenant billing system cannot run synchronously. If your application waits to write a billing record to your database every time an LLM returns a response, your user experience will grind to a halt. Instead, you need an asynchronous, event-driven telemetry layer.
When a user triggers an AI feature, your application backend sends the request to the LLM. Once the model responds, your backend extracts the usage data (prompt tokens, completion tokens, and model type) and instantly fires a lightweight event payload to a message broker like Apache Kafka or RabbitMQ.
The event payload must contain specific metadata to ensure accurate tracking. A standard event payload should look like this:
{
"event_id": "evt_987654321",
"tenant_id": "tenant_enterprise_alpha",
"user_id": "usr_dev_456",
"feature_id": "feature_pdf_summarizer",
"model_name": "gpt-4o",
"prompt_tokens": 1250,
"completion_tokens": 350,
"timestamp": "2026-03-30T14:22:00Z"
}
By decoupling this data collection from your primary application state, you guarantee that even if your billing engine experiences a temporary spike in traffic, your core software remains fast and responsive.
Step 2: Resolving the Feature-Level Attribution Challenge
Standard LLM APIs do not know what features exist inside your SaaS. They only see incoming prompts. To trace token usage by feature, your application must wrap every API call in a context manager or middleware layer that automatically injects the active tenant ID and feature ID.
For example, if a user accesses an AI-native search bar, the search service must tag the outgoing request with the "search_bar" feature ID. If they generate a weekly marketing campaign, the system tags it as "campaign_builder". This granular tag-to-token mapping is what allows your product team to run cohort analyses and see which features are highly profitable and which are running at a loss.
Step 3: Building the Stream Processing and Aggregation Engine
Writing millions of raw token events directly to a relational database like PostgreSQL will lock up your tables and balloon your storage costs. To make this data usable for billing, you need an aggregation engine that processes the stream in real time.
A dedicated worker service reads incoming events from your message queue and batches them. It aggregates usage metrics over short windows (e.g., every five minutes) before committing them to a timeseries database or an optimized data store like ClickHouse. This keeps your query speeds incredibly fast while giving you a precise, tamper-proof audit trail of exactly when and how resources were used.
Step 4: Enforcing Real-Time Budget Limits and Hard Gates
A great metered billing engine does more than just generate invoices at the end of the month; it acts as an active financial guardrail. By comparing aggregated hourly usage against a tenant's predefined budget or prepaid credit balance, you can implement automated limits.
If a client's agentic workflow gets stuck in an infinite loop and starts burning hundreds of dollars per hour, your engine can automatically pause their API access, trigger an email alert, and prompt them to upgrade their tier. This proactive limit-setting is critical for building trust with enterprise clients who want to ensure they never receive an unexpected five-figure bill.
Creating a Sustainable Pricing Engine
Building a robust billing infrastructure is not about charging your customers for every single click. It is about understanding your underlying costs so you can package and price your software intelligently. With granular feature-level telemetry, you can experiment with credit-based systems, offer premium add-ons for expensive models, and confidently scale your enterprise operations without risking your business health.
At Oracon Global, our senior in-house team specializes in building high-performance, custom software architectures, AI agent integrations, and scalable billing engines. We write clean, robust code that you own 100%, ensuring your business is fully equipped to scale in the AI era.
Are you ready to secure your software margins and build a resilient billing engine for your SaaS? Contact Oracon Global today to discuss how we can bring your system architecture to life.
Frequently asked questions
Why can we not just use standard subscription billing platforms for AI SaaS?
Traditional subscription platforms expect predictable flat-rate cycles. They lack the high-throughput ingest systems needed to process millions of real-time token events per minute without lagging or dropping data.
What is the main challenge of tracking token usage by feature?
The main challenge is context correlation. Standard LLM API calls do not know which application feature triggered them, meaning you must intercept the request and append metadata tags (such as Tenant ID and Feature ID) before sending the payload to your database.
How do you prevent the billing engine from slowing down the user experience?
By using an asynchronous, event-driven architecture. Instead of writing usage data to your transactional database during the LLM call, you emit a lightweight billing event to a message queue, keeping user facing latency extremely low.
Can we implement custom pricing tiers and credit limits with this architecture?
Yes. Because the raw usage data is structured with detailed metadata, you can write simple database queries or microservices to enforce hard limits, deduction rates, or tier-specific markups in real time.
Read next
Beyond Chatbots: How to Build AI Agents That Actually Do Work for Your Business
Most businesses use AI to answer questions. Here is how to build custom AI agents that actually take action, connect to your internal tools, and handle complex workflows.
Beyond the Wrapper: How to Build Custom AI Agents for Business That Actually Work
Many businesses invest in basic AI wrappers only to find they lack the security and context needed for real work. Here is how to build custom AI agents that integrate deeply with your workflows and databases.
Enterprise AI Maintenance Costs: Budgeting for Year Two and Beyond
Building an AI system is only half the battle. Discover the practical, ongoing operational costs of enterprise AI, including token management, model drift, and continuous security audits.
Oracon Global builds production-grade AI agents, automation and apps — and you own the code and IP. Tell us what you want to automate.
Book a call →See our work
