Skip to content

feat: Add instrumentation for anthropic.beta.sessions - #2333

Merged
Luca Forstner (lforst) merged 5 commits into
mainfrom
lforst/dum-e/kyoto-e4eec1f3a9
Aug 4, 2026
Merged

feat: Add instrumentation for anthropic.beta.sessions#2333
Luca Forstner (lforst) merged 5 commits into
mainfrom
lforst/dum-e/kyoto-e4eec1f3a9

Conversation

@lforst

@lforst Luca Forstner (lforst) commented Aug 3, 2026

Copy link
Copy Markdown
Member

Adds instrumentation for anthropic.beta.sessions.

The anthropic API is either sessions.event.stream() or sessions.threads.events.stream(). You are reading events from something that is happening on anthropic infra - that's why I didn't want to make these apis auto instrumented, instead, the user should be manually collecting the streams with collectAnthropicSession():

import Anthropic from "@anthropic-ai/sdk";
import {
  collectAnthropicSession,
  wrapAnthropic,
} from "braintrust";

const anthropic = wrapAnthropic(new Anthropic());

const stream = collectAnthropicSession(
  await anthropic.beta.sessions.events.stream("session-id", {
    event_deltas: ["agent.message"],
  }),
);

for await (const event of stream) {
  console.log(event);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah interesting. I decided to auto-instrument this in python iirc. Maybe I should revert that there too. If we decide this is how we want the sdks to work, I'd add a spec doc change for this.

@lforst

Copy link
Copy Markdown
Member Author

Ah interesting. I decided to auto-instrument this in python iirc. Maybe I should revert that there too. If we decide this is how we want the sdks to work, I'd add a spec doc change for this.

Abhijeet Prasad (@AbhiPrasad) yeah the main reason I decided to do it like this is that you may have braintrust tracing be "unwanted collateral damage" if we auto-instrument such APIs. I don't think there is a clear right or wrong here, but since getting the stream is mostly a manual decision someone has to make, I thought it would also be fine to only trace manually. Like I would be kinda surprised if I had something like analytics on the sessions, and suddenly braintrust traces my analytics service. Or maybe even emits duplicates traces if I retrieve the session events from multiple places. Makes sense?

@AbhiPrasad

Copy link
Copy Markdown
Member

I think you're right here. I'm going to update the python SDK accordingly.

@lforst
Luca Forstner (lforst) merged commit e2d5ea2 into main Aug 4, 2026
49 checks passed
@lforst
Luca Forstner (lforst) deleted the lforst/dum-e/kyoto-e4eec1f3a9 branch August 4, 2026 16:26
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