[bot] Agent middleware non-streaming path gates token usage and timing behind content capture flag
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 65/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Quiet
- Tech stack
- csharp
- Domain
- observability
Research direction
Start in src/Braintrust.Sdk.AgentFramework/BraintrustAgentMiddleware.cs at RunCoreAsync and compare its metadata handling with GetResponseAsync in BraintrustChatClientMiddleware.cs. Check the standalone helpers in SpanTagHelper.cs. Done means output remains gated by captureMessageContent while token metrics and time-to-first-token are recorded when content capture is disabled.
Written by the indexing model from the issue text.
Description
Summary
The BraintrustAgentMiddleware non-streaming path (RunCoreAsync) gates token usage metrics and time-to-first-token behind _captureMessageContent, so disabling content capture for privacy also drops all execution metrics from agent spans. The BraintrustChatClientMiddleware in this same package correctly separates metadata from content — always recording model, tokens, and timing — making this an inconsistency within the Agent Framework integration.
What is missing
In BraintrustAgentMiddleware.RunCoreAsync(), the output, token metrics, and TTFT are all gated behind _captureMessageContent (line 56):
if (activity != null && _captureMessageContent)
{
SpanTagHelper.SetOutputMessages(activity, response.Messages);
SpanTagHelper.SetTokenMetrics(activity, response.Usage);
SpanTagHelper.SetTimeToFirstToken(activity, (DateTime.UtcNow - startTime).TotalSeconds);
}
When captureMessageContent is false, all three are skipped — including SetTokenMetrics and SetTimeToFirstToken which are execution metadata, not message content.
Correct behavior — BraintrustChatClientMiddleware (same package)
The ChatClient middleware correctly separates metadata from content (lines 53–64):
// Always set — regardless of _captureMessageContent
SpanTagHelper.SetResponseModel(activity, response.ModelId);
SpanTagHelper.SetTokenMetrics(activity, response.Usage);
SpanTagHelper.SetTimeToFirstToken(activity, timeToFirstToken);
// Only content is gated
if (_captureMessageContent)
{
SpanTagHelper.SetOutputMessages(activity, response.Messages);
}
Impact
Users who disable content capture for privacy (a legitimate use case in regulated environments) will get agent-level spans with no token usage, no timing metrics, and no cost attribution — while LLM-level spans from the ChatClient middleware in the same pipeline will include them. This inconsistency makes agent-level cost tracking and performance monitoring unreliable when content capture is off.
Note: Issue #49 describes this same class of bug for OpenAI and Anthropic integrations, but specifically states "The Agent Framework integration in this same repo correctly separates metadata from content." That statement is accurate for BraintrustChatClientMiddleware but not for BraintrustAgentMiddleware.
Braintrust docs status
The Braintrust tracing docs at https://www.braintrust.dev/docs/instrument/trace-llm-calls document that token usage and timing are automatically captured. These execution metrics should not be tied to a content-capture privacy toggle. Status: supported (documented as always captured; incorrectly gated behind content capture in the agent middleware).
Upstream sources
- Microsoft Agents Framework: NuGet packages
Microsoft.Agents.AIv1.0.0 andMicrosoft.Agents.AI.Workflowsv1.0.0 —AgentResponse.Usageprovides token counts as response metadata, separate from message content - M.E.AI
UsageDetailsdocs: https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.ai.usagedetails
Local files inspected
src/Braintrust.Sdk.AgentFramework/BraintrustAgentMiddleware.cs— non-streamingRunCoreAsync(line 56) gatesSetTokenMetricsandSetTimeToFirstTokenbehind_captureMessageContentsrc/Braintrust.Sdk.AgentFramework/BraintrustChatClientMiddleware.cs— non-streamingGetResponseAsync(lines 53–64) correctly separates metadata from contentsrc/Braintrust.Sdk.AgentFramework/SpanTagHelper.cs—SetTokenMetricsandSetTimeToFirstTokenare available as standalone helpers
- Dominant language
- C#
- Stars
- 4
- Forks
- 3
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 3
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from braintrustdata/braintrust-sdk-dotnet
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
[bot] OpenAI instrumentation missing extended token usage details (cached_tokens, reasoning_tokens) Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 5/5 Over a week Newbie friendliness 45/100
All issues in braintrustdata/braintrust-sdk-dotnet
Similar issues
-
type/automation type/tech-debt
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
t/bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
ci-failure-cause test-failure
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
area:auth FE mvp P3
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
klasolsson81/jobbliggaren#1788 ·