feat: add OTel invoke_agent spans to inline chat#4151
Open
Conversation
Add root invoke_agent spans wrapping both inline chat strategies (tools and heuristic), giving inline chat full OTel parity with the foreground agent: - invoke_agent InlineChat2Intent root span groups all child chat and execute_tool spans under a single trace - Aggregated gen_ai.usage.input/output_tokens on root span - copilot_chat.agent.invocation.duration metric - copilot_chat.agent.turn.count metric - copilot_chat.session.start event per interaction - copilot_chat.agent.turn event per LLM round-trip - Error handling with span status and error.type attribute Fixes microsoft/vscode#298885
Contributor
There was a problem hiding this comment.
Pull request overview
Adds OpenTelemetry (OTel) invoke_agent root span instrumentation to inline chat edit flows so inline chat has observability parity with the foreground agent, including aggregated token usage, events, and agent-level metrics.
Changes:
- Wrapes
InlineChatEditToolsStrategyandInlineChatEditHeuristicStrategyexecutions ininvoke_agent InlineChat2Intentroot spans with GenAI attributes. - Emits
copilot_chat.session.start/copilot_chat.agent.turnevents and records agent duration + turn count metrics. - Aggregates token usage onto the root span and sets span status/attributes for completion (and errors in the tools strategy).
alexr00
previously approved these changes
Mar 3, 2026
emitAgentTurnEvent was receiving cumulative totalInputTokens/totalOutputTokens, making each subsequent turn event look inflated. Now computes per-turn token counts from result.fetchResult.usage and emits those, matching the pattern used in ToolCallingLoop. Accumulated totals are still used for root span attributes.
Wrap the InlineChatEditHeuristicStrategy's startActiveSpan callback body in try/catch to set SpanStatusCode.ERROR and StdAttr.ERROR_TYPE on failures, matching the tools strategy and toolCallingLoop patterns.
roblourens
approved these changes
Mar 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add OTel
invoke_agentroot spans to inline chat, giving it full observability parity with the foreground agent.Changes:
InlineChatEditToolsStrategyandInlineChatEditHeuristicStrategywithinvoke_agent InlineChat2Intentroot spanschatandexecute_toolspans auto-nest under the root spangen_ai.usage.input/output_tokenson root span (enables single-query token usage for inline chat benchmarks)copilot_chat.agent.invocation.durationandcopilot_chat.agent.turn.countmetricscopilot_chat.session.startandcopilot_chat.agent.turneventsSpanStatusCode.ERROR+error.typeFixes microsoft/vscode#298885