From 2551a057f0ebbdf7bc03a494fdf5bcda0cbd075c Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Sun, 20 Sep 2026 09:39:20 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=B5=20feat:=20Show=20Cost=20per=20Reco?= =?UTF-8?q?rd,=20Step=20and=20Response=20in=20the=20Trace=20Ledger=20(#161?= =?UTF-8?q?20)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 💵 feat: Show Cost per Record, Step and Response in the Trace Ledger * 💵 fix: Cost for Whole Responses Only, Hidden Records Included, and Said Aloud * 💵 fix: Give the Oldest Loaded Response No Cost While Older Records Remain --- client/src/components/Chat/Trace/Ledger.tsx | 39 ++++++-- client/src/components/Chat/Trace/Viewer.tsx | 5 +- .../Chat/Trace/__tests__/Viewer.test.tsx | 26 ++++- .../Chat/Trace/__tests__/model.test.ts | 97 +++++++++++++++++++ client/src/components/Chat/Trace/model.ts | 76 ++++++++++++--- 5 files changed, 222 insertions(+), 21 deletions(-) diff --git a/client/src/components/Chat/Trace/Ledger.tsx b/client/src/components/Chat/Trace/Ledger.tsx index 2d6c199bea..6e27b0cb45 100644 --- a/client/src/components/Chat/Trace/Ledger.tsx +++ b/client/src/components/Chat/Trace/Ledger.tsx @@ -16,9 +16,9 @@ import type { RecordPresentation } from './present'; import StackedToolIcons from '~/components/Chat/Messages/Content/ToolOutput/StackedToolIcons'; import { spanOf, stepSpan, turnSpan, turnKey, boundsOf, isLabelRecord } from './model'; import { useTraceFormat, recordDurationText } from './format'; +import { formatCost, formatTokens } from '~/utils/tokens'; import { appearanceOf, STATUS_LABEL } from './kinds'; import { cn, renderAgentAvatar } from '~/utils'; -import { formatTokens } from '~/utils/tokens'; import { useRowWindow } from './virtual'; import { useLocalize } from '~/hooks'; @@ -32,6 +32,9 @@ const ACTIVE_RING = 'group-focus-visible/tree:ring-2 group-focus-visible/tree:ring-inset group-focus-visible/tree:ring-ring-primary'; const GRID = 'grid grid-cols-[minmax(0,1fr)_4.5rem_minmax(5rem,1fr)] md:grid-cols-[minmax(14rem,2fr)_5rem_4.5rem_minmax(10rem,3fr)]'; +/** With a cost column beside the tokens; both are wide-screen columns. */ +const GRID_WITH_COST = + 'grid grid-cols-[minmax(0,1fr)_4.5rem_minmax(5rem,1fr)] md:grid-cols-[minmax(14rem,2fr)_5rem_4.5rem_5rem_minmax(10rem,3fr)]'; type Domain = { start: number; span: number }; @@ -132,6 +135,8 @@ function Ledger({ unrecordedCalls, stepOffsets, mcpIconMap, + showCost, + currency, onSelect, onToggle, }: { @@ -152,12 +157,27 @@ function Ledger({ /** Steps of a response that ran before its first loaded one, by response. */ stepOffsets: ReadonlyMap; mcpIconMap: Map; + /** Whether the deployment shows cost, as the context usage gauge does. */ + showCost: boolean; + currency?: { code: string; rate: number }; onSelect: (id: string) => void; onToggle: (key: string) => void; }) { const localize = useLocalize(); const format = useTraceFormat(); const idPrefix = useId(); + const grid = showCost ? GRID_WITH_COST : GRID; + /** The rows name themselves with `aria-label`, so what a column shows has to be said there too. */ + const costText = (cost?: number) => + showCost && cost != null + ? `, ${localize('com_ui_trace_summary_cost')} ${formatCost(cost, currency)}` + : ''; + const costCell = (cost?: number) => + showCost && ( + + {cost != null ? formatCost(cost, currency) : ''} + + ); const scrollRef = treeRef; const [activeKey, setActiveKey] = useState(null); const { first, last } = useRowWindow(scrollRef, rows.length, ROW_HEIGHT, { @@ -344,11 +364,11 @@ function Ledger({ aria-level={row.level} aria-expanded={row.expanded} aria-selected={false} - aria-label={`${label}, ${description}, ${localize('com_ui_trace_turn_records', { 0: String(recordCount) })}`} + aria-label={`${label}, ${description}, ${localize('com_ui_trace_turn_records', { 0: String(recordCount) })}${costText(row.type === 'turn' ? row.turn.cost : row.step.cost)}`} style={{ top: HEADER_HEIGHT + index * ROW_HEIGHT, height: ROW_HEIGHT }} onClick={() => activate(row)} className={cn( - GRID, + grid, 'absolute inset-x-0 cursor-pointer items-center gap-2 px-2 text-xs text-text-primary hover:bg-surface-hover', row.type === 'turn' ? 'border-t border-border-light bg-surface-primary-alt font-semibold' @@ -395,6 +415,7 @@ function Ledger({ )} + {costCell(row.type === 'turn' ? row.turn.cost : row.step.cost)} @@ -466,12 +487,12 @@ function Ledger({ aria-label={localize('com_ui_trace_bar_description', { 0: `${name}, ${localize(appearance.label)}`, 1: format.duration(node.start - (turnStart ?? model.start)), - 2: statusText, + 2: `${statusText}${costText(record.cost)}`, })} style={{ top: HEADER_HEIGHT + index * ROW_HEIGHT, height: ROW_HEIGHT }} onClick={() => activate(row)} className={cn( - GRID, + grid, 'absolute inset-x-0 cursor-pointer items-center gap-2 px-2 text-sm text-text-primary hover:bg-surface-hover', selectedId === row.key && 'bg-surface-active-alt', active && ACTIVE_RING, @@ -553,6 +574,7 @@ function Ledger({ {recordTokens(node)} + {costCell(record.cost)} @@ -587,13 +609,18 @@ function Ledger({