|
| 1 | +/** |
| 2 | + * Approximation-based token statistics for web workers |
| 3 | + * Avoids loading 8MB+ ai-tokenizer in the renderer |
| 4 | + * |
| 5 | + * Uses simple text.length/4 approximation which is ~90% accurate |
| 6 | + * Live token counts are ephemeral so approximation is acceptable |
| 7 | + */ |
| 8 | + |
| 9 | +import type { CmuxMessage } from "@/types/message"; |
| 10 | +import type { ChatStats, TokenConsumer } from "@/types/chatStats"; |
| 11 | +import type { LanguageModelV2Usage } from "@ai-sdk/provider"; |
| 12 | +import { getModelStats } from "./modelStats"; |
| 13 | +import type { ChatUsageDisplay } from "./usageAggregator"; |
| 14 | + |
| 15 | +// Simple approximation tokenizer to avoid loading 8MB ai-tokenizer package |
| 16 | +const approximateTokenizer = { |
| 17 | + encoding: 'approximate', |
| 18 | + countTokens: (text: string) => Math.ceil(text.length / 4), |
| 19 | +}; |
| 20 | + |
| 21 | +function approximateCountTokensForData(data: unknown): number { |
| 22 | + const serialized = JSON.stringify(data); |
| 23 | + return Math.ceil(serialized.length / 4); |
| 24 | +} |
| 25 | + |
| 26 | +function approximateToolDefinitionTokens(_toolName: string): number { |
| 27 | + // Rough average for tool definitions |
| 28 | + return 50; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Create a display-friendly usage object from AI SDK usage |
| 33 | + */ |
| 34 | +export function createDisplayUsage( |
| 35 | + usage: LanguageModelV2Usage | undefined, |
| 36 | + model: string, |
| 37 | + providerMetadata?: Record<string, unknown> |
| 38 | +): ChatUsageDisplay | undefined { |
| 39 | + if (!usage) return undefined; |
| 40 | + |
| 41 | + // Provider-specific token handling: |
| 42 | + // - OpenAI: inputTokens is INCLUSIVE of cachedInputTokens |
| 43 | + // - Anthropic: inputTokens EXCLUDES cachedInputTokens |
| 44 | + const cachedTokens = usage.cachedInputTokens ?? 0; |
| 45 | + const rawInputTokens = usage.inputTokens ?? 0; |
| 46 | + |
| 47 | + // Detect provider from model string |
| 48 | + const isOpenAI = model.startsWith("openai:"); |
| 49 | + |
| 50 | + // For OpenAI, subtract cached tokens to get uncached input tokens |
| 51 | + const inputTokens = isOpenAI ? Math.max(0, rawInputTokens - cachedTokens) : rawInputTokens; |
| 52 | + |
| 53 | + // Extract cache creation tokens from provider metadata (Anthropic-specific) |
| 54 | + const cacheCreateTokens = |
| 55 | + (providerMetadata?.anthropic as { cacheCreationInputTokens?: number } | undefined) |
| 56 | + ?.cacheCreationInputTokens ?? 0; |
| 57 | + |
| 58 | + // Calculate output tokens excluding reasoning |
| 59 | + const outputWithoutReasoning = Math.max( |
| 60 | + 0, |
| 61 | + (usage.outputTokens ?? 0) - (usage.reasoningTokens ?? 0) |
| 62 | + ); |
| 63 | + |
| 64 | + // Get model stats for cost calculation |
| 65 | + const modelStats = getModelStats(model); |
| 66 | + |
| 67 | + // Calculate costs based on model stats (undefined if model unknown) |
| 68 | + let inputCost: number | undefined; |
| 69 | + let cachedCost: number | undefined; |
| 70 | + let cacheCreateCost: number | undefined; |
| 71 | + let outputCost: number | undefined; |
| 72 | + let reasoningCost: number | undefined; |
| 73 | + |
| 74 | + if (modelStats) { |
| 75 | + inputCost = inputTokens * modelStats.input_cost_per_token; |
| 76 | + cachedCost = cachedTokens * (modelStats.cache_read_input_token_cost ?? 0); |
| 77 | + cacheCreateCost = cacheCreateTokens * (modelStats.cache_creation_input_token_cost ?? 0); |
| 78 | + outputCost = outputWithoutReasoning * modelStats.output_cost_per_token; |
| 79 | + reasoningCost = (usage.reasoningTokens ?? 0) * modelStats.output_cost_per_token; |
| 80 | + } |
| 81 | + |
| 82 | + return { |
| 83 | + input: { |
| 84 | + tokens: inputTokens, |
| 85 | + cost_usd: inputCost, |
| 86 | + }, |
| 87 | + cached: { |
| 88 | + tokens: cachedTokens, |
| 89 | + cost_usd: cachedCost, |
| 90 | + }, |
| 91 | + cacheCreate: { |
| 92 | + tokens: cacheCreateTokens, |
| 93 | + cost_usd: cacheCreateCost, |
| 94 | + }, |
| 95 | + output: { |
| 96 | + tokens: outputWithoutReasoning, |
| 97 | + cost_usd: outputCost, |
| 98 | + }, |
| 99 | + reasoning: { |
| 100 | + tokens: usage.reasoningTokens ?? 0, |
| 101 | + cost_usd: reasoningCost, |
| 102 | + }, |
| 103 | + }; |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Calculate token statistics from raw CmuxMessages |
| 108 | + * This is the single source of truth for token counting |
| 109 | + * |
| 110 | + * @param messages - Array of CmuxMessages from chat history |
| 111 | + * @param model - Model string (e.g., "anthropic:claude-opus-4-1") |
| 112 | + * @returns ChatStats with token breakdown by consumer and usage history |
| 113 | + */ |
| 114 | +export function calculateTokenStatsApproximate(messages: CmuxMessage[], model: string): ChatStats { |
| 115 | + if (messages.length === 0) { |
| 116 | + return { |
| 117 | + consumers: [], |
| 118 | + totalTokens: 0, |
| 119 | + model, |
| 120 | + tokenizerName: "No messages", |
| 121 | + usageHistory: [], |
| 122 | + }; |
| 123 | + } |
| 124 | + |
| 125 | + performance.mark("calculateTokenStatsStart"); |
| 126 | + |
| 127 | + const tokenizer = approximateTokenizer; |
| 128 | + const consumerMap = new Map<string, { fixed: number; variable: number }>(); |
| 129 | + const toolsWithDefinitions = new Set<string>(); // Track which tools have definitions included |
| 130 | + const usageHistory: ChatUsageDisplay[] = []; |
| 131 | + let systemMessageTokens = 0; // Accumulate system message tokens across all requests |
| 132 | + |
| 133 | + // Calculate tokens by content producer (User, Assistant, individual tools) |
| 134 | + // This shows what activities are consuming tokens, useful for debugging costs |
| 135 | + for (const message of messages) { |
| 136 | + if (message.role === "user") { |
| 137 | + // User message text |
| 138 | + let userTokens = 0; |
| 139 | + for (const part of message.parts) { |
| 140 | + if (part.type === "text") { |
| 141 | + userTokens += tokenizer.countTokens(part.text); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + const existing = consumerMap.get("User") ?? { fixed: 0, variable: 0 }; |
| 146 | + consumerMap.set("User", { fixed: 0, variable: existing.variable + userTokens }); |
| 147 | + } else if (message.role === "assistant") { |
| 148 | + // Accumulate system message tokens from this request |
| 149 | + if (message.metadata?.systemMessageTokens) { |
| 150 | + systemMessageTokens += message.metadata.systemMessageTokens; |
| 151 | + } |
| 152 | + |
| 153 | + // Store usage in history for comparison with estimates |
| 154 | + if (message.metadata?.usage) { |
| 155 | + const usage = createDisplayUsage( |
| 156 | + message.metadata.usage, |
| 157 | + message.metadata.model ?? model, // Use actual model from request, not UI model |
| 158 | + message.metadata.providerMetadata |
| 159 | + ); |
| 160 | + if (usage) { |
| 161 | + usageHistory.push(usage); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + // Count assistant text separately from tools |
| 166 | + // IMPORTANT: Batch tokenization by type to avoid calling tokenizer for each tiny part |
| 167 | + // (reasoning messages can have 600+ parts like "I", "'m", " thinking") |
| 168 | + |
| 169 | + // Group and concatenate parts by type |
| 170 | + const textParts = message.parts.filter((p) => p.type === "text"); |
| 171 | + const reasoningParts = message.parts.filter((p) => p.type === "reasoning"); |
| 172 | + |
| 173 | + // Tokenize text parts once (not per part!) |
| 174 | + if (textParts.length > 0) { |
| 175 | + const allText = textParts.map((p) => p.text).join(""); |
| 176 | + const textTokens = tokenizer.countTokens(allText); |
| 177 | + const existing = consumerMap.get("Assistant") ?? { fixed: 0, variable: 0 }; |
| 178 | + consumerMap.set("Assistant", { fixed: 0, variable: existing.variable + textTokens }); |
| 179 | + } |
| 180 | + |
| 181 | + // Tokenize reasoning parts once (not per part!) |
| 182 | + if (reasoningParts.length > 0) { |
| 183 | + const allReasoning = reasoningParts.map((p) => p.text).join(""); |
| 184 | + const reasoningTokens = tokenizer.countTokens(allReasoning); |
| 185 | + const existing = consumerMap.get("Reasoning") ?? { fixed: 0, variable: 0 }; |
| 186 | + consumerMap.set("Reasoning", { fixed: 0, variable: existing.variable + reasoningTokens }); |
| 187 | + } |
| 188 | + |
| 189 | + // Handle tool parts |
| 190 | + for (const part of message.parts) { |
| 191 | + if (part.type === "dynamic-tool") { |
| 192 | + // Count tool arguments |
| 193 | + const argsTokens = approximateCountTokensForData(part.input, tokenizer); |
| 194 | + |
| 195 | + // Count tool results if available |
| 196 | + // Tool results have nested structure: { type: "json", value: {...} } |
| 197 | + let resultTokens = 0; |
| 198 | + if (part.state === "output-available" && part.output) { |
| 199 | + // Extract the actual data from the nested output structure |
| 200 | + const outputData = |
| 201 | + typeof part.output === "object" && part.output !== null && "value" in part.output |
| 202 | + ? part.output.value |
| 203 | + : part.output; |
| 204 | + |
| 205 | + // Special handling for web_search encrypted content |
| 206 | + if (part.toolName === "web_search" && Array.isArray(outputData)) { |
| 207 | + // Check if this is encrypted web search results |
| 208 | + const hasEncryptedContent = outputData.some( |
| 209 | + (item: unknown): item is { encryptedContent: string } => |
| 210 | + item !== null && |
| 211 | + typeof item === "object" && |
| 212 | + "encryptedContent" in item && |
| 213 | + typeof (item as Record<string, unknown>).encryptedContent === "string" |
| 214 | + ); |
| 215 | + |
| 216 | + if (hasEncryptedContent) { |
| 217 | + // Calculate tokens for encrypted content with heuristic |
| 218 | + // Encrypted content is base64 encoded and then encrypted/compressed |
| 219 | + // Apply reduction factors: |
| 220 | + // 1. Remove base64 overhead (multiply by 0.75) |
| 221 | + // 2. Apply an estimated token reduction factor of 4 |
| 222 | + let encryptedChars = 0; |
| 223 | + for (const item of outputData) { |
| 224 | + if ( |
| 225 | + item !== null && |
| 226 | + typeof item === "object" && |
| 227 | + "encryptedContent" in item && |
| 228 | + typeof (item as Record<string, unknown>).encryptedContent === "string" |
| 229 | + ) { |
| 230 | + encryptedChars += (item as { encryptedContent: string }).encryptedContent |
| 231 | + .length; |
| 232 | + } |
| 233 | + } |
| 234 | + // Use heuristic: encrypted chars / 40 for token estimation |
| 235 | + resultTokens = Math.ceil(encryptedChars * 0.75); |
| 236 | + } else { |
| 237 | + // Normal web search results without encryption |
| 238 | + resultTokens = approximateCountTokensForData(outputData, tokenizer); |
| 239 | + } |
| 240 | + } else { |
| 241 | + // Normal tool results |
| 242 | + resultTokens = approximateCountTokensForData(outputData, tokenizer); |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + // Get existing or create new consumer for this tool |
| 247 | + const existing = consumerMap.get(part.toolName) ?? { fixed: 0, variable: 0 }; |
| 248 | + |
| 249 | + // Add tool definition tokens if this is the first time we see this tool |
| 250 | + let fixedTokens = existing.fixed; |
| 251 | + if (!toolsWithDefinitions.has(part.toolName)) { |
| 252 | + fixedTokens += approximateToolDefinitionTokens(part.toolName); |
| 253 | + toolsWithDefinitions.add(part.toolName); |
| 254 | + } |
| 255 | + |
| 256 | + // Add variable tokens (args + results) |
| 257 | + const variableTokens = existing.variable + argsTokens + resultTokens; |
| 258 | + |
| 259 | + consumerMap.set(part.toolName, { fixed: fixedTokens, variable: variableTokens }); |
| 260 | + } |
| 261 | + } |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + // Add system message tokens as a consumer if present |
| 266 | + if (systemMessageTokens > 0) { |
| 267 | + consumerMap.set("System", { fixed: 0, variable: systemMessageTokens }); |
| 268 | + } |
| 269 | + |
| 270 | + // Calculate total tokens |
| 271 | + const totalTokens = Array.from(consumerMap.values()).reduce( |
| 272 | + (sum, val) => sum + val.fixed + val.variable, |
| 273 | + 0 |
| 274 | + ); |
| 275 | + |
| 276 | + // Create sorted consumer array (descending by token count) |
| 277 | + const consumers: TokenConsumer[] = Array.from(consumerMap.entries()) |
| 278 | + .map(([name, counts]) => { |
| 279 | + const total = counts.fixed + counts.variable; |
| 280 | + return { |
| 281 | + name, |
| 282 | + tokens: total, |
| 283 | + percentage: totalTokens > 0 ? (total / totalTokens) * 100 : 0, |
| 284 | + fixedTokens: counts.fixed > 0 ? counts.fixed : undefined, |
| 285 | + variableTokens: counts.variable > 0 ? counts.variable : undefined, |
| 286 | + }; |
| 287 | + }) |
| 288 | + .sort((a, b) => b.tokens - a.tokens); |
| 289 | + |
| 290 | + return { |
| 291 | + consumers, |
| 292 | + totalTokens, |
| 293 | + model, |
| 294 | + tokenizerName: tokenizer.encoding, |
| 295 | + usageHistory, |
| 296 | + }; |
| 297 | +} |
0 commit comments