Skip to content

feat: Support Agent tool call tracing for AI SDK v5/v6#1575

Merged
Luca Forstner (lforst) merged 9 commits intomainfrom
lforst/ai-sdk-parity-gap
Mar 18, 2026
Merged

feat: Support Agent tool call tracing for AI SDK v5/v6#1575
Luca Forstner (lforst) merged 9 commits intomainfrom
lforst/ai-sdk-parity-gap

Conversation

@lforst
Copy link
Member

@lforst Luca Forstner (lforst) commented Mar 17, 2026

Screenshot 2026-03-17 at 15 01 26

^ vast majority of this is snapshots

  • Adds AI SDK auto-instrumentation coverage for agent entrypoints across supported versions: Agent.generate / Agent.stream for v5 and ToolLoopAgent.generate / ToolLoopAgent.stream for v6.
  • Bring auto-instrumentation to parity with the wrapper for AI SDK child spans by wrapping live models and tools at call start, so doGenerate, doStream, and tool execution emit the same nested traces.
  • Fix streaming lifecycle handling in the auto path by patching returned AI SDK streams, which keeps spans open through consumption and preserves correct output, timing, and metric logging.
Screenshot 2026-03-17 at 16 41 41

# Conflicts:
#	e2e/helpers/ai-sdk.ts
#	e2e/scenarios/wrap-ai-sdk-generation-traces/scenario.test.ts
# Conflicts:
#	e2e/scenarios/wrap-ai-sdk-generation-traces/scenario.test.ts
@AbhiPrasad
Copy link
Member

Abhijeet Prasad (AbhiPrasad) commented Mar 17, 2026

I worked on #1582 that also adds v5/v6 e2e tests - but looks like we can merge that in after this PR gets merged in


context.result = result;
asyncStart?.publish(context);
asyncEnd?.publish(context);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we have to do this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in general we did this change because our patching is buggy because if the wrapped function returns a non-promise or null we would crash accessing .then. So it's effectively just a compat change.

The event publishing and context setting is just necessary to be in line with the resolve/reject behavior above.


const REMOVE_NORMALIZED_VALUE = Symbol("braintrust.ai-sdk.remove-normalized");

function normalizeAISDKLoggedOutput(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is defined here and in the wrapper. We can prob DRY.


// Agent.generate - async method (v3-v5 only, Agent structure changed in v6)
// Agent.generate - async method (v5 only)
// The compiled AI SDK bundle emits this as an anonymous class method, so we

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did add support for export aliases to orchestrion (https://github.com/nodejs/orchestrion-js?tab=readme-ov-file#export-aliases). Can we maybe patch orchestrion to make this better somehow?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a blocker per se right now but we can definitely improve that in orchestrion.

@lforst Luca Forstner (lforst) merged commit 117ad69 into main Mar 18, 2026
48 checks passed
@lforst Luca Forstner (lforst) deleted the lforst/ai-sdk-parity-gap branch March 18, 2026 08:49
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.

2 participants