Skip to content

otel: emit reasoning_tokens on invoke_agent root span for foreground agent#319469

Merged
zhichli merged 2 commits into
mainfrom
zhichli/reasoning-tokens-invoke-agent
Jun 1, 2026
Merged

otel: emit reasoning_tokens on invoke_agent root span for foreground agent#319469
zhichli merged 2 commits into
mainfrom
zhichli/reasoning-tokens-invoke-agent

Conversation

@zhichli
Copy link
Copy Markdown
Member

@zhichli zhichli commented Jun 1, 2026

chatMLFetcher already sets gen_ai.usage.reasoning.output_tokens on every chat span, but the invoke_agent root span in toolCallingLoop didn't aggregate it. App Insights queries reading parent-only totals were missing reasoning.

  • Accumulate response.usage.completion_tokens_details.reasoning_tokens across turns.
  • Emit on the span when non-zero. Reasoning is a subset of output_tokens, not an addition.

vscode-claude has the same gap; Anthropic SDK Usage doesn't expose thinking tokens, so it needs a bridge from ClaudeStreamingPassThroughEndpoint. Inline TODO left.

…agent

Reasoning/thinking tokens are already populated on every chat span by
chatMLFetcher (dual-emit of gen_ai.usage.reasoning_tokens and
gen_ai.usage.reasoning.output_tokens), but the invoke_agent root span
built by toolCallingLoop only aggregated input/output/cache_read/cache_creation
totals. As a result App Insights queries that read parent-only token
totals off the invoke_agent span were missing reasoning entirely.

Accumulate response.usage.completion_tokens_details.reasoning_tokens
across all turns (same shape chatMLFetcherTelemetry.ts already consumes
for 1DS telemetry) and dual-emit it onto the span when non-zero.

ATIF / Kusto reports already read this from chat spans via
transformSqliteToAtif (vscode-copilot-evaluation), so this is purely an
App-Insights-side parity fix for the foreground agent.

vscode-claude has the same gap on its invoke_agent root span. Documented
the limitation on _accumulateParentTokenUsage: the Anthropic SDK Usage
type does not surface reasoning/thinking tokens separately, so fixing
the Claude case requires bridging the count from
ClaudeStreamingPassThroughEndpoint into ClaudeOTelTracker and is left as
a follow-up.
Copilot AI review requested due to automatic review settings June 1, 2026 22:35
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates Copilot’s OpenTelemetry (OTel) root invoke_agent span for the foreground agent so that it includes aggregated reasoning/thinking token totals, matching what is already emitted on per-turn chat spans. This closes a telemetry parity gap for App Insights dashboards that query only the parent/root span.

Changes:

  • Accumulates response.usage.completion_tokens_details.reasoning_tokens across all turns in toolCallingLoop.ts.
  • Conditionally dual-emits aggregated reasoning totals on the root span under both gen_ai.usage.reasoning_tokens (legacy) and gen_ai.usage.reasoning.output_tokens (semconv-aligned) when non-zero.
  • Documents why the equivalent Claude root span cannot currently emit reasoning tokens in claudeOTelTracker.ts.
Show a summary per file
File Description
extensions/copilot/src/extension/intents/node/toolCallingLoop.ts Aggregates and emits reasoning token usage on the foreground agent’s invoke_agent root span.
extensions/copilot/src/extension/chatSessions/claude/node/claudeOTelTracker.ts Adds inline documentation explaining the Claude reasoning-token aggregation gap and pointing to the foreground pattern.

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 1

Comment thread extensions/copilot/src/extension/intents/node/toolCallingLoop.ts Outdated
@zhichli zhichli enabled auto-merge June 1, 2026 23:02
@zhichli zhichli merged commit 596efb2 into main Jun 1, 2026
25 checks passed
@zhichli zhichli deleted the zhichli/reasoning-tokens-invoke-agent branch June 1, 2026 23:13
@vs-code-engineering vs-code-engineering Bot added this to the 1.124.0 milestone Jun 1, 2026
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.

3 participants