Skip to content

Add OTel tracing for Claude agent sessions#310878

Merged
zhichli merged 11 commits into
mainfrom
zhichli/claudeOtel
Apr 27, 2026
Merged

Add OTel tracing for Claude agent sessions#310878
zhichli merged 11 commits into
mainfrom
zhichli/claudeOtel

Conversation

@zhichli
Copy link
Copy Markdown
Member

@zhichli zhichli commented Apr 17, 2026

Add OpenTelemetry tracing for Claude agent sessions with full GenAI semantic convention compliance.

  • invoke_agent claude root span with token usage, cache breakdown, cost, and turn count
  • chat spans parented under invoke_agent via trace context propagation through CAPI proxy
  • execute_tool and execute_hook spans with subagent nesting under execute_tool Agent
  • Parent-only token rollup on root span (excludes subagent tokens, comparable with foreground agent)
  • Cache token attributes (cache_read, cache_creation) on both root and per-chat spans for all agent types
  • OTel env forwarding to Claude SDK subprocess (metrics + logs exporters)
  • Documentation in agent_monitoring.md
image

Copilot AI review requested due to automatic review settings April 17, 2026 00:39
@zhichli zhichli marked this pull request as ready for review April 17, 2026 00:40
@zhichli zhichli requested a review from TylerLeonhardt April 17, 2026 00:41
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 17, 2026

Base: c2235ee2 Current: 98fdfa2b

No screenshot changes.

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

Adds OpenTelemetry (OTel) tracing enhancements to Claude agent sessions in the Copilot extension, aligning emitted spans/attributes with GenAI semantic conventions and extending cache token usage reporting across agent types.

Changes:

  • Extend usage schemas + span attributes to include cache creation token breakdown (gen_ai.usage.cache_creation.input_tokens) alongside cache read tokens.
  • Add Claude session OTel context propagation so chat spans from the CAPI proxy are parented under the per-request invoke_agent claude root span (and support subagent nesting).
  • Document Claude agent tracing behavior and cache usage attributes in agent_monitoring.md.
Show a summary per file
File Description
extensions/copilot/src/platform/networking/common/openai.ts Extends APIUsage.prompt_tokens_details to include cache_creation_input_tokens.
extensions/copilot/src/platform/endpoint/node/messagesApi.ts Populates cache_creation_input_tokens in Anthropic Messages API usage output.
extensions/copilot/src/extension/prompt/node/chatMLFetcher.ts Emits cache creation token usage on chat spans when present.
extensions/copilot/src/extension/intents/node/toolCallingLoop.ts Rolls up cache read/creation token counts onto invoke_agent spans for the foreground agent loop.
extensions/copilot/src/extension/chatSessions/claude/common/claudeSessionStateService.ts Adds per-session stored TraceContext getter/setter to support span parenting.
extensions/copilot/src/extension/chatSessions/claude/node/claudeSessionStateService.ts Implements trace-context persistence in Claude session state.
extensions/copilot/src/extension/chatSessions/claude/node/claudeLanguageModelServer.ts Wraps proxy requests in stored trace context so chat spans parent to invoke_agent.
extensions/copilot/src/extension/chatSessions/claude/node/claudeCodeAgent.ts Introduces invoke_agent claude root span per request, token rollups, subagent context switching, and OTel env forwarding to subprocess.
extensions/copilot/src/extension/chatSessions/claude/common/claudeMessageDispatch.ts Parents execute_tool spans correctly under root/subagent contexts; renames hook span naming/attributes.
extensions/copilot/src/extension/chatSessions/claude/common/test/claudeMessageDispatch.spec.ts Updates test state shape for new subagent trace context tracking.
extensions/copilot/docs/monitoring/agent_monitoring.md Documents cache token attributes and Claude agent tracing/span hierarchy.

Copilot's findings

  • Files reviewed: 11/11 changed files
  • Comments generated: 5

Comment thread extensions/copilot/src/extension/chatSessions/claude/node/claudeCodeAgent.ts Outdated
Comment thread extensions/copilot/docs/monitoring/agent_monitoring.md Outdated
Comment thread extensions/copilot/docs/monitoring/agent_monitoring.md Outdated
@zhichli zhichli force-pushed the zhichli/claudeOtel branch 3 times, most recently from ada75d1 to bb07f50 Compare April 20, 2026 16:25
@zhichli zhichli linked an issue Apr 23, 2026 that may be closed by this pull request
@rwoll rwoll closed this Apr 24, 2026
@rwoll rwoll reopened this Apr 27, 2026
@zhichli zhichli force-pushed the zhichli/claudeOtel branch from bb07f50 to 07470ce Compare April 27, 2026 21:13
@zhichli zhichli force-pushed the zhichli/claudeOtel branch from 07470ce to bb13080 Compare April 27, 2026 21:18
@zhichli zhichli merged commit 51e531a into main Apr 27, 2026
26 checks passed
@zhichli zhichli deleted the zhichli/claudeOtel branch April 27, 2026 22:31
@vs-code-engineering vs-code-engineering Bot added this to the 1.119.0 milestone Apr 27, 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.

Support OTel Instrumentation for Claude Agent

4 participants