| 1 | import { calculateRequestCost } from "core/llm/utils/calculateRequestCost"; |
| 2 | import React, { useMemo, useState } from "react"; |
| 3 | import { LLMLog } from "../../hooks/useLLMLog"; |
| 4 | import useTotalUsage from "../../hooks/useTotalUsage"; |
| 5 | |
| 6 | interface TotalUsageProps { |
| 7 | llmLog: LLMLog; |
| 8 | } |
| 9 | |
| 10 | const StatCard: React.FC<{ label: string; value: number; color?: string }> = ({ |
| 11 | label, |
| 12 | value, |
| 13 | color = "var(--vscode-foreground)", |
| 14 | }) => ( |
| 15 | <div className="flex flex-col items-center rounded-lg border border-[color:var(--vscode-panel-border)] bg-[color:var(--vscode-editor-background)] p-3"> |
| 16 | <div className="text-sm text-[color:var(--vscode-descriptionForeground)]"> |
| 17 | {label} |
| 18 | </div> |
| 19 | <div className="text-lg font-semibold" style={{ color }}> |
| 20 | {value.toLocaleString()} |
| 21 | </div> |
| 22 | </div> |
| 23 | ); |
| 24 | |
| 25 | const CostChart: React.FC<{ llmLog: LLMLog }> = ({ llmLog }) => { |
| 26 | const chartData = useMemo(() => { |
| 27 | let cumulativeCost = 0; |
| 28 | const dataPoints: { interaction: number; cost: number }[] = []; |
| 29 | |
| 30 | llmLog.order.forEach((id, index) => { |
| 31 | const interaction = llmLog.interactions.get(id); |
| 32 | |
| 33 | // Calculate cost for this interaction if we have the necessary data |
| 34 | if ( |
| 35 | interaction?.end?.usage && |
| 36 | interaction.start?.provider && |
| 37 | interaction.start?.options?.model |
| 38 | ) { |
| 39 | const costBreakdown = calculateRequestCost( |
| 40 | interaction.start.provider, |
| 41 | interaction.start.options.model, |
| 42 | interaction.end.usage, |
| 43 | ); |
| 44 | if (costBreakdown) { |
| 45 | cumulativeCost += costBreakdown.cost; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | dataPoints.push({ interaction: index + 1, cost: cumulativeCost }); |
| 50 | }); |
| 51 | |
| 52 | return dataPoints; |
| 53 | }, [llmLog.order, llmLog.interactions]); |
| 54 | |
| 55 | if (chartData.length === 0 || chartData[chartData.length - 1].cost === 0) { |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | const maxCost = Math.max(...chartData.map((d) => d.cost)); |
| 60 | const maxInteraction = chartData.length; |
| 61 | const chartWidth = 400; |
| 62 | const chartHeight = 200; |
| 63 | const padding = 40; |
| 64 | |
| 65 | const xScale = (interaction: number) => |
| 66 | ((interaction - 1) / (maxInteraction - 1)) * (chartWidth - 2 * padding) + |
| 67 | padding; |
| 68 | |
| 69 | const yScale = (cost: number) => |
| 70 | chartHeight - padding - (cost / maxCost) * (chartHeight - 2 * padding); |
| 71 | |
| 72 | const pathData = chartData |
| 73 | .map( |
| 74 | (d, i) => |
| 75 | `${i === 0 ? "M" : "L"} ${xScale(d.interaction)} ${yScale(d.cost)}`, |
| 76 | ) |
| 77 | .join(" "); |
| 78 | |
| 79 | return ( |
| 80 | <div className="mb-4 rounded-lg border border-[color:var(--vscode-panel-border)] bg-[color:var(--vscode-editor-background)] p-4"> |
| 81 | <h3 className="mb-3 text-sm font-semibold text-[color:var(--vscode-foreground)]"> |
| 82 | Cumulative Cost Over Interactions |
| 83 | </h3> |
| 84 | <div className="flex justify-center"> |
| 85 | <svg |
| 86 | width={chartWidth} |
| 87 | height={chartHeight} |
| 88 | className="overflow-visible" |
| 89 | > |
| 90 | {/* Grid lines */} |
| 91 | <defs> |
| 92 | <pattern |
| 93 | id="grid" |
| 94 | width="20" |
| 95 | height="20" |
| 96 | patternUnits="userSpaceOnUse" |
| 97 | > |
| 98 | <path |
| 99 | d="M 20 0 L 0 0 0 20" |
| 100 | fill="none" |
| 101 | stroke="var(--vscode-panel-border)" |
| 102 | strokeWidth="0.5" |
| 103 | opacity="0.3" |
| 104 | /> |
| 105 | </pattern> |
| 106 | </defs> |
| 107 | <rect width={chartWidth} height={chartHeight} fill="url(#grid)" /> |
| 108 | |
| 109 | {/* Chart area background */} |
| 110 | <rect |
| 111 | x={padding} |
| 112 | y={padding} |
| 113 | width={chartWidth - 2 * padding} |
| 114 | height={chartHeight - 2 * padding} |
| 115 | fill="var(--vscode-editor-background)" |
| 116 | stroke="var(--vscode-panel-border)" |
| 117 | strokeWidth="1" |
| 118 | /> |
| 119 | |
| 120 | {/* Cost line */} |
| 121 | <path |
| 122 | d={pathData} |
| 123 | fill="none" |
| 124 | stroke="var(--vscode-charts-green)" |
| 125 | strokeWidth="2" |
| 126 | strokeLinecap="round" |
| 127 | /> |
| 128 | |
| 129 | {/* Data points */} |
| 130 | {chartData.map((d, i) => ( |
| 131 | <circle |
| 132 | key={i} |
| 133 | cx={xScale(d.interaction)} |
| 134 | cy={yScale(d.cost)} |
| 135 | r="3" |
| 136 | fill="var(--vscode-charts-green)" |
| 137 | stroke="var(--vscode-editor-background)" |
| 138 | strokeWidth="1" |
| 139 | /> |
| 140 | ))} |
| 141 | |
| 142 | {/* Y-axis labels */} |
| 143 | {[0, 0.25, 0.5, 0.75, 1].map((ratio) => { |
| 144 | const cost = maxCost * ratio; |
| 145 | const y = yScale(cost); |
| 146 | return ( |
| 147 | <g key={ratio}> |
| 148 | <line |
| 149 | x1={padding - 5} |
| 150 | y1={y} |
| 151 | x2={padding} |
| 152 | y2={y} |
| 153 | stroke="var(--vscode-foreground)" |
| 154 | strokeWidth="1" |
| 155 | /> |
| 156 | <text |
| 157 | x={padding - 8} |
| 158 | y={y + 4} |
| 159 | textAnchor="end" |
| 160 | fontSize="10" |
| 161 | fill="var(--vscode-descriptionForeground)" |
| 162 | > |
| 163 | ${cost.toFixed(4)} |
| 164 | </text> |
| 165 | </g> |
| 166 | ); |
| 167 | })} |
| 168 | |
| 169 | {/* X-axis labels */} |
| 170 | {[0, 0.25, 0.5, 0.75, 1].map((ratio) => { |
| 171 | const interaction = Math.round(1 + ratio * (maxInteraction - 1)); |
| 172 | const x = xScale(interaction); |
| 173 | return ( |
| 174 | <g key={ratio}> |
| 175 | <line |
| 176 | x1={x} |
| 177 | y1={chartHeight - padding} |
| 178 | x2={x} |
| 179 | y2={chartHeight - padding + 5} |
| 180 | stroke="var(--vscode-foreground)" |
| 181 | strokeWidth="1" |
| 182 | /> |
| 183 | <text |
| 184 | x={x} |
| 185 | y={chartHeight - padding + 18} |
| 186 | textAnchor="middle" |
| 187 | fontSize="10" |
| 188 | fill="var(--vscode-descriptionForeground)" |
| 189 | > |
| 190 | {interaction} |
| 191 | </text> |
| 192 | </g> |
| 193 | ); |
| 194 | })} |
| 195 | |
| 196 | {/* Axis labels */} |
| 197 | <text |
| 198 | x={chartWidth / 2} |
| 199 | y={chartHeight - 5} |
| 200 | textAnchor="middle" |
| 201 | fontSize="12" |
| 202 | fill="var(--vscode-foreground)" |
| 203 | > |
| 204 | Interaction Number |
| 205 | </text> |
| 206 | <text |
| 207 | x={15} |
| 208 | y={chartHeight / 2} |
| 209 | textAnchor="middle" |
| 210 | fontSize="12" |
| 211 | fill="var(--vscode-foreground)" |
| 212 | transform={`rotate(-90 15 ${chartHeight / 2})`} |
| 213 | > |
| 214 | Cumulative Cost ($) |
| 215 | </text> |
| 216 | </svg> |
| 217 | </div> |
| 218 | </div> |
| 219 | ); |
| 220 | }; |
| 221 | |
| 222 | const TotalUsage: React.FC<TotalUsageProps> = ({ llmLog }) => { |
| 223 | const totalUsage = useTotalUsage(llmLog); |
| 224 | const [showCostBreakdown, setShowCostBreakdown] = useState(false); |
| 225 | |
| 226 | return ( |
| 227 | <div className="border-b border-[color:var(--vscode-panel-border)] p-4"> |
| 228 | <div className="mb-4 flex items-center justify-between"> |
| 229 | <h2 className="text-lg font-semibold text-[color:var(--vscode-foreground)]"> |
| 230 | Total Usage Summary |
| 231 | </h2> |
| 232 | <div className="flex items-center gap-4"> |
| 233 | {totalUsage.totalCost > 0 && ( |
| 234 | <button |
| 235 | onClick={() => setShowCostBreakdown(!showCostBreakdown)} |
| 236 | className="cursor-pointer text-lg font-semibold text-[color:var(--vscode-charts-green)] hover:text-[color:var(--vscode-charts-blue)]" |
| 237 | > |
| 238 | ${totalUsage.totalCost.toFixed(6)} |
| 239 | </button> |
| 240 | )} |
| 241 | <div className="text-sm text-[color:var(--vscode-descriptionForeground)]"> |
| 242 | {totalUsage.totalInteractions} interactions |
| 243 | </div> |
| 244 | </div> |
| 245 | </div> |
| 246 | |
| 247 | {showCostBreakdown && totalUsage.costBreakdowns.length > 0 && ( |
| 248 | <div className="mb-4 max-h-32 overflow-y-auto rounded border border-[color:var(--vscode-panel-border)] bg-[color:var(--vscode-editor-background)] p-3"> |
| 249 | <h3 className="mb-2 text-sm font-semibold text-[color:var(--vscode-foreground)]"> |
| 250 | Cost Breakdown |
| 251 | </h3> |
| 252 | <div className="space-y-2 text-xs"> |
| 253 | {totalUsage.costBreakdowns.map((breakdown, index) => ( |
| 254 | <div |
| 255 | key={index} |
| 256 | className="border-b border-[color:var(--vscode-panel-border)] pb-2 last:border-b-0" |
| 257 | > |
| 258 | <pre className="whitespace-pre-wrap text-[color:var(--vscode-descriptionForeground)]"> |
| 259 | {breakdown.breakdown} |
| 260 | </pre> |
| 261 | </div> |
| 262 | ))} |
| 263 | </div> |
| 264 | </div> |
| 265 | )} |
| 266 | |
| 267 | <div className="mb-4 grid grid-cols-2 gap-3 md:grid-cols-4"> |
| 268 | <StatCard |
| 269 | label="Input Tokens" |
| 270 | value={totalUsage.totalPromptTokens} |
| 271 | color="var(--vscode-charts-blue)" |
| 272 | /> |
| 273 | <StatCard |
| 274 | label="Output Tokens" |
| 275 | value={totalUsage.totalGeneratedTokens} |
| 276 | color="var(--vscode-charts-green)" |
| 277 | /> |
| 278 | <StatCard |
| 279 | label="Thinking Tokens" |
| 280 | value={totalUsage.totalThinkingTokens} |
| 281 | color="var(--vscode-charts-purple)" |
| 282 | /> |
| 283 | <StatCard |
| 284 | label="Cache Read" |
| 285 | value={totalUsage.totalCachedTokens} |
| 286 | color="var(--vscode-charts-orange)" |
| 287 | /> |
| 288 | </div> |
| 289 | |
| 290 | {totalUsage.totalCost > 0 && ( |
| 291 | <> |
| 292 | <CostChart llmLog={llmLog} /> |
| 293 | <div className="mb-4"> |
| 294 | <div className="flex flex-col items-center rounded-lg border border-[color:var(--vscode-panel-border)] bg-[color:var(--vscode-editor-background)] p-3"> |
| 295 | <div className="text-sm text-[color:var(--vscode-descriptionForeground)]"> |
| 296 | Total Cost |
| 297 | </div> |
| 298 | <div className="text-2xl font-bold text-[color:var(--vscode-charts-green)]"> |
| 299 | ${totalUsage.totalCost.toFixed(6)} |
| 300 | </div> |
| 301 | <div className="text-xs text-[color:var(--vscode-descriptionForeground)]"> |
| 302 | {totalUsage.costBreakdowns.length} cost calculations |
| 303 | </div> |
| 304 | </div> |
| 305 | </div> |
| 306 | </> |
| 307 | )} |
| 308 | |
| 309 | {totalUsage.totalCacheWriteTokens > 0 && ( |
| 310 | <div className="mb-4 grid grid-cols-1 gap-3 md:grid-cols-2"> |
| 311 | <StatCard |
| 312 | label="Cache Write" |
| 313 | value={totalUsage.totalCacheWriteTokens} |
| 314 | color="var(--vscode-charts-red)" |
| 315 | /> |
| 316 | <div className="flex items-center justify-center rounded-lg border border-[color:var(--vscode-panel-border)] bg-[color:var(--vscode-editor-background)] p-3"> |
| 317 | <div className="text-sm text-[color:var(--vscode-descriptionForeground)]"> |
| 318 | Cache Hit Rate:{" "} |
| 319 | {totalUsage.totalPromptTokens > 0 |
| 320 | ? ( |
| 321 | (totalUsage.totalCachedTokens / |
| 322 | totalUsage.totalPromptTokens) * |
| 323 | 100 |
| 324 | ).toFixed(1) |
| 325 | : "0"} |
| 326 | % |
| 327 | </div> |
| 328 | </div> |
| 329 | </div> |
| 330 | )} |
| 331 | |
| 332 | <div className="grid grid-cols-3 gap-3 text-sm"> |
| 333 | <div className="rounded border border-[color:var(--vscode-panel-border)] bg-[color:var(--vscode-editor-background)] p-2 text-center"> |
| 334 | <div className="text-[color:var(--vscode-charts-green)]"> |
| 335 | {totalUsage.totalSuccessfulInteractions} |
| 336 | </div> |
| 337 | <div className="text-[color:var(--vscode-descriptionForeground)]"> |
| 338 | Success |
| 339 | </div> |
| 340 | </div> |
| 341 | <div className="rounded border border-[color:var(--vscode-panel-border)] bg-[color:var(--vscode-editor-background)] p-2 text-center"> |
| 342 | <div className="text-[color:var(--vscode-charts-red)]"> |
| 343 | {totalUsage.totalErrorInteractions} |
| 344 | </div> |
| 345 | <div className="text-[color:var(--vscode-descriptionForeground)]"> |
| 346 | Error |
| 347 | </div> |
| 348 | </div> |
| 349 | <div className="rounded border border-[color:var(--vscode-panel-border)] bg-[color:var(--vscode-editor-background)] p-2 text-center"> |
| 350 | <div className="text-[color:var(--vscode-charts-yellow)]"> |
| 351 | {totalUsage.totalCancelledInteractions} |
| 352 | </div> |
| 353 | <div className="text-[color:var(--vscode-descriptionForeground)]"> |
| 354 | Cancelled |
| 355 | </div> |
| 356 | </div> |
| 357 | </div> |
| 358 | </div> |
| 359 | ); |
| 360 | }; |
| 361 | |
| 362 | export default TotalUsage; |