Skip to content

fix(trace-spans): fix input/output token count in trace spans - #2168

Merged
waleedlatif1 merged 1 commit into
stagingfrom
fix/trace-spans
Dec 3, 2025
Merged

fix(trace-spans): fix input/output token count in trace spans#2168
waleedlatif1 merged 1 commit into
stagingfrom
fix/trace-spans

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • fix input/output token count in trace spans

Type of Change

  • Bug fix

Testing

Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel

vercelBot commented Dec 3, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
ProjectDeploymentPreviewCommentsUpdated (UTC)
docsSkippedSkippedDec 3, 2025 4:31am

@greptile-apps

Copy link
Copy Markdown
Contributor

Greptile Overview

Greptile Summary

Fixed token count aggregation in calculateCostSummary to handle both naming conventions (prompt/input and completion/output) when extracting token usage from trace spans.

Key changes:

  • Added fallback logic using nullish coalescing to check both span.tokens.prompt and span.tokens.input for prompt tokens
  • Added fallback logic for span.tokens.completion and span.tokens.output for completion tokens
  • Extracted token values into variables to ensure consistency across both total and per-model aggregation
  • Removed outdated comments that no longer applied
  • Aligns with existing pattern used in telemetry.ts

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The fix is a straightforward bug fix that adds defensive fallback logic for token counting. It aligns with existing patterns in the codebase (telemetry.ts), has no breaking changes, and only improves accuracy of token aggregation without affecting other functionality
  • No files require special attention

Important Files Changed

File Analysis

FilenameScoreOverview
apps/sim/lib/logs/execution/logging-factory.ts5/5Added fallback logic for token count fields to handle both prompt/input and completion/output naming conventions, ensuring accurate token aggregation across different trace span formats

Sequence Diagram

sequenceDiagram
participant Caller
participant calculateCostSummary
participant collectCostSpans
participant TraceSpans
Caller->>calculateCostSummary: Call with traceSpans[]
calculateCostSummary->>calculateCostSummary: Initialize totals & models object
calculateCostSummary->>collectCostSpans: Recursively collect spans with cost data
collectCostSpans->>TraceSpans: Traverse span tree
TraceSpans-->>collectCostSpans: Return spans with cost info
collectCostSpans-->>calculateCostSummary: Return costSpans[]
loop For each costSpan
calculateCostSummary->>calculateCostSummary: Extract promptTokens (prompt ?? input)
calculateCostSummary->>calculateCostSummary: Extract completionTokens (completion ?? output)
calculateCostSummary->>calculateCostSummary: Aggregate costs and tokens
calculateCostSummary->>calculateCostSummary: Update model-specific metrics
end
calculateCostSummary->>calculateCostSummary: Add BASE_EXECUTION_CHARGE
calculateCostSummary-->>Caller: Return cost summary with totals
Loading

@greptile-appsgreptile-appsBot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@waleedlatif1
waleedlatif1 merged commit 3e83fb3 into stagingDec 3, 2025
9 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/trace-spans branch December 3, 2025 04:36
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@waleedlatif1