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 ( +