Add OTel tracing for Claude agent sessions#310878
Merged
Merged
Conversation
Contributor
|
Base:
|
Contributor
There was a problem hiding this comment.
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
chatspans from the CAPI proxy are parented under the per-requestinvoke_agent clauderoot 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
ada75d1 to
bb07f50
Compare
bb07f50 to
07470ce
Compare
…r gen_ai.usage consistency
…gent tracing and usage metrics
…moving beta tracing and content capture variables
…larity feat(tracing): rename hook_name attribute to hook_command for better context fix(agent): clear subagent trace contexts after request completion
07470ce to
bb13080
Compare
TylerLeonhardt
approved these changes
Apr 27, 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 OpenTelemetry tracing for Claude agent sessions with full GenAI semantic convention compliance.
invoke_agent clauderoot span with token usage, cache breakdown, cost, and turn countchatspans parented under invoke_agent via trace context propagation through CAPI proxyexecute_toolandexecute_hookspans with subagent nesting underexecute_tool Agentcache_read,cache_creation) on both root and per-chat spans for all agent typesagent_monitoring.md