Skip to content

Conversation

@stephentoub
Copy link
Member

@stephentoub stephentoub commented Nov 9, 2025

This addresses several issues:

  1. When FunctionInvokingChatClient is used from an agent, and the agent creates an invoke_agent span, that effectively serves the same purpose as the orchestrate_tools span the FICC would have otherwise created, yielding duplication in the telemetry. If we detect an "invoke_agent" span is the parent, we can simply not create "orchestrate_tools".
  2. When FunctionInvokingChatClient is used from an agent, its operation is much more closely tied to the agent than to the inner chat client it's orchestrating. So whereas by default it gets its ActivitySource from the inner client, if it detects an "invoke_agent" span as current, it will instead use that activity's source. This means that we can still get "execute_tool" spans created even if telemetry isn't enabled for the inner chat client but is for the parent agent.
  3. "execute_tool" optionally has sensitive data to emit. FICC currently determines whether to emit sensitive data based on the inner OpenTelemetryChatClient, since it's also using its ActivitySource. Now when it uses the "invoke_agent" span, it also picks up the sensitivity setting from a custom property on that span.

cc: @rogerbarreto, @TaoChenOSU, @eiriktsarpalis

Microsoft Reviewers: Open in CodeFlow

This addresses several issues:
1. When FunctionInvokingChatClient is used from an agent, and the agent creates an invoke_agent span, that effectively serves the same purpose as the orchestrate_tools span the FICC would have otherwise created, yielding duplication in the telemetry. If we detect an "invoke_agent" span is the parent, we can simply not create "orchestrate_tools".
2. When FunctionInvokingChatClient is used from an agent, its operation is much more closely tied to the agent than to the inner chat client it's orchestrating. So whereas by default it gets its ActivitySource from the inner client, if it detects an "invoke_agent" span as current, it will instead use that activity's source. This means that we can still get "execute_tool" spans created even if telemetry isn't enabled for the inner chat client but is for the parent agent.
3. "execute_tool" optionally has sensitive data to emit. FICC currently determines whether to emit sensitive data based on the inner OpenTelemetryChatClient, since it's also using its ActivitySource. Now when it uses the "invoke_agent" span, it also picks up the sensitivity setting from a custom property on that span.
Copilot AI review requested due to automatic review settings November 9, 2025 14:31
@stephentoub stephentoub requested a review from a team as a code owner November 9, 2025 14:31
@github-actions github-actions bot added the area-ai Microsoft.Extensions.AI libraries label Nov 9, 2025
Copy link
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 adds support for detecting and respecting "invoke_agent" activities when creating telemetry spans in the AI chat completion pipeline. When an "invoke_agent" span is the current activity, the FunctionInvokingChatClient will skip creating an "orchestrate_tools" span and instead use the agent's activity source for tool execution spans. Additionally, the sensitive data configuration from the agent activity is propagated to tool execution activities.

  • Added invoke_agent constant to OpenTelemetryConsts.GenAI for standardized activity naming
  • Modified FunctionInvokingChatClient to detect "invoke_agent" parent activities and adjust telemetry behavior accordingly
  • Propagated sensitive data settings from agent activities to tool execution activities via custom properties

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/FunctionInvokingChatClientTests.cs Added three comprehensive tests verifying invoke_agent span detection, activity source selection, and sensitive data propagation behavior
src/Libraries/Microsoft.Extensions.AI/OpenTelemetryConsts.cs Added InvokeAgentName constant for standardized activity naming
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/OpenTelemetryChatClient.cs Added internal constants for sensitive data custom property and logic to set this property on chat activities
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs Modified to detect invoke_agent activities, conditionally create orchestrate_tools spans, use agent activity sources, and propagate sensitive data settings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-ai Microsoft.Extensions.AI libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant