Price a SaaS feature before execution
Translate a representative workload into exact COGS, credits, and margin guardrails without floating-point money errors.
What you will build
A server-side preflight that estimates the cost of one product action, converts the quote to integer micro-USD, and decides how many internal credits to reserve before running the model.
1. Model representative usage
Use percentiles from production traces, not a context-window maximum or a provider’s headline price. Keep distinct profiles for features whose input/output mix differs.
TypeScript
const usageProfiles = {
summarizeDocument: {
inputTokens: 80_000,
cachedInputTokens: 0,
outputTokens: 2_500,
},
regenerateSummary: {
inputTokens: 80_000,
cachedInputTokens: 72_000,
outputTokens: 2_500,
},
} as const;2. Quote the deployment-specific COGS
TypeScript
const quote = await allm.pricing.calculate({
deployment: "dep_openai_gpt_5_1_responses_global",
...usageProfiles.summarizeDocument,
});
console.log(quote.totalUsd); // exact decimal string
console.table(quote.lineItems);3. Apply margin using integer money
TypeScript
function usdToMicros(value: string): bigint {
const [whole, fraction = ""] = value.split(".");
return BigInt(whole) * 1_000_000n +
BigInt(fraction.padEnd(6, "0").slice(0, 6));
}
function microsToUsd(value: bigint): string {
const whole = value / 1_000_000n;
const fraction = (value % 1_000_000n).toString().padStart(6, "0");
return `${whole}.${fraction}`;
}
const costMicros = usdToMicros(quote.totalUsd);
const priceMicros = (costMicros * 250n + 99n) / 100n; // 2.5× markup
const credits = (priceMicros + 9_999n) / 10_000n; // $0.01 per credit
await creditLedger.reserve({
customerId,
feature: "summarize_document",
credits,
});Example output
JSON
{
"feature": "summarize_document",
"deployment_id": "dep_openai_gpt_5_1_responses_global",
"estimated_cost_usd": "0.125000",
"customer_price_usd": "0.312500",
"credits_reserved": "32",
"usage_profile": "p75_2026_07"
}Production guardrails
- Reserve using estimated usage, then settle with actual input, cached input, and output tokens.
- Keep retries as separate cost events; do not pretend failed paid calls were free.
- Version usage profiles and pricing policies so historical margins remain explainable.
- Reject or require confirmation when estimated cost exceeds a feature budget.
- Use private price books for negotiated rates instead of modifying public catalog data.