Skip to main content

Function: calculateCost()

calculateCost(modelKey, inputTokens, outputTokens, cachedInputTokens?): object

Defined in: packages/workflow-engine/src/ai/model-helper.ts:317

Calculate costs based on token usage.

inputTokens is the total prompt size (the AI SDK's usage.inputTokens and OpenRouter's prompt_tokens both include cached tokens); cachedInputTokens is the part of it served from the prompt cache, billed at the model's cachedInputCostPerMillion when the catalogue has one and at the full input rate otherwise. outputTokens already includes reasoning tokens (both sources report them as part of the completion count), so reasoning is priced as output without any adjustment here.

Parameters​

modelKey​

ModelKey

inputTokens​

number

outputTokens​

number

cachedInputTokens?​

number = 0

Returns​

object

inputCost​

inputCost: number

outputCost​

outputCost: number

totalCost​

totalCost: number