| 1 | import { Usage } from "../.."; |
| 2 | |
| 3 | export interface CostBreakdown { |
| 4 | cost: number; |
| 5 | breakdown: string; |
| 6 | } |
| 7 | |
| 8 | function calculateAnthropicCost( |
| 9 | model: string, |
| 10 | usage: Usage, |
| 11 | ): CostBreakdown | null { |
| 12 | // Normalize model name to handle various formats |
| 13 | const normalizedModel = model.toLowerCase(); |
| 14 | |
| 15 | // Define pricing per million tokens (MTok) |
| 16 | const pricing: Record< |
| 17 | string, |
| 18 | { input: number; output: number; cacheWrite: number; cacheRead: number } |
| 19 | > = { |
| 20 | // Claude Opus 4 (most intelligent model) |
| 21 | "claude-3-opus": { |
| 22 | input: 15, |
| 23 | output: 75, |
| 24 | cacheWrite: 18.75, |
| 25 | cacheRead: 1.5, |
| 26 | }, |
| 27 | "claude-3-opus-20240229": { |
| 28 | input: 15, |
| 29 | output: 75, |
| 30 | cacheWrite: 18.75, |
| 31 | cacheRead: 1.5, |
| 32 | }, |
| 33 | |
| 34 | // Claude Sonnet 4 (optimal balance) |
| 35 | "claude-3-5-sonnet": { |
| 36 | input: 3, |
| 37 | output: 15, |
| 38 | cacheWrite: 3.75, |
| 39 | cacheRead: 0.3, |
| 40 | }, |
| 41 | "claude-3-5-sonnet-20241022": { |
| 42 | input: 3, |
| 43 | output: 15, |
| 44 | cacheWrite: 3.75, |
| 45 | cacheRead: 0.3, |
| 46 | }, |
| 47 | "claude-3-5-sonnet-20240620": { |
| 48 | input: 3, |
| 49 | output: 15, |
| 50 | cacheWrite: 3.75, |
| 51 | cacheRead: 0.3, |
| 52 | }, |
| 53 | |
| 54 | // Claude Haiku 3.5 (fastest, most cost-effective) |
| 55 | "claude-3-5-haiku": { |
| 56 | input: 0.8, |
| 57 | output: 4, |
| 58 | cacheWrite: 1, |
| 59 | cacheRead: 0.08, |
| 60 | }, |
| 61 | "claude-3-5-haiku-20241022": { |
| 62 | input: 0.8, |
| 63 | output: 4, |
| 64 | cacheWrite: 1, |
| 65 | cacheRead: 0.08, |
| 66 | }, |
| 67 | |
| 68 | // Legacy Claude 3 Haiku |
| 69 | "claude-3-haiku": { |
| 70 | input: 0.25, |
| 71 | output: 1.25, |
| 72 | cacheWrite: 0.3, |
| 73 | cacheRead: 0.03, |
| 74 | }, |
| 75 | "claude-3-haiku-20240307": { |
| 76 | input: 0.25, |
| 77 | output: 1.25, |
| 78 | cacheWrite: 0.3, |
| 79 | cacheRead: 0.03, |
| 80 | }, |
| 81 | }; |
| 82 | |
| 83 | // Find matching pricing model |
| 84 | let modelPricing = pricing[normalizedModel]; |
| 85 | |
| 86 | // If exact match not found, try to find by partial match |
| 87 | if (!modelPricing) { |
| 88 | for (const [key, value] of Object.entries(pricing)) { |
| 89 | if (normalizedModel.includes(key) || key.includes(normalizedModel)) { |
| 90 | modelPricing = value; |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // If still no match, try common patterns |
| 97 | if (!modelPricing) { |
| 98 | if (normalizedModel.includes("opus")) { |
| 99 | modelPricing = pricing["claude-3-opus"]; |
| 100 | } else if (normalizedModel.includes("sonnet")) { |
| 101 | modelPricing = pricing["claude-3-5-sonnet"]; |
| 102 | } else if (normalizedModel.includes("haiku")) { |
| 103 | modelPricing = pricing["claude-3-5-haiku"]; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (!modelPricing) { |
| 108 | return null; // Unknown model |
| 109 | } |
| 110 | |
| 111 | // Calculate costs |
| 112 | const inputCost = (usage.promptTokens / 1_000_000) * modelPricing.input; |
| 113 | const outputCost = (usage.completionTokens / 1_000_000) * modelPricing.output; |
| 114 | |
| 115 | // Build breakdown components |
| 116 | const breakdownParts: string[] = []; |
| 117 | |
| 118 | // Input tokens breakdown |
| 119 | if (usage.promptTokens > 0) { |
| 120 | breakdownParts.push( |
| 121 | `Input: ${usage.promptTokens.toLocaleString()} tokens × $${modelPricing.input}/MTok = $${inputCost.toFixed(6)}`, |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | // Output tokens breakdown |
| 126 | if (usage.completionTokens > 0) { |
| 127 | breakdownParts.push( |
| 128 | `Output: ${usage.completionTokens.toLocaleString()} tokens × $${modelPricing.output}/MTok = $${outputCost.toFixed(6)}`, |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | // Handle prompt caching costs if available |
| 133 | let cacheCost = 0; |
| 134 | if (usage.promptTokensDetails) { |
| 135 | const { cachedTokens, cacheWriteTokens } = usage.promptTokensDetails; |
| 136 | |
| 137 | if (cacheWriteTokens && cacheWriteTokens > 0) { |
| 138 | const cacheWriteCost = |
| 139 | (cacheWriteTokens / 1_000_000) * modelPricing.cacheWrite; |
| 140 | cacheCost += cacheWriteCost; |
| 141 | breakdownParts.push( |
| 142 | `Cache Write: ${cacheWriteTokens.toLocaleString()} tokens × $${modelPricing.cacheWrite}/MTok = $${cacheWriteCost.toFixed(6)}`, |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | if (cachedTokens && cachedTokens > 0) { |
| 147 | const cacheReadCost = (cachedTokens / 1_000_000) * modelPricing.cacheRead; |
| 148 | cacheCost += cacheReadCost; |
| 149 | breakdownParts.push( |
| 150 | `Cache Read: ${cachedTokens.toLocaleString()} tokens × $${modelPricing.cacheRead}/MTok = $${cacheReadCost.toFixed(6)}`, |
| 151 | ); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | const totalCost = inputCost + outputCost + cacheCost; |
| 156 | |
| 157 | // Build final breakdown string |
| 158 | let breakdown = `Model: ${model}\n`; |
| 159 | breakdown += breakdownParts.join("\n"); |
| 160 | if (breakdownParts.length > 1) { |
| 161 | breakdown += `\nTotal: $${totalCost.toFixed(6)}`; |
| 162 | } |
| 163 | |
| 164 | return { |
| 165 | cost: totalCost, |
| 166 | breakdown, |
| 167 | }; |
| 168 | } |
| 169 | |
| 170 | export function calculateRequestCost( |
| 171 | provider: string, |
| 172 | model: string, |
| 173 | usage: Usage, |
| 174 | ): CostBreakdown | null { |
| 175 | switch (provider) { |
| 176 | case "anthropic": |
| 177 | return calculateAnthropicCost(model, usage); |
| 178 | default: |
| 179 | return null; |
| 180 | } |
| 181 | } |