[bot] Agent middleware non-streaming path gates token usage and timing behind content capture flag

Open Beginner friendly
#51 0 comments 0 reactions 0 assignees View on GitHub

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

Local files inspected

  • src/Braintrust.Sdk.AgentFramework/BraintrustAgentMiddleware.cs — non-streaming RunCoreAsync (line 56) gates SetTokenMetrics and SetTimeToFirstToken behind _captureMessageContent
  • src/Braintrust.Sdk.AgentFramework/BraintrustChatClientMiddleware.cs — non-streaming GetResponseAsync (lines 53–64) correctly separates metadata from content
  • src/Braintrust.Sdk.AgentFramework/SpanTagHelper.csSetTokenMetrics and SetTimeToFirstToken are available as standalone helpers
Dominant language
C#
Stars
4
Forks
3
Avg merge
1d 23h
Merged PRs (30d)
3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from braintrustdata/braintrust-sdk-dotnet

All issues in braintrustdata/braintrust-sdk-dotnet

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.