Core mechanics
When a customer consumes a metered resource, you create a usage event that captures:- Who consumed it (customer ID)
- What was consumed (metric ID and quantity)
- When it occurred (timestamp)
- How to prevent duplicates (idempotency key)
Event processing models
Direct account processing
Standard flow for real-time billing:- Event arrives with customer and metric identifiers
- System resolves active subscription and pricing rules
- Cost calculation based on quantity × unit price (or dynamic/percentage models)
- Immediate account debit from consumer
- Instant credit to merchant (minus platform fees)
- Transaction recording and analytics update
Entitlement-based processing
Pre-authorized payment flow:- Event includes entitlement ID linking to reserved funds
- Same pricing calculation occurs
- Deduction from pre-reserved entitlement balance
- Merchant credit remains immediate
- Entitlement balance decrements
Event structure
Essential fields for every usage event: Identity & DeduplicationidempotencyKey- Unique identifier preventing duplicate processingcustomerId- Links consumption to specific customer relationshipmerchantId- Identifies the service provider
timestamp- Exact moment of resource consumption (ISO 8601)properties- Array of metric consumption records
entitlementId- References pre-authorized payment reservationmetadata- Arbitrary key-value pairs for internal tracking
Consumption properties
Each element in the properties array represents one metric’s consumption:price field serves different purposes based on the pricing model:
- Dynamic pricing: Total cost for this specific consumption
- Percentage pricing: Base amount for percentage calculation
- Standard pricing: Not required (uses plan’s fixed rate)
Pricing model interactions
dynamic and percentage are legacy models. Existing prices that use them keep billing exactly as described below, but new prices can no longer be created with them — use standard instead. For percentage / revenue share, create a standard price whose unit price is the rate (a unit price of 0.1 charges 10% of the metered value). See Prices.Standard pricing
Fixed rate multiplication. Quantity × configured unit price. For percentage-style charges, set the unit price to the rate and send the base amount as the quantity.Dynamic pricing (legacy)
Runtime price determination. The event carries the actual price within bounds set by the plan. Useful for spot pricing, market rates, or time-based variations.Percentage pricing (legacy)
Transaction-based fees. The event includes the transaction amount; the system calculates the percentage cut with min/max bounds applied.Idempotency and reliability
The idempotency key ensures exactly-once processing semantics:- Same key within time window → subsequent attempts ignored
- Failed requests can be safely retried with identical payload
- Generate deterministic keys from your internal identifiers
Timing constraints
Events must align with subscription billing periods:- Timestamp within active subscription range
- Events outside current period may be rejected
- Historical backfill requires special handling
Batch processing
For high-volume scenarios, batch multiple events in a single request:- Reduces network overhead
- Atomic processing (all succeed or all fail)
- Same validation rules per event
- Ideal for periodic bulk reporting
Regional optimization
Leverage edge infrastructure for reduced latency:- Regional endpoints process events locally
- Entitlements enable ultra-low latency processing
- Critical for real-time applications
Error handling
Common failure scenarios and recovery: Validation Failures- Invalid customer/metric IDs → verify references exist
- Timestamp outside billing period → check subscription status
- Price outside dynamic bounds → validate against plan limits
- Insufficient account balance → requires top-up or entitlement
- Entitlement exhausted → create new entitlement
- Network timeouts → retry with same idempotency key
Design patterns
High-frequency reporting
For systems generating thousands of events per second:- Buffer and batch events locally
- Use entitlements to guarantee payment
- Leverage regional endpoints
- Implement exponential backoff for retries
Guaranteed delivery
For critical billing accuracy:- Local event queue with persistence
- Idempotency keys derived from queue position
- Dead letter queue for failed events
- Periodic reconciliation jobs
Cost attribution
For detailed cost tracking:- Rich metadata on each event
- Hierarchical customer IDs for sub-accounts
- Temporal bucketing for period analysis
- Tag-based grouping in metadata
Next steps
- Customer Lifecycle - Understand billing flows
- Entitlements - Pre-authorize payments
- Accounts - Manage payment instruments