Skip to content

calculateCost

calculateCost(modelId, usage, customPricing?, fallback?): number

Calculates the USD cost for a model call based on token usage and pricing.

string

Model identifier (e.g., “anthropic/claude-sonnet-4-6”)

Usage

Token counts from the model response

Record<string, ModelPricing>

User-provided pricing overrides

ModelPricing = DEFAULT_FALLBACK_PRICING

Fallback pricing for unknown models

number

Cost in USD

const cost = calculateCost("anthropic/claude-sonnet-4-6", { inputTokens: 1000, outputTokens: 500 })
// cost ≈ 0.0105 ($3/1M * 1000 + $15/1M * 500)