Metering — Counting Billions of Events Without Losing One

Usage-based pricing has a humble prerequisite: you must count correctly. Not approximately — correctly, at whatever volume success brings, across retries, duplicates, late arrivals, and clock skew, because every counted unit is money and every miscounted one is a dispute. Metering is stream processing where the aggregation result appears on an invoice, and that destination changes every engineering trade-off.

Post 2 modeled prices over usage; this post builds the system that measures the usage. It leans on ideas from Stream Processing from First Principles — but where analytics tolerates approximation, metering’s output is billable, and the differences all flow from that.

The metering pipeline

Every metering system is the same four stages, each with a correctness obligation:

The hard problems are all about time and truth

Two meters, one truth

A pattern most teams converge on: real-time approximate, batch exact. A streaming path maintains running totals for product surfaces — dashboards, “80% of quota” alerts, spend caps — where seconds matter and small transient error is tolerable. A batch path recomputes period totals from the raw log at finalization — where correctness matters and latency doesn’t. The invoice always bills from the batch truth; the streaming view reconciles against it continuously, and divergence beyond tolerance is an alert, not a shrug. This buys the product immediacy without ever billing from an approximation — and the raw event log underneath both is the system of record that makes any total re-derivable, auditable, and (post 8) reconcilable.

Two operational notes earn their place early: meter at the value boundary, not the cost boundary — for AI products, bill the thing customers understand (documents processed, minutes transcribed) even while you track tokens internally for margin (AI Cost Optimization manages that side); and cardinality is a design budget — per-customer × per-metric × per-period totals are cheap, but per-request line-item storage at billions of events is a database bill of its own; keep raw events in cheap append storage and aggregates in the fast path.

The takeaway: metering is the emit → ingest → aggregate → rate pipeline with invoice-grade obligations at each stage — deterministic idempotency keys minted at the source, dedup at the door, event-time aggregation into exactly one billing period with a declared grace window, compensating events instead of silent mutation, and rating kept separate from counting. Run two meters — streaming for product immediacy, batch from the raw log for the invoice — reconciled continuously, and meter the unit customers understand while tracking cost units internally. Counting sounds trivial; counting billably is a discipline.

Key takeaways

Further reading

Sources & References

the computation model metering specializes
the property that makes retries billable-safe