mirror of
https://github.com/RooVetGit/Roo-Code.git
synced 2026-08-28 05:27:24 +00:00
57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
import type { ModelInfo } from "@roo-code/types"
|
|
|
|
function calculateApiCostInternal(
|
|
modelInfo: ModelInfo,
|
|
inputTokens: number,
|
|
outputTokens: number,
|
|
cacheCreationInputTokens: number,
|
|
cacheReadInputTokens: number,
|
|
): number {
|
|
const cacheWritesCost = ((modelInfo.cacheWritesPrice || 0) / 1_000_000) * cacheCreationInputTokens
|
|
const cacheReadsCost = ((modelInfo.cacheReadsPrice || 0) / 1_000_000) * cacheReadInputTokens
|
|
const baseInputCost = ((modelInfo.inputPrice || 0) / 1_000_000) * inputTokens
|
|
const outputCost = ((modelInfo.outputPrice || 0) / 1_000_000) * outputTokens
|
|
const totalCost = cacheWritesCost + cacheReadsCost + baseInputCost + outputCost
|
|
return totalCost
|
|
}
|
|
|
|
// For Anthropic compliant usage, the input tokens count does NOT include the
|
|
// cached tokens.
|
|
export function calculateApiCostAnthropic(
|
|
modelInfo: ModelInfo,
|
|
inputTokens: number,
|
|
outputTokens: number,
|
|
cacheCreationInputTokens?: number,
|
|
cacheReadInputTokens?: number,
|
|
): number {
|
|
return calculateApiCostInternal(
|
|
modelInfo,
|
|
inputTokens,
|
|
outputTokens,
|
|
cacheCreationInputTokens || 0,
|
|
cacheReadInputTokens || 0,
|
|
)
|
|
}
|
|
|
|
// For OpenAI compliant usage, the input tokens count INCLUDES the cached tokens.
|
|
export function calculateApiCostOpenAI(
|
|
modelInfo: ModelInfo,
|
|
inputTokens: number,
|
|
outputTokens: number,
|
|
cacheCreationInputTokens?: number,
|
|
cacheReadInputTokens?: number,
|
|
): number {
|
|
const cacheCreationInputTokensNum = cacheCreationInputTokens || 0
|
|
const cacheReadInputTokensNum = cacheReadInputTokens || 0
|
|
const nonCachedInputTokens = Math.max(0, inputTokens - cacheCreationInputTokensNum - cacheReadInputTokensNum)
|
|
|
|
return calculateApiCostInternal(
|
|
modelInfo,
|
|
nonCachedInputTokens,
|
|
outputTokens,
|
|
cacheCreationInputTokensNum,
|
|
cacheReadInputTokensNum,
|
|
)
|
|
}
|
|
|
|
export const parseApiPrice = (price: any) => (price ? parseFloat(price) * 1_000_000 : undefined)
|