( modelInfo: ModelInfo, inputTokens: number, outputTokens: number, cacheCreationInputTokens?: number, cacheReadInputTokens?: number, serviceTier?: ServiceTier, )
| 90 | |
| 91 | // For OpenAI compliant usage, the input tokens count INCLUDES the cached tokens. |
| 92 | export function calculateApiCostOpenAI( |
| 93 | modelInfo: ModelInfo, |
| 94 | inputTokens: number, |
| 95 | outputTokens: number, |
| 96 | cacheCreationInputTokens?: number, |
| 97 | cacheReadInputTokens?: number, |
| 98 | serviceTier?: ServiceTier, |
| 99 | ): ApiCostResult { |
| 100 | const cacheCreationInputTokensNum = cacheCreationInputTokens || 0 |
| 101 | const cacheReadInputTokensNum = cacheReadInputTokens || 0 |
| 102 | const nonCachedInputTokens = Math.max(0, inputTokens - cacheCreationInputTokensNum - cacheReadInputTokensNum) |
| 103 | const effectiveModelInfo = applyLongContextPricing(modelInfo, inputTokens, serviceTier) |
| 104 | |
| 105 | // For OpenAI: inputTokens ALREADY includes all tokens (cached + non-cached) |
| 106 | // So we pass the original inputTokens as the total |
| 107 | return calculateApiCostInternal( |
| 108 | effectiveModelInfo, |
| 109 | nonCachedInputTokens, |
| 110 | outputTokens, |
| 111 | cacheCreationInputTokensNum, |
| 112 | cacheReadInputTokensNum, |
| 113 | inputTokens, |
| 114 | outputTokens, |
| 115 | ) |
| 116 | } |
| 117 | |
| 118 | export const parseApiPrice = (price: any) => (price ? parseFloat(price) * 1_000_000 : undefined) |
no test coverage detected