diff --git a/gui/src/components/console/TotalUsage.tsx b/gui/src/components/console/TotalUsage.tsx index 41f3843ee..f5a9819cc 100644 --- a/gui/src/components/console/TotalUsage.tsx +++ b/gui/src/components/console/TotalUsage.tsx @@ -1,4 +1,5 @@ -import React, { useState } from "react"; +import { calculateRequestCost } from "core/llm/utils/calculateRequestCost"; +import React, { useMemo, useState } from "react"; import { LLMLog } from "../../hooks/useLLMLog"; import useTotalUsage from "../../hooks/useTotalUsage"; @@ -21,6 +22,203 @@ const StatCard: React.FC<{ label: string; value: number; color?: string }> = ({ ); +const CostChart: React.FC<{ llmLog: LLMLog }> = ({ llmLog }) => { + const chartData = useMemo(() => { + let cumulativeCost = 0; + const dataPoints: { interaction: number; cost: number }[] = []; + + llmLog.order.forEach((id, index) => { + const interaction = llmLog.interactions.get(id); + + // Calculate cost for this interaction if we have the necessary data + if ( + interaction?.end?.usage && + interaction.start?.provider && + interaction.start?.options?.model + ) { + const costBreakdown = calculateRequestCost( + interaction.start.provider, + interaction.start.options.model, + interaction.end.usage, + ); + if (costBreakdown) { + cumulativeCost += costBreakdown.cost; + } + } + + dataPoints.push({ interaction: index + 1, cost: cumulativeCost }); + }); + + return dataPoints; + }, [llmLog.order, llmLog.interactions]); + + if (chartData.length === 0 || chartData[chartData.length - 1].cost === 0) { + return null; + } + + const maxCost = Math.max(...chartData.map((d) => d.cost)); + const maxInteraction = chartData.length; + const chartWidth = 400; + const chartHeight = 200; + const padding = 40; + + const xScale = (interaction: number) => + ((interaction - 1) / (maxInteraction - 1)) * (chartWidth - 2 * padding) + + padding; + + const yScale = (cost: number) => + chartHeight - padding - (cost / maxCost) * (chartHeight - 2 * padding); + + const pathData = chartData + .map( + (d, i) => + `${i === 0 ? "M" : "L"} ${xScale(d.interaction)} ${yScale(d.cost)}`, + ) + .join(" "); + + return ( +
+

+ Cumulative Cost Over Interactions +

+
+ + {/* Grid lines */} + + + + + + + + {/* Chart area background */} + + + {/* Cost line */} + + + {/* Data points */} + {chartData.map((d, i) => ( + + ))} + + {/* Y-axis labels */} + {[0, 0.25, 0.5, 0.75, 1].map((ratio) => { + const cost = maxCost * ratio; + const y = yScale(cost); + return ( + + + + ${cost.toFixed(4)} + + + ); + })} + + {/* X-axis labels */} + {[0, 0.25, 0.5, 0.75, 1].map((ratio) => { + const interaction = Math.round(1 + ratio * (maxInteraction - 1)); + const x = xScale(interaction); + return ( + + + + {interaction} + + + ); + })} + + {/* Axis labels */} + + Interaction Number + + + Cumulative Cost ($) + + +
+
+ ); +}; + const TotalUsage: React.FC = ({ llmLog }) => { const totalUsage = useTotalUsage(llmLog); const [showCostBreakdown, setShowCostBreakdown] = useState(false); @@ -90,19 +288,22 @@ const TotalUsage: React.FC = ({ llmLog }) => { {totalUsage.totalCost > 0 && ( -
-
-
- Total Cost -
-
- ${totalUsage.totalCost.toFixed(6)} -
-
- {totalUsage.costBreakdowns.length} cost calculations + <> + +
+
+
+ Total Cost +
+
+ ${totalUsage.totalCost.toFixed(6)} +
+
+ {totalUsage.costBreakdowns.length} cost calculations +
-
+ )} {totalUsage.totalCacheWriteTokens > 0 && (