usage-based-pricing
Architecture and implementation strategies for consumption-based billing models in SaaS products.
安装与使用
请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。
查看来源 ↗使用指南技能说明
Usage-Based Pricing (Consumption Billing)
Overview
Usage-based pricing (UBP) is a billing model where customers pay based on their consumption of a product or service. Unlike fixed subscription models, UBP aligns cost with value for the customer and revenue with infrastructure expense for the provider.
Core Principle: "Charge for the value metric most correlated with the user's success (e.g., messages sent, minutes watched, GB stored)."
1. Usage-Based Pricing Models
| Model | Description | Example |
|---|---|---|
| Pure Pay-per-use | Linear cost per unit (e.g., $0.01 per query). | AWS Lambda, Twilio |
| Tiered (Graduated) | Cost per unit decreases as volume increases. | Stripe, S3 |
| Volume-based | Price for all units changes based on total volume. | Bulk SMS providers |
| Hybrid | Fixed base fee + Overage/Usage. | Snowflake, Datadog |
2. Metering Fundamentals
Metering is the technical process of capturing, aggregating, and reporting usage events.
What to Meter?
Select a Value Metric that is:
- Understandable: The user knows what they are being charged for.
- Scalable: It grows as the user's business grows.
- Measurable: You can track it with 99.999% accuracy.
Aggregation Methods
- Sum: Total units in a period (e.g., total API calls).
- Unique (Count Distinct): Total unique items (e.g., monthly active users).
- Max (High Water Mark): The highest point of usage (e.g., peak concurrent users).
- Last: The value at the end of the period (e.g., current storage size).
3. Usage Tracking Architecture
Implementation requires an "Event-to-Invoice" pipeline.
Architecture Diagram (Simplified)
[App Service] --> [Event Bus (Kafka/Kinesis)] --> [Meter Aggregator] --> [Billing DB]
|
V
[Stripe/Billing API]
The Reliability Challenge
A billing system must be Idempotent. If you process the same usage event twice, you shouldn't charge the customer twice.
4. Implementation with Stripe Billing
Stripe is the industry standard for metered billing.
Step 1: Define a Price in Stripe
Use recurring.usage_type = 'metered' and aggregate_usage = 'sum'.
Step 2: Reporting Usage (Node.js)
import Stripe from 'stripe';
const stripe = new Stripe('sk_test_...');
async function reportUsage(subscriptionItemId: string, usageQuantity: number) {
const timestamp = Math.floor(Date.now() / 1000);
try {
await stripe.subscriptionItems.createUsageRecord(
subscriptionItemId,
{
quantity: usageQuantity,
timestamp: timestamp,
action: 'increment', // Adds to current period total
},
{
idempotencyKey: `usage-${subscriptionItemId}-${timestamp}`
}
);
} catch (err) {
console.error('Failed to report usage:', err);
}
}
5. Predictable Billing (Avoiding "Bill Shock")
Usage-based pricing can be scary for customers. You must provide transparency.
Guardrails
- Usage Quotas (Hard Limits): Stop service when a budget is hit.
- Spending Alerts (Soft Limits): Email the user when they hit 50%, 80%, and 100% of their "target" budget.
- Real-Time Dashboard: Show users their currently accrued bill before the invoice is generated.
Python Example: Checking Quota
def check_quota(user_id, metric, limit):
current_usage = cache.get(f"usage:{user_id}:{metric}")
if current_usage >= limit:
return False, "Quota Exceeded"
return True, "OK"
6. Unit Economics: Margin Calculation
When implementing UBP, you must track your Cost of Goods Sold (COGS) relative to your pricing.
| Metric | Revenue per Unit | COGS per Unit | Contribution Margin |
|---|---|---|---|
| API Call | $0.05 | $0.002 (Compute) | 96% |
| LLM Inference | $0.15 | $0.10 (GPU/Token) | 33% |
| Storage | $0.08 | $0.02 (S3) | 75% |
If your LLM margin is too low, you need to either raise prices or optimize inference (see llm-cost-optimization).
7. Metering Infrastructure Tools
- Lago: Open-source metering and billing engine.
- Metronome: Enterprise usage-based billing platform.
- Orb: Flexible pricing and usage management.
- Apache Flink: For high-volume streaming aggregation.
8. Real-world Examples
Twilio (Pure Pay-per-use)
- Metric: Number of SMS segments or voice minutes.
- Why it works: Cost is almost entirely external (carrier fees), so the price must scale exactly with usage.
Datadog (Hybrid)
- Metric: Number of hosts (fixed) + GB of logs (metered).
- Why it works: Captures value from both infra scale and data scale.
Snowflake (Credit-based)
- Metric: "Credits" based on virtual warehouse size and uptime.
- Why it works: Simplifies complex compute cost into a single abstract currency.
9. Implementation Checklist
- Idempotency: Do usage events have a unique ID to prevent double-billing?
- Precision: Does the aggregator handle floating point math correctly (no rounding errors)?
- Delay/Latency: How long does it take for usage in the app to appear on the dashboard? (Target: < 5 mins).
- Backfill: Can you re-process events if the billing pipeline fails?
- Limits: Are there spending caps to protect customers from accidental "infinite loops"?
- Margin: Have you calculated the infrastructure cost of 1 million units of the metric?
Related Skills
-
42-cost-engineering/cloud-cost-models -
42-cost-engineering/cost-observability -
45-product-thinking/pricing-strategies
使用示例
查看来源仓库中的使用示例与配置说明。
查看来源 ↗来源历史
- 收录时间
- 2026-01-20
- 索引更新时间
- 2026-01-26
- Branch
- main
收录与同步日期属于本站记录。源码修改历史请查看来源仓库。
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!