Skip to content

Commit 3e83fb3

Browse files
authored
fix(trace-spans): fix input/output token count in trace spans (#2168)
1 parent a6e3c92 commit 3e83fb3

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

apps/sim/lib/logs/execution/logging-factory.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ export function calculateCostSummary(traceSpans: any[]): {
8080
}
8181
}
8282

83-
// Recursively collect all spans with cost information from the trace span tree
8483
const collectCostSpans = (spans: any[]): any[] => {
8584
const costSpans: any[] = []
8685

@@ -119,12 +118,12 @@ export function calculateCostSummary(traceSpans: any[]): {
119118
totalCost += span.cost.total || 0
120119
totalInputCost += span.cost.input || 0
121120
totalOutputCost += span.cost.output || 0
122-
// Tokens are at span.tokens, not span.cost.tokens
121+
const promptTokens = span.tokens?.prompt ?? span.tokens?.input ?? 0
122+
const completionTokens = span.tokens?.completion ?? span.tokens?.output ?? 0
123123
totalTokens += span.tokens?.total || 0
124-
totalPromptTokens += span.tokens?.prompt || 0
125-
totalCompletionTokens += span.tokens?.completion || 0
124+
totalPromptTokens += promptTokens
125+
totalCompletionTokens += completionTokens
126126

127-
// Aggregate model-specific costs - model is at span.model, not span.cost.model
128127
if (span.model) {
129128
const model = span.model
130129
if (!models[model]) {
@@ -138,8 +137,8 @@ export function calculateCostSummary(traceSpans: any[]): {
138137
models[model].input += span.cost.input || 0
139138
models[model].output += span.cost.output || 0
140139
models[model].total += span.cost.total || 0
141-
models[model].tokens.prompt += span.tokens?.prompt || 0
142-
models[model].tokens.completion += span.tokens?.completion || 0
140+
models[model].tokens.prompt += promptTokens
141+
models[model].tokens.completion += completionTokens
143142
models[model].tokens.total += span.tokens?.total || 0
144143
}
145144
}

0 commit comments

Comments
 (0)