Prompt
How to handle usage metering in SaaS
Latest observation
Handling usage metering in SaaS usually comes down to 5 parts:
- Define what you meter
- Capture events reliably
- Aggregate into billable usage
- Expose usage to customers
- Use it for billing, limits, and alerts
Here’s a practical approach.
1) Define the usage model first
Be explicit about what counts as billable usage.
Examples:
- API calls
- Seats/users
- Storage used
- Compute minutes
- Messages sent
- Documents processed
- Active projects/workspaces
For each metric, define:
- Unit: request, GB, seat, minute, etc.
- Granularity: per event, per hour, per day, monthly total
- Rounding rules: exact bytes vs rounded MB, partial seat rules
- Reset period: monthly, daily, never
- Free tier / included quota: e.g. first 10,000 requests included
- Overage policy: block, throttle, or bill extra
A vague metric causes billing disputes later, so nail this down early.
2) Capture usage as immutable events
Don’t just store a running total in one row and increment it. Instead, record usage events.
A usage event should include:
tenant_iduser_idor actormetric_typequantitytimestamprequest_id/event_idfor idempotency- metadata like plan, region, source, etc.
Example:
{
"event_id": "evt_123",
"tenant_id": "acct_456",
"metric": "api_calls",
"quantity": 1,
"timestamp": "2026-10-29T10:00:00Z",
"request_id": "req_abc"
}
Important properties
- Idempotent: same event should not be counted twice
- Append-only: never overwrite raw usage records
- Traceable: you can explain any bill line item later
3) Aggregate usage for billing
Create a separate aggregation layer that computes billable totals.
Typical flow:
- Raw events go into a usage log / event store
- A worker or stream processor aggregates them into daily or monthly buckets
- Billing engine reads the aggregate totals to compute charges
Common tables:
usage_events— raw immutable recordsusage_daily_aggregates— per tenant/metric/daybilling_period_usage— totals for invoice period
This gives you:
- better performance
- easy auditability
- ability to reprocess if logic changes
4) Decide real-time vs batch metering
Real-time
Good for:
- rate limits
- usage dashboards
- immediate overage blocking
Implementation:
- write event
- update near-real-time counters
- use cache/stream processor for fast reads
Batch
Good for:
- invoicing
- monthly reporting
- audit-safe calculations
Implementation:
- process events periodically
- produce final billable totals
- less operational complexity
In practice, many SaaS products use both:
- real-time counters for UX and limits
- batch reconciliation for billing accuracy
5) Make it reliable
Use idempotency keys
If a client retries a request, don’t double count.
Handle late events
Events may arrive late due to retries or async processing. Decide:
- count them in the period they occurred
- or in the period they were received
Usually billing uses event timestamp, not ingestion timestamp.
Reconcile regularly
Run periodic jobs to compare:
- raw events vs aggregates
- aggregates vs invoices
Don’t trust client-side reporting alone
Meter on the server side whenever possible.
6) Make the system auditable
Customers will ask: “Why was I charged this?”
You should be able to show:
- raw events
- aggregation rules
- period boundaries
- rate card
- discounts/credits
Good practice:
- store a billable usage ledger
- keep versioned pricing rules
- log computation inputs for each invoice
7) Support quotas, limits, and alerts
Metering isn’t just for billing.
Use the same usage data for:
- quota enforcement
- warning emails at 80%, 90%, 100%
- plan upgrades
- usage-based product analytics
Examples:
- notify when storage exceeds 85%
- soft limit with warnings
- hard limit that blocks new usage
- throttling for API consumption
8) Common pricing patterns
Flat subscription
No metering needed except for internal analytics.
Seat-based
Track active seats, assignments, invites, or billable users.
Usage-based
Bill per unit consumed.
Tiered pricing
E.g.:
- 0–1M API calls: $0.001 each
- 1M–5M: $0.0008 each
Hybrid
Base fee + usage overage.
This is very common and often easiest to sell.
9) Recommended architecture
A simple, robust setup:
- Product action occurs
- Backend emits a usage event
- Event is written to durable storage
- Aggregator updates tenant/period totals
- Billing service calculates charges
- Dashboard reads aggregates
- Alerts use thresholds from aggregates
Possible components:
- Postgres for event log and aggregates
- Queue/stream like Kafka, SQS, Pub/Sub
- Worker for aggregation
- Billing provider integration like Stripe Metered Billing, Orb, Lago, Amberflo, etc.
10) Build vs buy
Build it yourself if:
- you have simple metering
- you need full control
- your pricing model is custom but not too complex
Buy a metering/billing platform if:
- you need quick launch
- you have many pricing plans
- you want invoicing, tax, proration, credits handled
Popular options:
- Stripe Billing usage-based billing
- Lago
- Orb
- Chargebee
- Metronome
- Amberflo
11) Practical advice
Start with:
- one usage metric
- server-side event capture
- monthly aggregation
- simple dashboard
- Stripe or a billing provider for invoicing
Avoid early complexity like:
- multiple complex metrics
- per-second billing
- client-side usage reporting
- tightly coupling usage tracking to invoice generation
12) Example implementation pattern
Event collection
- API endpoint
/usage - accepts
{tenant_id, metric, quantity, idempotency_key}
Storage
- insert into
usage_eventswith unique constraint onidempotency_key
Aggregation job
- every minute/hour/day:
- group by tenant, metric, billing period
- update aggregate table
Billing
- at invoice time:
- read totals for period
- apply pricing tiers
- generate invoice line items
If you want, I can also give you:
- a database schema for usage metering,
- a Stripe-based implementation, or
- a reference architecture diagram.