The Metering & Billing Surface: From Usage Event to Invoice
Chapter 20 made cost a runtime constraint and showed where the spend comes from. Turning "the gateway processed 1.3 million tokens" into "this customer owes $X, and we earned $Y of margin on it" is a different system with different requirements. Telemetry can drop a span and no one is harmed; a billing pipeline that double-counts or loses events either erodes customer trust or quietly erodes margin. The discipline is to separate the high-volume, real-time metering path from the low-volume, financial-grade invoicing path — and to know exactly where the seams are so you can decide what to build and what to buy.
25.1 The decomposition
Usage becomes money through a chain that most tools blur together; naming the stages tells you where your platform ends and a billing vendor begins. Meter — count what happened. Mediate — deduplicate and normalize the raw events. Rate — apply pricing to produce charges. Bill — aggregate charges over a period. Invoice — render the bill. Collect — take payment. Recognize — book the revenue. The first three are high-volume and real-time; the last four are financial and audited. The boundary between them is the most important architectural line in the whole surface.
25.2 The emission surface — where usage is born
The model gateway is the canonical meter point. It already sees the model, the input/output/cached token split, latency, and success or failure (Ch. 6, Ch. 18), so metering rides on instrumentation you built anyway. Other dimensions join from their own sources: sandbox-seconds (Ch. 9, Ch. 24), tool calls (Ch. 8), retrieval and storage (Ch. 10–11), and seats. Every usage event carries an idempotency key — naturally run_id plus step_id — so that at-least-once delivery deduplicates to effectively-once. And you reconcile metered tokens against the bills you receive from Bedrock, Anthropic, or OpenAI: the gap between what you metered and what you were charged is your margin alarm.
25.3 The stack and the integration strategy
| Tool | Shape | Notable for |
|---|---|---|
| Lago | Open-source metering + billing | Event ingestion, aggregation, plans, invoicing, prepaid wallets/credits |
| OpenMeter | Open-source, CloudEvents-based | Real-time aggregation oriented to AI token metering; Stripe integration |
| Metronome | Usage-based billing platform | Real-time, high-scale; used by large AI vendors |
| Orb | Event-based usage billing | Flexible usage models and revenue reporting |
| m3ter | Metering & rating engine | Complex rating feeding an existing billing system |
| Stripe Billing | Billing + payments | Meter events, usage pricing, invoicing, and collection in one |
The strategy that holds these together: ingest events idempotently and tolerate late or out-of-order arrival; pre-aggregate so a real-time balance is always queryable; choose prepaid wallets with real-time decrement or postpaid metered invoicing per product; and price across multiple dimensions — per-token (input, output, and cached differ), per-run, per-tool-call, per-seat, a platform fee, a markup on raw model cost. The defining move for an agentic platform is to close the loop: the aggregated balance feeds back into the gateway so an over-budget tenant or runaway agent is throttled or blocked in real time. Metering and the budget enforcement of Chapter 20 are two ends of one mechanism.
One source of truth for usage — the gateway — feeds both the real-time control loop that caps spend and the financial pipeline that bills it. Keep the high-volume metering path separate from the audited invoicing path, but let both read from the same metered events, so what you enforce and what you charge can never disagree.
Retries, fan-out, and multi-agent calls (Ch. 12) make it easy to emit the same usage twice; without idempotent keys and provider reconciliation you over-bill (and lose trust) or under-bill (and lose margin). And usage events are commercially sensitive and often carry tenant identifiers — they fall under the same residency and PII rules as traces (Ch. 17, Ch. 18), not a laxer standard because they're "just counters."