Loading...
Loading...
Use when billing for AI model token usage — setting up @commet/ai-sdk tracked() middleware, configuring balance consumption model plans with AI model pricing, tracking input/output/cache tokens, cost calculation with margins, or building AI products that need usage-based billing.
npx skill4agent add commet-labs/commet-skills ai-billingtracked()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.tracked()inputTokensoutputTokenscacheReadTokenscacheWriteTokensYour 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) ---| |blockOnExhaustion = trueblockOnExhaustion = falseincludedBalanceinputCost = 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 + marginAmountMath.ceil()| # | 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 |
| 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 |
pricingMode: "ai_model"export COMMET_API_KEY=ck_xxxxxxxxxnpm install @commet/node @commet/ai-sdk| 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 | |