| Primitive | Role in this flow |
|---|---|
| Events + Metrics | Your agent emits usage events; a metric turns them into a billable quantity (e.g. summed tokens). See Metering. |
| Real-time billing plan | Prices the metered usage and debits the wallet continuously (~5s) instead of at period end. |
| Prepaid wallet | The customer’s balance. Funded inline in your app via the wallet widget; debited automatically as usage accrues. |
The shape of the model: customer funds wallet → agent runs → usage metered → wallet debited in near-real-time → balance hits zero → metering pauses → customer tops up → metering resumes. No invoices to chase, no surprise bills — the customer only spends what they’ve funded.
How it works
Prerequisites
- A Monigo account and a secret API key (
mk_live_…). All steps below run server-side — never expose the key in client code. - The
@monigo/sdkpackage (examples use TypeScript; the REST endpoints are shown alongside).
Step 1 — Create the customer
Step 2 — Define your usage metric
A metric turns raw events into a billable quantity. For token billing, sum atokens property from your agent_token_usage events.
Step 3 — Create a real-time prepaid plan
Setbilling_mode: "realtime" and attach a price for your metric. Here, ₦0.10 per token via a per_unit price.
Step 4 — Create the wallet and subscribe
The wallet holds the prepaid balance; the subscription connects the customer to the plan. Both use the Monigo customerid (not the external_id).
Step 5 — Let the customer fund the wallet
Drop the prepaid wallet UI into your app. Mint a customer-scoped portal token on your server, then render<WalletWidget> — it shows the live balance, opens the provider’s inline checkout on Top up, and refreshes automatically. Full details in Wallet management and Embedding the portal.
<WalletWidget> is available for React, Svelte, and Vue (@monigo/{react,svelte,vue}). The provider webhook is the source of truth for crediting — never credit the wallet from the browser; Monigo credits it automatically when the payment provider confirms.
Step 6 — Meter usage
As your agent runs, send aagent_token_usage event for each unit of work. Reference the customer by your external_id, put the token count in properties, and always include an idempotency_key so retries never double-charge.
What happens automatically
Within a few seconds of each event, Monigo:- Reconciles and debits. It recomputes the period’s cumulative cost from the metric and debits the wallet for the marginal increase since the last charge — so the running cost is always exactly
price(total usage so far). Typical latency is ~5 seconds. - Updates the live balance. The
<WalletWidget>poll picks up the new (lower) balance, so the customer watches their balance draw down as the agent works. - Pauses on empty. When the wallet can’t cover the next charge, the subscription is paused, further events for that customer are dropped, and a
subscription.prepaid_balance_insufficientwebhook fires. - Resumes on top-up. When the customer funds the wallet, the subscription reactivates, the accrued (unpaid) usage is charged from the new balance, and metering continues.
- Closes the period. At month end, Monigo emits one paid summary invoice (per-metric line items, total = what was actually debited) for the customer’s records — with no additional debit.
Step 7 — Handle the webhooks
Register an endpoint (Dashboard → Webhooks) and react to these events. See Webhooks for signature verification.| Event | When | What to do |
|---|---|---|
subscription.prepaid_balance_insufficient | Wallet can’t cover the next charge; subscription paused | Notify the customer to top up; surface the wallet widget / a “fund wallet” CTA |
customer.wallet.topped_up | Wallet credited (the customer funded it) | Optionally confirm in-app; the subscription auto-resumes — no action required |
invoice.paid | Monthly summary invoice issued (already paid from the wallet) | Store/forward for the customer’s billing records |
subscription.prepaid_balance_insufficient
How real-time charging works
For the curious — and for reasoning about edge cases:- Marginal reconcile, not per-event pricing. Each cycle charges
price(cumulative usage) − already_charged. This keeps tiered/package/overage pricing exact across a period and makescapcharge its flat fee exactly once. - Crash-safe and idempotent. Each debit carries a deterministic idempotency key, so a retry after a failure can never double-charge.
- Bounded free usage. Because charging trails ingestion by a few seconds, a tiny amount of usage can be metered in the window between the balance hitting zero and the pause taking effect. This is bounded to roughly the charge interval.
- Wallet is the source of truth. Real-time debits are ordinary
usageledger entries; the monthly invoice merely records the total. There is never a second debit at period end.
A working example
A minimal SvelteKit app — mint a portal token, render<WalletWidget>, and simulate agent spend — lives at platform/samples/ai-agent-wallet.
Next steps
Wallet management
Wallet operations, virtual accounts, and the
<WalletWidget> in depth.Metering
Event ingestion patterns, batching, and idempotency.
Pricing models
Tiered, package, overage, and cap pricing — all work with real-time billing.
Embedding the portal
Drop-in components for React, Svelte, and Vue.

