Usage-based pricing moved the meter out of the billing system and into the product codebase. That is the change most teams miss. When a customer pays per API call, per generated image, per processed minute or per gigabyte stored, the bill is calculated from events your application emits. The invoice becomes a report on a database the billing team did not build and usually cannot audit.
Most of these pipelines start life as analytics. Someone adds an event emitter next to the feature, points it at a queue and ships. For a funnel chart that is fine. For an invoice it is not, because the two failure modes do not cost the same. A dropped analytics event makes a chart look slightly better than reality. A duplicated billing event charges a customer twice, and the customer who notices is the one who leaves and explains why.
An event log is not a meter
Two properties separate a meter from a log. It has to count each billable action exactly once even when the pipeline delivers it more than once, and it has to land on the right total when events arrive late or out of order. Those pull against each other. Exactly-once delivery is not something most brokers offer, and building it with distributed transactions produces a system that is slow and still wrong at the edges. The design that holds up accepts at-least-once delivery and makes the counting layer idempotent.
Under-counting is the quieter failure and the more expensive one to fix later. An event that arrives after the invoice was cut either disappears or lands in next month's bill against the wrong meter. Neither outcome shows up on a dashboard. Both show up at renewal, when the customer brings their own logs.
The identifier belongs at the source, not in the client
Every serious metering system deduplicates on a stable per-event identifier, and the vendor names differ. Stripe meter events take an identifier and enforce uniqueness within a rolling period of at least 24 hours. OpenMeter pairs the CloudEvents id with source and keeps a 32-day dedupe window by default. Lago requires a transaction_id per event. Underneath, all three are a unique constraint plus a discard on conflict.
What breaks them is minting the identifier at send time. A UUID generated inside the retry loop is new on every attempt, so the second attempt looks like a second billable action and the dedupe window never fires. Generate the key where the billable action happens, attach it to every send attempt, and keep mutable state out of it. If the payload is later corrected, the identifier still has to be stable or the correction arrives as new usage.
At the storage layer the rule is one constraint:
create table usage_events (
identifier text primary key,
account_id text not null,
meter text not null,
quantity numeric(18,6) not null,
occurred_at timestamptz not null,
received_at timestamptz not null default now()
);
insert into usage_events (identifier, account_id, meter, quantity, occurred_at)
values ($1, $2, $3, $4, $5)
on conflict (identifier) do nothing;
Two details matter more than the syntax. Quantity should be numeric with a fixed scale rather than a float, because a tenth of a gigabyte added a few million times drifts. And the usage row belongs in the same transaction as the action being billed. If the order commits and the usage row does not, or the reverse, no later job can tell which system is right.
Late events need an acceptance window
Periods close at midnight and invoices go out before the data has settled. Mobile clients buffer, batch exports run on a schedule, a retry queue drains hours late. If the invoice is generated the moment the period ends, every one of those events is either lost or pushed into the next period.
A short delay before the invoice is the cheapest fix in the system. Hold the period open for 24 to 72 hours after it closes, fold in whatever arrives, then freeze. The customer waits one day and the bill is right. The alternative is credit memos, disputes and a manual adjustment process that never quite reconciles.
Providers put their own limits on how far back you can report. Stripe accepts meter event timestamps within the past 35 calendar days or up to five minutes in the future. That window is an outer bound on your backfill strategy. If your raw events sit in a warehouse for six weeks before anyone notices the gap, the provider may already refuse to take them.
Freeze the period and keep the events
Once an invoice is issued, the number behind it has to stop moving. Pre-aggregation makes that awkward when the aggregate table is the only copy. Recomputing a closed period then changes a figure the customer has already been billed for, and every system that cached it is now wrong.
Keep the raw event log append-only and immutable. Treat the aggregate as a read optimization you can rebuild, not as the record. When a dispute arrives, the question is never what the dashboard said in March. It is which 4,182 events sit behind this line and what rule turned them into a number. That takes seconds if the raw rows survived and a week if they were rolled up and dropped. Storage is cheap at this scale. Billing archaeology is not.
Aggregation has to match what you sold
Sum, count and last-value are not interchangeable, and the pricing page usually implies one while the meter computes another. Sum fits API calls and tokens. Count fits completed jobs. Last-value fits a gauge: current seats, storage consumed at the end of the period, active devices. Choose the aggregation when you set the price, because changing it later means re-billing everyone.
The unit has the same problem. A meter that bills raw tokens is accurate and almost impossible for a customer to forecast. A meter that bills per thousand requests, per document processed or per minute of audio is coarser and far easier to discuss at renewal. The unit is a product decision that the pipeline has to compute exactly. Do not sell a metric the meter can only estimate, and be careful with percentile pricing, which needs the full distribution retained rather than a rolled-up average.
Show the customer the number you will bill
Usage dashboards are usually built by a different team, on different data, with a different definition of a month. The customer then compares that figure to the invoice and finds a gap. Each gap is a support ticket, and enough of them turn a usage-based contract into a monthly argument.
Build the dashboard on the aggregation that produces the invoice, from the same frozen period, with the same cutoff. A live figure for the current open period is useful if it is labelled provisional and says when it closes. A provisional number that later moves by three percent because late events landed is expected. A final number that moves is a credibility problem.
Reconcile on a schedule, not during a dispute
A daily job that walks from raw events to aggregate to invoice line, per account and per meter, catches most problems while they are small. It should answer three questions. Does the sum of raw events match the aggregate. Does the aggregate match the line item. Does the line item match what the provider recorded. Each step fails independently. Events go missing between the application and the queue. An aggregation job runs twice and doubles a partition. A provider request times out after it was accepted.
The provider is not a substitute for your own record. Stripe's dedupe window is at least 24 hours, which is not the same as forever, and a replay a week later counts again. Keep your ledger as the source of truth and treat the provider as a downstream replica. When the two disagree, the ledger wins, and the fix is a correction event rather than an edit to a closed row.
Build order that survives contact with finance
If the pipeline is already live, most of this is retrofitting, and the order matters.
- Add the identifier and its unique constraint first. Nothing else works until duplicates are impossible.
- Write the usage row in the same transaction as the billable action, then send asynchronously. The product should not wait on a billing API, and a queue drop should not lose revenue. Stripe caps live meter events at 1,000 calls per second per account, so a burst of product activity needs a sender that batches and backs off rather than a call on the request path.
- Keep raw events append-only, and give each aggregation job a unique key on account, meter and period so a rerun upserts instead of duplicating.
- Set the acceptance window before the first invoice, and write down what happens to events that miss it.
- Version the price rules. A plan change in July must not silently reprice June.
- Build the replay tool early. You will need it the first time an emitter ships a wrong quantity, and a replay is only safe when the identifiers are stable.
None of this is exotic. It is a ledger with an ingestion path, and nearly all of the engineering sits in the unglamorous parts: constraints, windows, immutability and a reconciliation query that runs whether or not anyone is watching. Teams that get those right spend renewal calls talking about the product. Teams that skip them spend theirs explaining a number.
If your usage-based pricing is producing invoices nobody can defend, Tech Hub Services builds and audits metering pipelines: event design, ledger schema, provider integration and the reconciliation jobs that keep finance out of the incident channel. Reach the team at info@techhubservices.com or +1-416-477-6087.