ai-billing
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAI Billing
AI计费
Quick Start
快速开始
Wrap any Vercel AI SDK model with to automatically bill for token usage:
tracked()typescript
import { Commet } from "@commet/node";
import { tracked } from "@commet/ai-sdk";
import { anthropic } from "@ai-sdk/anthropic";
import { generateText } from "ai";
const commet = new Commet({ apiKey: process.env.COMMET_API_KEY! });
const result = await generateText({
model: tracked(anthropic("claude-sonnet-4-20250514"), {
commet,
feature: "ai_generation",
customerId: "user_123",
}),
prompt: "Explain quantum computing",
});
// Tokens tracked, cost calculated, balance deducted. Done.That's it. intercepts the model response, reports , , , and to Commet, which calculates cost from the AI model catalog and deducts from the customer's balance.
tracked()inputTokensoutputTokenscacheReadTokenscacheWriteTokens使用封装任意Vercel AI SDK模型,即可自动针对Token用量计费:
tracked()typescript
import { Commet } from "@commet/node";
import { tracked } from "@commet/ai-sdk";
import { anthropic } from "@ai-sdk/anthropic";
import { generateText } from "ai";
const commet = new Commet({ apiKey: process.env.COMMET_API_KEY! });
const result = await generateText({
model: tracked(anthropic("claude-sonnet-4-20250514"), {
commet,
feature: "ai_generation",
customerId: "user_123",
}),
prompt: "Explain quantum computing",
});
// Tokens tracked, cost calculated, balance deducted. Done.就这么简单。会拦截模型响应,将、、和上报到Commet,Commet会基于AI模型目录计算成本并从客户余额中扣除对应费用。
tracked()inputTokensoutputTokenscacheReadTokenscacheWriteTokensHow It Works
工作原理
Your app Commet AI Provider
| | |
|-- tracked(model) ------->| |
| | |
|-- generateText() --------|-------- API call ------------->|
| | |
|<-- tokens + response ----|<------- response --------------|
| | |
| |-- resolve model price |
| |-- apply margin |
| |-- deduct from balance |
| |-- record ledger entry |
| | |
|<-- result (unchanged) ---| |你的应用 Commet AI供应商
| | |
|-- tracked(model) ------->| |
| | |
|-- generateText() --------|-------- API调用 ------------->|
| | |
|<-- Token + 响应 ---------|<------- 响应 -----------------|
| | |
| |-- 匹配模型定价 |
| |-- 应用利润率 |
| |-- 扣除余额 |
| |-- 记录台账条目 |
| | |
|<-- 结果(无修改)--------| |Balance Model Overview
余额模式概述
AI billing uses the balance consumption model. Customers get a dollar balance (e.g., $10.00/month) that depletes as they use AI features. When the balance reaches zero, behavior depends on configuration:
- : API returns 402, customer must top up or wait for renewal
blockOnExhaustion = true - : Usage continues, overage charged at period end
blockOnExhaustion = false
Balance resets to at each billing period renewal.
includedBalanceAI计费采用余额消耗模式。客户会获得一笔美元余额(例如每月$10.00),随着使用AI功能逐步消耗。余额归零时的行为取决于配置:
- :API返回402,客户需要充值或等待周期续期
blockOnExhaustion = true - :可继续使用,超额部分在周期结束时统一收费
blockOnExhaustion = false
余额会在每个计费周期续期时重置为的数值。
includedBalanceCost Formula
成本计算公式
All values use rate scale (10000 = $1.00) for sub-cent precision:
inputCost = ceil(inputTokens x inputPricePerMillionTokens / 1,000,000)
outputCost = ceil(outputTokens x outputPricePerMillionTokens / 1,000,000)
subtotal = inputCost + outputCost + cacheReadCost + cacheWriteCost
marginAmount = ceil(subtotal x margin / 10000)
total = subtotal + marginAmountMargin is configured per feature per plan in basis points (2000 = 20% markup). All costs use to prevent underbilling.
Math.ceil()所有数值采用费率刻度(10000 = $1.00)以保证亚分精度:
inputCost = ceil(inputTokens x inputPricePerMillionTokens / 1,000,000)
outputCost = ceil(outputTokens x outputPricePerMillionTokens / 1,000,000)
subtotal = inputCost + outputCost + cacheReadCost + cacheWriteCost
marginAmount = ceil(subtotal x margin / 10000)
total = subtotal + marginAmount利润率按每个套餐的每个功能以基点配置(2000 = 20%溢价)。所有成本采用计算以避免少计费。
Math.ceil()Key Gotchas
关键注意事项
| # | Gotcha | Detail |
|---|---|---|
| 1 | Feature must be | Configure this in the Commet dashboard before tracking works |
| 2 | Model must exist in the catalog | Commet syncs models from AI Gateway automatically, but verify your model is listed |
| 3 | Tracking is non-blocking | If token reporting fails, the AI response still returns. Use |
| 4 | Balance deduction is real-time | Cost is deducted immediately, not at period end |
| 5 | Rate scale, not cents | Token prices and costs use rate scale (10000 = $1.00), not settlement scale (100 = $1.00) |
| 6 | Margin is per feature per plan | Different plans can have different markups on the same AI feature |
| # | 注意事项 | 详情 |
|---|---|---|
| 1 | 功能必须配置 | 跟踪功能生效前需要在Commet控制台完成该配置 |
| 2 | 模型必须存在于目录中 | Commet会自动从AI网关同步模型,但请确认你使用的模型在列表中 |
| 3 | 跟踪逻辑是非阻塞的 | 如果Token上报失败,AI响应仍会正常返回。可使用 |
| 4 | 余额扣减是实时的 | 成本会立即扣除,而非在周期结束时结算 |
| 5 | 采用费率刻度,而非美分 | Token定价和成本使用费率刻度(10000 = $1.00),而非结算刻度(100 = $1.00) |
| 6 | 利润率按套餐下的功能单独配置 | 同一个AI功能在不同套餐中可以设置不同的溢价 |
What Do You Need?
相关任务指引
| Task | Reference |
|---|---|
| Set up tracked() middleware | tracked-middleware.md -- installation, streaming, multiple models, error handling |
| Understand cost calculation | cost-calculation.md -- AI model catalog, pricing, margins, multi-currency |
| Configure balance model | balance-model.md -- how balance works, exhaustion, top-ups, choosing the right model |
| 任务 | 参考文档 |
|---|---|
| 搭建tracked()中间件 | tracked-middleware.md -- 安装、流式处理、多模型、错误处理 |
| 了解成本计算逻辑 | cost-calculation.md -- AI模型目录、定价、利润率、多币种 |
| 配置余额模式 | balance-model.md -- 余额规则、用尽处理、充值、选择合适的模式 |
Prerequisites
前提条件
- Feature configured with in the Commet dashboard
pricingMode: "ai_model" - Plan using the balance consumption model
- Margin configured on the plan feature (0 basis points = pass-through cost)
- Customer with an active subscription to that plan
- 在Commet控制台中已将功能配置为
pricingMode: "ai_model" - 套餐采用余额消耗模式
- 套餐功能下已配置利润率(0基点 = 成本价透传)
- 客户已订阅该套餐且订阅有效
Common Setup
通用配置
API Key
API Key
Store in environment variable:
bash
export COMMET_API_KEY=ck_xxxxxxxxx存储在环境变量中:
bash
export COMMET_API_KEY=ck_xxxxxxxxxInstall
安装
bash
npm install @commet/node @commet/ai-sdkbash
npm install @commet/node @commet/ai-sdkError Handling Quick Reference
错误处理快速参考
| Code | Condition | Action |
|---|---|---|
| 402 | Insufficient balance | Customer needs to top up or upgrade plan |
| 403 | Feature not in plan | Check customer's subscription includes the AI feature |
| 404 | Model not in catalog | Verify the model ID matches the catalog (e.g., |
| 422 | Missing required fields | |
| 错误码 | 触发条件 | 处理方案 |
|---|---|---|
| 402 | 余额不足 | 客户需要充值或升级套餐 |
| 403 | 套餐不包含该功能 | 检查客户的订阅是否包含该AI功能 |
| 404 | 模型不在目录中 | 确认模型ID与目录一致(例如 |
| 422 | 缺少必填字段 | 提供 |