Prompt
How do I set up billing and usage metering for SaaS?
Latest observation
Setting up billing and usage metering for a SaaS product usually means building three layers:
- Meter usage — capture what customers do
- Price usage — decide how to charge for it
- Bill and collect — invoice, charge, reconcile, and handle changes/refunds
Here’s a practical blueprint.
1) Decide what you’re charging for
Start by defining your billable units clearly.
Common models:
- Flat subscription: $X/month for access
- Per seat: charge per active user
- Usage-based: charge per API call, GB stored, compute minutes, messages sent, etc.
- Tiered pricing: different price bands as usage increases
- Hybrid: base fee + usage overage
For each meter, define:
- Unit: what is counted? (request, GB, minute, seat)
- Aggregation: per event, per hour, per day, monthly
- Rate: fixed price or tiered
- Billing timing: prepaid, postpaid, real-time deduction, monthly invoice
- Rounding rules: e.g. per KB rounded up to next MB
Document this as a pricing spec before coding.
2) Design the usage data model
You need a reliable event record for billing.
A typical usage event includes:
event_id(unique, idempotency key)customer_id/account_idsubscription_idmeter_namequantitytimestampsourcemetadata(project, region, plan, etc.)
Example:
{
"event_id": "evt_123",
"account_id": "acct_456",
"meter_name": "api_requests",
"quantity": 1,
"timestamp": "2026-11-01T10:15:00Z",
"metadata": {
"endpoint": "/v1/search"
}
}
Important rules:
- Idempotency: duplicate events must not double-bill
- Immutable events: never edit raw usage; append corrections instead
- Time zone consistency: usually bill in UTC
- Late events: define whether you accept backdated usage and how far back
3) Instrument your product to capture usage
Capture usage as close to the source as possible.
Examples:
- API gateway logs request counts
- Database/storage layer reports consumed bytes
- Background jobs emit compute minutes
- App code emits “seat activated” or “message sent”
Implementation tips:
- Use an event pipeline or queue (Kafka, Kinesis, Pub/Sub, SQS)
- Send usage asynchronously so billing doesn’t slow product traffic
- Batch and retry safely
- Log raw events for auditability
Good practice:
- Capture raw events and also build rollups for billing performance
- Keep an internal “source of truth” ledger, not just dashboard aggregates
4) Build aggregation and pricing logic
At billing time, you’ll aggregate usage over the billing period.
Typical process:
- Fetch all usage events for customer + period
- Deduplicate by
event_id - Sum by meter
- Apply pricing rules
- Compute invoice line items
- Store calculation results for audit
Pricing examples:
- Simple: 10,000 API requests × $0.001 = $10
- Tiered:
- first 100k requests = free
- next 900k = $0.001
- above 1M = $0.0005
- Seat-based:
- active seat count on billing date or average active seats in month
Make sure you define whether:
- Billing is based on peak, average, last known, or max monthly active
- Usage is counted on event time or processing time
5) Use a billing engine or build your own
Option A: Use a billing platform
Common choices:
- Stripe Billing
- Chargebee
- Recurly
- Paddle
- Orb / Metronome / Lago for usage-based billing
Pros:
- Faster to launch
- Handles invoicing, taxes, dunning, payment methods
- Built-in proration, coupons, subscriptions
Cons:
- Less flexibility for custom billing models
- Data model constraints
- Platform fees
Option B: Build your own
Useful if you need:
- Highly custom pricing
- Complex enterprise contracts
- Multi-dimensional meters
- Internal ledger and custom invoicing
If you build it yourself, you’ll need:
- Usage ingestion service
- Billing ledger
- Pricing service
- Invoice generator
- Payment collection
- Tax logic
- Dunning and retries
- Customer portal
A common path is:
- Start with Stripe/Chargebee
- Keep your own internal usage ledger
- Export usage totals to the billing provider
6) Handle invoicing and payment collection
Your billing cycle needs:
- Invoice creation: monthly, annual, or on threshold
- Payment collection: auto-charge card or net terms/invoice
- Failed payments: retries, dunning emails, account suspension
- Proration: if customers upgrade/downgrade mid-cycle
- Credits and adjustments: refunds, SLA credits, goodwill credits
Things to support:
- Trials
- Discounts/coupons
- Promo periods
- Usage caps / overage limits
- Purchase orders and invoicing for enterprise customers
7) Add a customer-facing usage dashboard
Customers need transparency.
Show:
- Current period usage
- Meter breakdown
- Estimated bill
- Historical invoices
- Rate card / pricing explanation
- Alerts when nearing thresholds
Benefits:
- Fewer disputes
- Better trust
- Less support burden
8) Build controls and safeguards
Billing systems can create expensive mistakes. Add:
- Usage caps
- Alerts at 50%, 80%, 100%
- Rate limits to prevent runaway usage
- Manual review for unusually high bills
- Audit logs
- Reconciliation jobs between raw events, rollups, and invoices
Also define what happens if:
- Events arrive late
- Events are duplicated
- A meter changes mid-cycle
- A customer changes plans
- A pricing rule is updated
9) Reconcile everything
Reconciliation is essential.
Compare:
- Product logs vs usage events
- Usage events vs billing rollups
- Billing rollups vs invoices
- Invoices vs payments received
Run daily/monthly checks for:
- Missing usage
- Duplicate events
- Negative adjustments
- Unexpected zero-usage accounts
- Large spikes
10) Start simple, then evolve
A good MVP:
- One or two billable meters
- Monthly billing
- Usage events stored in a database
- Rollup job that sums monthly totals
- Stripe/Chargebee for invoices and payments
- Usage dashboard with estimated charges
Then add:
- Real-time metering
- Tiered rates
- Multi-product billing
- Commitments/minimums
- Enterprise invoicing
- Tax handling and multi-currency
Recommended architecture
A common SaaS billing architecture looks like this:
- App/API emits usage events
- Event queue buffers them
- Usage service validates and stores them
- Billing ledger stores immutable usage and adjustments
- Aggregator computes monthly totals
- Pricing engine maps totals to charges
- Billing provider issues invoices and collects payment
- Dashboard shows usage and forecast
Key design principles
- Keep usage events immutable
- Make billing idempotent
- Separate raw usage from priced charges
- Favor auditable ledgers
- Make pricing rules explicit and versioned
- Build reconciliation from day one
If you want the fastest path
If you’re early-stage:
- Use Stripe Billing or Chargebee
- Store usage events in your DB with idempotency keys
- Run a nightly/month-end aggregation job
- Send monthly usage totals to the billing platform
- Expose a simple usage page to customers
If you want, I can also give you:
- a sample database schema
- a Stripe usage-based billing setup
- or a step-by-step implementation plan for a specific stack like Node.js, Python, or Rails.