Batch aggregation closes a period at midnight, totals the events, and issues an invoice. The customer sees the bill at month end. That works, and it is easier to run than the alternative. The trouble is timing: once the bill arrives, the customer had no chance to stop anything. An agent that ran 400 percent over expected usage ran, finished, and the invoice records the whole run. Spending controls depend on knowing the current total, and batch aggregation does not have one until the batch runs.

Real-time aggregation keeps a balance per customer updated as events arrive. Each event hitting the metering endpoint increments the total immediately, so a dashboard reflects usage to within seconds, and threshold alerts fire when the total crosses a limit. It takes more infrastructure than batch; it also makes possible product features batch cannot support.

Where Batch Wins

Batch processes events asynchronously from ingestion. Events go to a durable store, a queue or a raw event table, and a scheduled job computes totals. Out-of-order arrival is a non-issue because the job scans the whole window at computation time instead of maintaining incremental state.

That out-of-order tolerance is a genuine edge over real-time. Concurrent sub-agents in a distributed system can deliver events in an order that has nothing to do with their logical sequence. An event with occurred_at at 2:58 PM may show up after one stamped 3:04 PM. Batch simply sums everything in the window regardless of arrival order. Real-time aggregation that applies events to a running total as they land can post a temporarily wrong balance when events arrive late, and correcting it takes compensating logic.

Batch is also easier to reason about for accounting. It runs once, deterministically, over a closed set of events. A wrong result can be rerun against the same data and produce the same wrong result, which at least makes the bug reproducible.

What Only Real-Time Can Do

Spending controls: a hard cap that stops counting billable events once a customer's running total passes a threshold, and soft limits that alert while allowing work to continue. Both need a queryable current balance accurate to the second.

Usage dashboards: a customer-facing view of period consumption that refreshes live. For customers running large pipelines, watching usage accumulate is how they catch a runaway workflow before the period ends.

Anomaly detection: spotting unusual consumption early. A customer running at three times their historical daily average can be flagged within minutes by a real-time system. A batch system flags it at the next run, possibly hours later.

Running Both

Paygentic combines them. Events are written to a fast Redis-backed counter for real-time balance queries and at the same time to a durable event store used for batch invoice generation. The counter answers dashboards and spending controls. At period close, the batch job over the durable store produces the authoritative invoice.

The two can drift. A node failure that loses a Redis counter is repaired by rebuilding from the durable record. Invoices always come from the durable store, never from the counter. Because of that split, a real-time layer failure leaves billing correctness untouched; what it does damage is the dashboard experience, which is a product concern rather than an integrity concern.

The design rule underneath: the durable event store is the source of truth for billing, and the real-time layer is derived state cached for product features. Both are written on every ingest, and they serve different consumers with different consistency needs. Collapsing them into one system forces you to give up either billing guarantees or freshness, depending on which requirement you sacrifice.

Picking Where to Begin

At an early stage, batch is the pragmatic start. It is correct, debuggable, and easier to operate. The real-time layer can come later when requirements call for it, since adding a Redis counter beside an existing durable event store is additive rather than a rebuild.

The mistake is starting with batch for its simplicity and then building features that assume a live balance. A dashboard querying the batch aggregate is a limitation customers will notice and ask about. Commit to a customer-facing usage dashboard and you have committed to the real-time layer. Building the dashboard against batch data first turns a simple addition into a migration you could have skipped.

Billing engineering for AI products

How usage becomes an invoice: architecture, schema choices, pricing mechanics. No product pitches.