%20(1).png)
Billing Mediation: What It Is, How It Works, and Why It Matters
What billing mediation is, why AI usage pushes it harder than traditional SaaS, the four functions, a worked event example, and build vs. buy guidance.
An automated billing system runs well on scheduled rules until pricing depends on the real-time state. Here's the architecture, and where it breaks.
%20(1).png)
A customer's payment cleared at 9 a.m. The dunning job that ran at 3 p.m. was still working off a balance snapshot taken the night before, when the payment was still marked outstanding, and it suspended the account anyway.
Support spent the rest of the day explaining to an enterprise customer why their access had vanished hours after they'd paid.
An automated billing system is only as reliable as the assumption that its parts are working from the same data.
An automated billing system is software that generates invoices, processes payments, and manages routine billing tasks, such as retries and dunning, without requiring a person to manually trigger each step.
It runs on rules. A defined condition is checked, and a defined action follows.
Most billing automation comes down to five recurring tasks, each triggered by a specific event rather than run on demand.
Each of these runs independently and checks its own trigger against its own view of the account. That works fine as long as every job's view of the account stays accurate and up to date.
An automated billing system runs on four components working together. That includes a rules engine, a scheduler, a webhook or event layer, and a state store. Here's what each one does.
The rules engine evaluates conditions and triggers actions, the actual "if this, then that" logic behind retries, dunning, and proration.
Each rule is a condition-action pair: a condition checked against incoming event data, and an action executed when that condition matches. Most engines support some form of prioritization or ordering, too, since a single event, a failed payment, for example, can match more than one rule at once.
A dunning rule defined for a rules engine might look something like this:
json
{
"rule_id": "dunning_retry_1",
"condition": {
"event": "payment_failed",
"attempt_count": { "less_than": 3 }
},
"action": {
"type": "retry_payment",
"delay_hours": 72
}
}
The engine checks incoming event data against the condition block and executes the action if it matches.
The scheduler handles anything time-based, checking for billing cycle dates, retry windows, and dunning thresholds on a recurring interval, hourly or daily, in most systems.
Its job is narrower than it sounds. It does not decide what should happen. Instead, it decides when to ask the rules engine whether something should happen.
A scheduler running on a fixed cadence is also where many timezone and clock-drift bugs in billing automation originate, since "the billing cycle date arrived" depends entirely on which clock is doing the counting.
The webhook or event layer receives and emits events, such as a payment succeeding, a plan changing, or an invoice generating, so that other systems can react without polling for changes.
Incoming webhooks from a payment provider need to be verified (by checking a signature to ensure the event can be trusted), deduplicated (since most providers guarantee at-least-once delivery), and processed idempotently before anything downstream acts on them.
Outgoing events work the same way in reverse, notifying other internal systems, an entitlements service, and a customer dashboard that something changed.
The state store holds the current account data that all other components read and write, which means balances, subscription status, and payment history.
This is the component that determines whether the other three agree with each other. A rules engine and a scheduler can both be implemented correctly and still produce contradictory results if they read from stale or inconsistent data instead of a single, current source.
Most reliability problems in these systems trace back to two components disagreeing on the same account, often because one read from the state store before the other's write had been committed.
The main difference between automated billing and manual billing is who executes the repetitive steps.
Manual billing means a person creates, checks, and sends each invoice individually, workable at low volume, where a human can catch errors by hand. Automated billing removes that person from the repetitive steps, running the same logic against every account without intervention.
I've reviewed enough of these systems to know the tradeoff always shows up at the edges.
Automation handles the situations it was built to anticipate. A refund issued outside the normal flow, a plan change that doesn't match any defined rule, and a customer whose situation falls between two conditions all still need a person to notice and step in, since the rule itself has no way to recognize what it wasn't written for.
Rule-based automation stops working once a decision depends on a state that's still changing at the moment it needs to be made, instead of a condition that's fixed and checkable on a schedule.
Automation rules are conditional and scheduled. If a payment fails, retry in three days. If a balance goes unpaid for two weeks, suspend the account. I've read a lot of these rule sets, and almost all of them assume the thing being checked holds still long enough for the check to matter.
That assumption breaks once pricing depends on a state that keeps changing in real time.
A rule that checks whether a customer has paid can't verify whether that same customer still has sufficient credits for the request currently being processed, because usage is continuous, and the rule runs only on a schedule.
Usage limits, per-team budget caps, and multi-tenant allocations all require a decision at the time of the request.
A pricing and usage orchestration layer adds a single, current view of pricing, packaging, and usage that every automation can use.
Without this layer, a dunning rule and a proration rule each maintain their own version of the truth, which causes them to drift further apart until a customer's invoice contradicts the account record.
An orchestration layer removes that by becoming the one place where pricing and packaging changes get made, so every downstream automation reads from the same definition.
It also changes when decisions get made. Rule-based automation runs on a schedule, checking conditions in batches.
An orchestration layer can make decisions synchronously, at the moment a request or a billing event occurs, which is the only way to correctly answer a question like whether a specific action should be allowed given a customer's current balance.
Idempotency means running the same operation twice produces the same result as running it once.
In automated billing, this matters most for webhook handling, since most providers guarantee at-least-once delivery, meaning the same event can arrive more than one time.
The standard approach is an idempotency key, a unique identifier attached to each event, checked against a short-lived store before the handler processes anything. If the key has already been seen, the event gets acknowledged and dropped instead of reprocessed.
Without this check, a duplicated payment_failed webhook can trigger two separate retry sequences for the same failed charge, or a duplicated invoice.created event can generate the same invoice twice.
I've traced enough of these incidents back to their root cause to notice the same handful of patterns keep showing up, regardless of the stack or the team that built it.
Each of these traces back to the same root issue. Components get built and tested independently, then are expected to agree with each other in production, and nobody verifies that they do until a customer notices first.
A few things separate an automated billing system that holds up under real-world use from one that becomes its own maintenance project.
This list doesn't matter until it does. This is usually the moment two automations disagree, or someone has to explain a charge line by line.
Everything covered so far, the components, the failure modes, the orchestration layer itself, describes a category. This is what it looks like as an actual product.
Stigg is the usage runtime for AI products. Entitlements, credits, usage limits, and spend governance are enforced synchronously in the request path. It's built to be the orchestration layer this piece has been describing, running alongside whatever billing automation and invoicing tools are already in place:
Deciding whether the request behind a failed payment should have gone through at all is a difficult problem, and it's the one the Stigg docs cover in detail.
No. A payment gateway processes the transaction itself, moving money between accounts. An automated billing system is the broader software that decides when to generate an invoice, retry a failed charge, or send a dunning notice, and it typically calls a payment gateway to execute the actual transaction.
Generally, no. Automated billing systems operate on scheduled or conditional rules, so they respond only after usage is recorded. Preventing overuse during the request requires a separate real-time entitlements layer.
The cost of building an automated billing system varies widely depending on complexity. Even a basic system, including invoicing, retries, and dunning, typically takes a dedicated engineer several months to build and longer to maintain as pricing models change.
Duplicate invoices are usually caused by a webhook or scheduled job processing the same event twice without an idempotency check. Most systems handle this by requiring a unique event ID before accepting a retrigger of the same action.
Partially. They can generate invoices from usage totals, but usage and credit-based pricing often need real-time balance checks that scheduled automation isn't built to perform on its own.