Skip to content

feat(core): Accumulate tokens for gen_ai.invoke_agent spans from child LLM calls #17281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

RulaKhaled
Copy link
Member

Problem

Currently, gen_ai.invoke_agent spans (representing operations like generateText()) contain inaccurate token usage information. Users can only see token data on individual gen_ai.generate_text child spans, but the tokens are not accumulated across nested spans, making it difficult to track total token consumption for complete AI operations.

Solution

Implement token accumulation for gen_ai.invoke_agent spans by iterating over client LLM child spans and aggregating their token usage.

Copy link

linear bot commented Aug 1, 2025

// TODO: Determine how to handle token aggregation for tool call spans.
for (const span of event.spans) {
accumulateTokensFromChildSpans(span, event.spans);
}
Copy link

Choose a reason for hiding this comment

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

Bug: Token Accumulation Error in Nested Spans

Token accumulation for nested gen_ai.invoke_agent spans is incorrect. The accumulateTokensFromChildSpans function processes spans in arbitrary array order. This means a parent gen_ai.invoke_agent span may aggregate tokens from its children before those children have fully accumulated tokens from their own descendants, resulting in incomplete token totals on the parent.

Locations (1)
Fix in Cursor Fix in Web

Copy link
Contributor

github-actions bot commented Aug 1, 2025

size-limit report 📦

Path Size % Change Change
@sentry/browser 23.76 kB - -
@sentry/browser - with treeshaking flags 22.35 kB - -
@sentry/browser (incl. Tracing) 39.43 kB - -
@sentry/browser (incl. Tracing, Replay) 77.52 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 67.39 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 82.22 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 94.35 kB - -
@sentry/browser (incl. Feedback) 40.45 kB - -
@sentry/browser (incl. sendFeedback) 28.45 kB - -
@sentry/browser (incl. FeedbackAsync) 33.34 kB - -
@sentry/react 25.5 kB - -
@sentry/react (incl. Tracing) 41.4 kB - -
@sentry/vue 28.2 kB - -
@sentry/vue (incl. Tracing) 41.23 kB - -
@sentry/svelte 23.79 kB - -
CDN Bundle 25.28 kB - -
CDN Bundle (incl. Tracing) 39.3 kB - -
CDN Bundle (incl. Tracing, Replay) 75.38 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 80.82 kB - -
CDN Bundle - uncompressed 73.86 kB - -
CDN Bundle (incl. Tracing) - uncompressed 116.35 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 230.56 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 243.37 kB - -
@sentry/nextjs (client) 43.46 kB - -
@sentry/sveltekit (client) 39.87 kB - -
@sentry/node-core 47.53 kB - -
@sentry/node 146.31 kB +0.09% +125 B 🔺
@sentry/node - without tracing 91.63 kB - -
@sentry/aws-serverless 103.08 kB - -

View base workflow run

Sign up for free to 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