calculateCost
calculateCost(
modelId,usage,customPricing?,fallback?):number
Calculates the USD cost for a model call based on token usage and pricing.
Parameters
Section titled “Parameters”modelId
Section titled “modelId”string
Model identifier (e.g., “anthropic/claude-sonnet-4-6”)
Token counts from the model response
customPricing?
Section titled “customPricing?”Record<string, ModelPricing>
User-provided pricing overrides
fallback?
Section titled “fallback?”ModelPricing = DEFAULT_FALLBACK_PRICING
Fallback pricing for unknown models
Returns
Section titled “Returns”number
Cost in USD
Example
Section titled “Example”const cost = calculateCost("anthropic/claude-sonnet-4-6", { inputTokens: 1000, outputTokens: 500 })// cost ≈ 0.0105 ($3/1M * 1000 + $15/1M * 500)