%20(1).png)
Event-Based Billing: How It Works and How to Build It
Build an event-based billing system with reliable events, metering, pricing, customer attribution, and invoicing for SaaS, APIs, and AI products.
API metering turns API traffic into a financial-grade record. Learn what to meter, where the meter belongs, and how to enforce limits in the request path.
%20(1).png)
Your API meter can look correct while measuring the wrong unit. A new analytics endpoint accepts one request, then triggers warehouse queries, vector lookups, and a document render. The gateway still records one call.
The counter did its job. The usage record still misses the work that drove compute.
This guide covers what to meter, where the meter belongs, how to build reliable events, and where runtime enforcement fits.
API metering is the practice of recording API consumption as structured, attributable events that can be aggregated into billable or enforceable quantities.
A useful metering event needs four pieces of information:
If you lose the customer ID, attribution disappears, and if you lose a trustworthy timestamp, billing-period aggregation becomes unreliable.
Observability data describes system behavior and may tolerate sampling. Metering carries a higher accuracy bar because the resulting records feed usage totals, credits, limits, and invoices.
API metering, rate limiting, and enforcement solve three different jobs with different accuracy requirements.
Rate limiting protects infrastructure from traffic bursts. Amazon API Gateway describes throttles and quotas as best-effort targets, which means configured values can be exceeded.
That trade-off works for capacity protection. Metering needs a tighter record because drift carries into usage totals, credits, and invoices.
Enforcement checks whether the next request can proceed based on the customer’s current allowance, balance, and usage.
A 429 Too Many Requests is a sign of capacity throttling. An exhausted entitlement is a product-policy decision, and the application needs different handling for each case.
Metering writes the usage record. Enforcement reads current state before the next protected action executes.
Meter the unit that tracks the work performed or the value delivered. Request count fits APIs where calls have similar resource requirements.
The unit changes when pricing follows compute, credits, tiers, or outcomes. Our guide to API pricing models covers those pricing structures and their infrastructure requirements.
Three patterns cover a large share of API products.
The last pattern needs more instrumentation because the customer-facing unit sits above the technical work.
One document-processing request might produce several usage dimensions, like:
That is one HTTP request with four possible meters.
Collapsing them into one blended number weakens traceability. Instead, emit an event for each relevant dimension and let the aggregation layer resolve them independently.
Every event needs a customer identifier. More complex contracts can require additional dimensions.
Useful attribution fields can include:
Choose those keys before you ship the meter. Historical events only contain the dimensions captured when they were written, which makes later attribution changes difficult to backfill.
In enterprise contracts, these are rarely flat labels, rather, they form a hierarchy. A single usage event decrements the agent, the user's team, the department, and the org root, and a check that lands on any protected request has to evaluate every level in that chain. The most generous applicable limit wins where sources conflict.
There are four stages between an API call and a charge, and each stage places different requirements on the infrastructure.
Emission and aggregation make up the usage metering layer. Rating applies pricing or credit rules, and the metered billing handoff sits downstream.
Emission and aggregation determine whether the usage record is trustworthy. Every later calculation inherits errors created there.
The pricing model also changes aggregation requirements. Per-call, tiered, credit-based, and outcome-based models need different grouping rules, counters, and windows.
API metering can live at the gateway, inside the application, or behind an event pipeline. More complex products may use more than one placement.
The gateway already sees authenticated requests, which makes request instrumentation convenient.
Its view stops at HTTP. A gateway cannot see all internal work, such as token consumption, model routing, or tool calls generated downstream.
Retries also need care, and a repeated request can look like fresh traffic if your event model cannot identify the original operation.
The application knows which work ran and can emit the real unit.
That could mean:
The trade-off is distributed instrumentation. Every service producing billable work needs consistent event fields, units, and semantics.
Gateway and application events can feed a shared stream where processing handles normalization, deduplication, aggregation, and replay.
You gain one place to apply event rules and several downstream consumers from the same source, and you also inherit another production pipeline to operate.
AWS Marketplace provides one example of this report-then-bill model. Sellers submit usage records, and AWS handles downstream processing.
AWS also records metering activity in CloudTrail. That independent audit trail gives sellers another record to compare during reconciliation.
Billing-grade API metering needs idempotency, immutable history, late-data rules, and replayability.
Clients retry, networks fail, and pipelines replay batches.
A stable event identifier lets ingestion recognize a repeated usage event before another copy reaches the aggregate.
Stripe’s idempotency model is a useful reference. Stripe stores the result associated with an idempotency key and returns that result when the key is reused.
Stripe documents a retention window of at least 24 hours. Your own event contract needs an explicit window that defines how long repeated delivery still refers to the same operation.
A bad usage record needs a correction path.
Keep the original event and write a compensating record, because preserving history makes previous aggregates reproducible and gives you a clear trail during reconciliation.
Usage can arrive after the action finishes. Client clocks drift, queues stall, and batched events can be delayed.
Your policy should define:
Keep raw records long enough to reconstruct periods you may need to inspect. Replayability gives you a path from an aggregate back to its source events, which becomes useful when a usage total or invoice is challenged.
AI APIs expose the weakness in request-count metering because one request can create very different amounts of internal work.
A short classifier response and a long code-generation response can hit the same endpoint while consuming very different token quantities.
The final token count appears after generation, and the meter can record that total once the response completes.
An agent workflow can create several operations from one incoming call.
That may include:
A gateway sees the entry request. Application-level metering has to preserve attribution across the work created underneath it.
A router can send harder requests to a larger model while keeping the same external API shape.
Model identity therefore becomes a useful event dimension. Two calls to the same endpoint can produce different usage profiles inside the workflow.
Credits can give several models and tools one customer-facing usage unit. A defensible mapping still starts with understanding AI token cost and the consumption underneath each workload.
API metering tells you what has been consumed, while runtime enforcement reads current state before another protected request runs.
The decision can depend on several inputs. An entitlement is a commercial allowance attached to a customer’s package, and can represent API access, a usage quota, a credit allowance, or access to a particular feature.
RBAC handles role permissions, billing manages invoices and payment state downstream, and entitlements supply the product-facing rules needed during execution.
Effective entitlements can come from several sources:
The runtime resolves those inputs into the value that applies to the current request.
An entitlement response can carry:
The access-denied reason lets the application render a paywall message that names the exact limit hit and what upgrading unlocks.
The application can use that result for an allow decision, hard limit, soft limit, or upgrade prompt.
Caching becomes important because the check sits in the request path. Local state keeps the common path close to the application, with a defined network fallback when the required value is missing.
Cached state also introduces staleness. You need clear refresh rules, cache invalidation, timeout behavior, and recovery policies.
Building API metering in-house can be the right first decision. A usage table and increment function can serve a product with a few stable usage rules for a long time.
Ownership gets heavier when usage state begins affecting live requests. Miro gives a concrete example of how far that scope can grow. The company launched its AI credit system with Stigg in under 6 weeks and avoided an estimated 5,000 engineering hours of internal infrastructure work.
The useful question is which concurrency, cache, ledger, entitlement, and recovery behaviors you want your own infrastructure to keep owning.
Accurate API metering tells you what has been consumed. The next infrastructure problem is using that state while a request is still in flight, before more model calls, agent actions, or compute can run.
Stigg handles that request-time layer. Stigg is the usage runtime for AI products. Entitlements, credits, usage limits, and spend governance are enforced synchronously in the request path.
Your existing billing provider can keep handling invoices, payments, tax, and financial records. Stigg manages the product-facing usage state your application needs during execution.
You can adopt the pieces your architecture needs:
Stigg can connect product-facing usage state with billing, CPQ, CRM, and data warehouses while your financial stack stays in place.
The Sidecar keeps request-time checks close to the application. On a cache hit, entitlement checks resolve instantly from the local in-memory cache.
Node.js applications skip the Sidecar entirely, and the Node SDK offers the same low-latency checks, local caching, and real-time updates natively in-process.
On a cache miss, the Sidecar fetches from Stigg's Edge API at around 100ms, with a configurable timeout, and falls back to configured defaults if the timeout is reached. Redis is available as an optional persistent cache when entitlements need to survive restarts or stay shared across a large fleet.
Teams can start with one SDK integration or runtime component, then add metering, credits, or entitlements as requirements grow.
If you’re running at high event volume or in regulated environments, Stigg's BYOC deployment runs the ingestion, aggregation, and enforcement pipeline inside your own AWS, GCP, or Azure account, with end-user and usage data never leaving your cloud boundary.
Stigg manages the infrastructure via Infrastructure-as-Code (templates, deployments, and upgrades) while your team keeps the account, IAM, and network perimeter.
For implementation details on Sidecar, entitlements, credits, and metering, head to the Stigg docs.
AI usage control is runtime infrastructure that enforces AI consumption rules before protected work executes. It can evaluate token limits, credit balances, feature entitlements, and usage allowances for a customer, user, agent, or workload.
You enforce AI token limits in real time by checking current usage against the applicable allowance in the request path. The application can allow execution, apply a hard limit, or follow a configured soft-limit policy before another model call starts.
The main difference between AI usage control and AI usage metering is when each layer acts. AI usage metering records and attributes consumption, while AI usage control evaluates current state and applies the relevant policy before protected work runs.
AI credits control usage by mapping AI workloads to deductions from a managed balance. A production credit system also needs block-level expiry, cost basis, paid and promotional categories, burn order, depletion rules, and an append-only ledger.
When an AI usage limit is reached, the application can apply a hard stop, soft-limit policy, or upgrade state based on the customer’s current entitlement. The enforcement layer applies that result before the protected workload executes.