Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/src/api/query-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const traceSpanSchema = z.object({
output: z.string().nullable(),
input_tokens: z.number().nullable(),
output_tokens: z.number().nullable(),
total_tokens: z.number().nullable().optional(),
model: z.string().nullable(),
provider: z.string().nullable(),
attributes: z.record(z.string(), z.union([z.string(), z.number()])),
Expand Down Expand Up @@ -176,6 +177,7 @@ export function mapTraceToSpans(traces: TraceSpan[], eventId: string): Span[] {
provider: t.provider,
input_tokens: t.input_tokens,
output_tokens: t.output_tokens,
total_tokens: t.total_tokens ?? null,
attributes: Object.keys(t.attributes).length > 0 ? JSON.stringify(t.attributes) : null,
};
});
Expand Down
1 change: 1 addition & 0 deletions app/src/api/saved-runs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const spanCacheSchema: z.ZodType<Span> = z.object({
provider: z.string().nullable(),
input_tokens: z.number().nullable(),
output_tokens: z.number().nullable(),
total_tokens: z.number().nullable().optional(),
attributes: z.string().nullable(),
normalized: z.custom<Span["normalized"]>().optional(),
});
Expand Down
4 changes: 4 additions & 0 deletions app/src/components/FlameTimeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ function SpanTooltip({
const type = spanTypeInfo(span);
const inTok = span.input_tokens ?? 0;
const outTok = span.output_tokens ?? 0;
const totalTok = span.total_tokens ?? 0;
const inputRaw = span.input_payload?.trim() ?? "";
const outputRaw = span.output_payload?.trim() ?? "";

Expand Down Expand Up @@ -132,6 +133,9 @@ function SpanTooltip({
{(inTok > 0 || outTok > 0) && (
<span style={{ color: C.fg1 }}>{inTok.toLocaleString()} in / {outTok.toLocaleString()} out</span>
)}
{inTok === 0 && outTok === 0 && totalTok > 0 && (
<span style={{ color: C.fg1 }}>{totalTok.toLocaleString()} total</span>
)}
</div>
<div className="flex flex-col flex-1 min-h-0 overflow-hidden">
{inputRaw ? <TooltipPayloadBlock label="Input" payload={span.input_payload!} /> : null}
Expand Down
10 changes: 7 additions & 3 deletions app/src/components/RunDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ function Badge({ label, copyValue }: { label: string; copyValue?: string }) {
}

function StatsLine({ stats, model, spans, active, startedAt }: {
stats: { spans: number; tools: number; llms: number; errors: number; dur: number; agents?: number; inTokens?: number; outTokens?: number };
stats: { spans: number; tools: number; llms: number; errors: number; dur: number; agents?: number; inTokens?: number; outTokens?: number; totalTokens?: number };
model?: string | null;
spans?: Span[];
active?: boolean;
Expand All @@ -231,6 +231,8 @@ function StatsLine({ stats, model, spans, active, startedAt }: {
const costRef = useRef<HTMLSpanElement>(null);
const inTok = stats.inTokens ?? 0;
const outTok = stats.outTokens ?? 0;
const totalTok = stats.totalTokens ?? 0;
const splitTok = inTok + outTok;

const [now, setNow] = useState(Date.now());
useEffect(() => {
Expand Down Expand Up @@ -261,7 +263,8 @@ function StatsLine({ stats, model, spans, active, startedAt }: {
{stats.errors > 0 && spans && <><ErrorsTooltip spans={spans} /><Dot /></>}
{stats.errors > 0 && !spans && <><span style={{ color: C.red }}><NumberFlow value={stats.errors} /> error{stats.errors !== 1 ? "s" : ""}</span><Dot /></>}
<Badge label="duration" /><span>{durMin > 0 ? <><NumberFlow value={durMin} />m <NumberFlow value={durRemSec} />s</> : <><NumberFlow value={durSec} />s</>}</span>
{(inTok > 0 || outTok > 0) && <><Dot /><Badge label="tokens" /><span><NumberFlow value={inTok} {...TOKEN_NUMBER_FLOW_TIMING} /> in / <NumberFlow value={outTok} {...TOKEN_NUMBER_FLOW_TIMING} /> out</span></>}
{(inTok > 0 || outTok > 0) && <><Dot /><Badge label="tokens" /><span><NumberFlow value={inTok} {...TOKEN_NUMBER_FLOW_TIMING} /> in / <NumberFlow value={outTok} {...TOKEN_NUMBER_FLOW_TIMING} /> out{totalTok > splitTok && <> / <NumberFlow value={totalTok} {...TOKEN_NUMBER_FLOW_TIMING} /> total</>}</span></>}
{splitTok === 0 && totalTok > 0 && <><Dot /><Badge label="tokens" /><span><NumberFlow value={totalTok} {...TOKEN_NUMBER_FLOW_TIMING} /> total</span></>}
{(() => {
const totalCost = breakdown.reduce((sum, b) => sum + (b.breakdown?.totalCost ?? 0), 0);
const cost = totalCost > 0 ? fmtCost(totalCost) : null;
Expand Down Expand Up @@ -449,7 +452,7 @@ function ViewHeader({
title: string;
model?: string | null;
active: boolean;
stats: { spans: number; tools: number; llms: number; errors: number; dur: number; agents?: number; inTokens?: number; outTokens?: number };
stats: { spans: number; tools: number; llms: number; errors: number; dur: number; agents?: number; inTokens?: number; outTokens?: number; totalTokens?: number };
allSpans?: Span[];
startedAt?: number;
anthropicModels?: string[];
Expand Down Expand Up @@ -1344,6 +1347,7 @@ export function RunDetail({ runId, routeBase, initialData, isReplay, source, onF
agents: subAgents.length,
inTokens: [...getTokensByModel(spans).values()].reduce((s, v) => s + v.inTok, 0),
outTokens: [...getTokensByModel(spans).values()].reduce((s, v) => s + v.outTok, 0),
totalTokens: spans.reduce((sum, s) => sum + (s.total_tokens ?? 0), 0),
}}
allSpans={spans}
run={run}
Expand Down
1 change: 1 addition & 0 deletions app/src/components/SpanTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ function SpanDetail({ span }: { span: Span }) {
{span.model && <><div style={{ color: C.fg0 }}>model</div><div style={{ color: C.fg2 }}>{span.model}</div></>}
{span.input_tokens != null && <><div style={{ color: C.fg0 }}>input tokens</div><div style={{ color: C.fg2 }}>{span.input_tokens.toLocaleString()}</div></>}
{span.output_tokens != null && <><div style={{ color: C.fg0 }}>output tokens</div><div style={{ color: C.fg2 }}>{span.output_tokens.toLocaleString()}</div></>}
{span.total_tokens != null && <><div style={{ color: C.fg0 }}>total tokens</div><div style={{ color: C.fg2 }}>{span.total_tokens.toLocaleString()}</div></>}
<div style={{ color: C.fg0 }}>status</div>
<div style={{ color: isErr ? C.red : C.fg2 }}>{span.status}</div>
<div style={{ color: C.fg0 }}>start</div>
Expand Down
4 changes: 2 additions & 2 deletions app/src/pages/SavedPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ interface TraceSpan {
span_name: string; span_type: string; status: string;
start_time_ns: number; end_time_ns: number; duration_ns: number;
input: string | null; output: string | null;
input_tokens: number | null; output_tokens: number | null;
input_tokens: number | null; output_tokens: number | null; total_tokens?: number | null;
model: string | null; provider: string | null;
attributes: Record<string, string | number>;
}
Expand Down Expand Up @@ -1048,7 +1048,7 @@ function mapTraceToSpans(traces: TraceSpan[], eventId: string): Span[] {
input_payload: inputPayload, output_payload: outputPayload,
start_time_ms: t.start_time_ns / 1e6, end_time_ms: t.end_time_ns / 1e6,
duration_ms: t.duration_ns / 1e6, model: t.model, provider: t.provider,
input_tokens: t.input_tokens, output_tokens: t.output_tokens,
input_tokens: t.input_tokens, output_tokens: t.output_tokens, total_tokens: t.total_tokens ?? null,
attributes: Object.keys(t.attributes).length > 0 ? JSON.stringify(t.attributes) : null,
};
});
Expand Down
2 changes: 2 additions & 0 deletions app/src/pages/SearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ interface TraceSpan {
output: string | null;
input_tokens: number | null;
output_tokens: number | null;
total_tokens?: number | null;
model: string | null;
provider: string | null;
attributes: Record<string, string | number>;
Expand Down Expand Up @@ -134,6 +135,7 @@ function mapTraceToSpans(traces: TraceSpan[], eventId: string): Span[] {
provider: t.provider,
input_tokens: t.input_tokens,
output_tokens: t.output_tokens,
total_tokens: t.total_tokens ?? null,
attributes: Object.keys(t.attributes).length > 0 ? JSON.stringify(t.attributes) : null,
}; });
}
Expand Down
1 change: 1 addition & 0 deletions app/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export interface Span {
provider: string | null;
input_tokens: number | null;
output_tokens: number | null;
total_tokens: number | null;
attributes: string | null;
/**
* SDK-agnostic typed view of this span's content. Populated by the server.
Expand Down
5 changes: 3 additions & 2 deletions app/tests-e2e/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ export type DbSpanRow = {
async function fetchSpansViaApi(
workshopUrl: string,
runId: string,
): Promise<{ id: string; name: string; span_type: string | null; status: string | null; input_preview: string; output_preview: string; model: string | null; tokens: { in: number; out: number } }[]> {
): Promise<{ id: string; name: string; span_type: string | null; status: string | null; input_preview: string; output_preview: string; model: string | null; tokens: { in: number; out: number; total: number } }[]> {
const res = await fetch(
`${workshopUrl}/api/runs/${encodeURIComponent(runId)}/spans?limit=500&payload_preview_chars=400`,
);
Expand All @@ -395,7 +395,7 @@ async function fetchSpansViaApi(
input_preview: string;
output_preview: string;
model: string | null;
tokens: { in: number; out: number };
tokens: { in: number; out: number; total: number };
}[]
>;
}
Expand Down Expand Up @@ -431,6 +431,7 @@ export type LlmSpanRow = {
model: string | null;
input_tokens: number | null;
output_tokens: number | null;
total_tokens: number | null;
};

/**
Expand Down
1 change: 1 addition & 0 deletions drizzle/0002_volatile_mordo.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `spans` ADD `total_tokens` integer;
Loading