计量计费与按用量定价
通过向 Stripe 报告用量、配置计量价格并构建基于用量的层级方案,按照客户的实际使用量收费。
计量计费与按用量定价 是 CoddyKit 上的免费 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Powered SaaS: Stripe + Auth + Billing + Deploy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Usage-Based Pricing?
Flat subscriptions do not fit products where cost scales with consumption — like AI tokens or API calls. Metered billing charges customers for actual usage, aligning revenue with cost.
Licensed vs Metered Prices
Stripe prices come in two usage types:
- Licensed: a fixed quantity (e.g. 5 seats)
- Metered: quantity reported over time and billed at period end
Creating a Metered Price
Define a recurring price with usage_type: metered. Stripe sums reported usage for each billing cycle.
await stripe.prices.create({
product: productId,
currency: 'usd',
recurring: { interval: 'month', usage_type: 'metered' },
unit_amount: 2
});Subscribing Without a Quantity
For metered items you do not set a quantity at subscription time — Stripe learns it from usage reports.
await stripe.subscriptions.create({
customer: customerId,
items: [{ price: meteredPriceId }]
});Reporting Usage
As customers consume the product, report usage against the subscription item. Use action: increment to add to the running total.
await stripe.subscriptionItems.createUsageRecord(itemId, {
quantity: 1000,
timestamp: Math.floor(Date.now() / 1000),
action: 'increment'
});When to Report
Report at the moment usage happens — for example, after each AI completion count its tokens. Buffer high-frequency events and flush periodically to avoid hitting API limits.
async function onAiCall(itemId, tokens) {
await reportUsage(itemId, tokens);
}Idempotent Reporting
Avoid double-counting on retries by sending an idempotency key. Stripe ignores duplicate requests with the same key.
await stripe.subscriptionItems.createUsageRecord(
itemId,
{ quantity: 1000, action: 'increment' },
{ idempotencyKey: 'usage-' + eventId }
);Tiered Pricing
You can combine metering with tiers: the first 10,000 units cost more per unit, additional units less. Stripe computes the blended total automatically.
recurring: { usage_type: 'metered' },
billing_scheme: 'tiered',
tiers_mode: 'graduated'Showing Usage to Customers
Customers want transparency. Fetch the current period's usage and display it in their dashboard so there are no billing surprises.
const summaries = await stripe.subscriptionItems.listUsageRecordSummaries(itemId);Combining Plans
Many SaaS use a hybrid model: a fixed monthly base fee (licensed) plus metered overage. Add both prices as separate items on one subscription.
items: [
{ price: baseFeePriceId },
{ price: meteredPriceId }
]Best Practices
Run metered billing reliably:
- Use metered prices for consumption-based products
- Report usage with increment and idempotency keys
- Consider tiered pricing for volume
- Show usage to customers for transparency
Quick Check
Test your metered billing knowledge.
Recap
You added usage-based billing:
- Create
meteredprices and subscribe without a quantity - Report usage with
incrementand idempotency keys - Use tiered pricing and hybrid base-plus-overage models
- Surface usage to customers for transparency
Your AI SaaS now charges fairly for what users consume.
常见问题解答
「计量计费与按用量定价」课时是免费的吗?
是的 — 「计量计费与按用量定价」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课程的其余内容,请升级到 CoddyKit PRO。 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课程共包含 4 节课。
「计量计费与按用量定价」这节课中我会学到什么?
通过向 Stripe 报告用量、配置计量价格并构建基于用量的层级方案,按照客户的实际使用量收费。 你通过在浏览器中直接运行的动手代码来练习 AI Powered SaaS: Stripe + Auth + Billing + Deploy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 AI Powered SaaS: Stripe + Auth + Billing + Deploy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「计量计费与按用量定价」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课中编写并运行代码吗?
能。每节 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 订阅管理
- 处理 Stripe Webhook
- 客户门户与账单历史
- 计量计费与按用量定价