Skip to content

fix(server-utils): Record Anthropic chat spans for thread and tool-runner streams on 1.2+ - #25931

Open
RulaKhaled wants to merge 3 commits into
developfrom
fix/anthropic-1x-thread-chat-spans
Open

RulaKhaled wants to merge 3 commits into
developfrom
fix/anthropic-1x-thread-chat-spans

Conversation

@RulaKhaled

Copy link
Copy Markdown
Collaborator

On @anthropic-ai/sdk 1.2.0+, streamed turns from beta.messages.toolRunner({ stream: true }) and beta.messages.thread().stream() had no gen_ai.chat spans. Every tool runner runs over a thread since 1.2.0, including on Bedrock and Vertex.

The thread calls beta.messages.create(), which opens our span, but parses the response through a new APIPromise our hook never sees, so the span never ended. This hooks BetaMessageStream._start(), matches each request to its stream by abort signal, and ends the span from the stream's events with usage and output.

instrumentMessageStream now also ends the span on end, so aborted or truncated streams, messages.stream() included, no longer leave it open. An abort before the response is not marked as an error.

Stacked on #25750.

Fixes #25816

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Compared: 331f9fb (PR test merge using develop at 8a102ea)

Against: 8a102ea (develop) · base run #64990

✅ Baseline matches: this diff contains only this PR's changes.

Path Size % Change Change
@sentry/browser 30.4 kB - -
@sentry/browser - with treeshaking flags 28.54 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 28.43 kB - -
@sentry/browser (incl. Tracing) 52.72 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 52.72 kB - -
@sentry/browser (incl. Tracing, Profiling) 55.69 kB - -
@sentry/browser (incl. Tracing, Replay) 93.02 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 81.61 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 97.76 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 110.75 kB - -
@sentry/browser (incl. Feedback) 47.98 kB - -
@sentry/browser (incl. sendFeedback) 35.5 kB - -
@sentry/browser (incl. FeedbackAsync) 40.61 kB - -
@sentry/browser (incl. Metrics) 31.44 kB - -
@sentry/browser (incl. Logs) 31.73 kB - -
@sentry/browser (incl. Metrics & Logs) 32.45 kB - -
@sentry/react 32.29 kB - -
@sentry/react (incl. Tracing) 55.09 kB - -
@sentry/vue 38.67 kB - -
@sentry/vue (incl. Tracing) 55.73 kB - -
@sentry/svelte 30.43 kB - -
@sentry/remix (Remix 3 client bundle) 57.8 kB - -
CDN Bundle 32.19 kB - -
CDN Bundle (incl. Tracing) 53.24 kB - -
CDN Bundle (incl. Logs, Metrics) 34.47 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 55.19 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 75.88 kB - -
CDN Bundle (incl. Tracing, Replay) 91.46 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 93.39 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 97.56 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 99.56 kB - -
CDN Bundle - uncompressed 94.67 kB - -
CDN Bundle (incl. Tracing) - uncompressed 157.63 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 101.14 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 163.37 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 232.71 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 279.32 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 285.05 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 293.03 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 298.74 kB - -
@sentry/nextjs (client) 58.04 kB - -
@sentry/sveltekit (client) 53.23 kB - -
@sentry/core/server 44.25 kB - -
@sentry/core/browser 13.93 kB - -
@sentry/node 155.93 kB +0.16% +234 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 82.3 kB +0.05% +36 B 🔺
@sentry/node - without tracing 96.94 kB +0.07% +65 B 🔺
@sentry/node - without channel injection 136.24 kB +0.17% +220 B 🔺
@sentry/aws-serverless 105.15 kB +0.07% +65 B 🔺
@sentry/cloudflare (withSentry) - minified 200.67 kB - -
@sentry/cloudflare (withSentry) 499.05 kB - -
@sentry/nextjs/cloudflare (withSentry) - minified 221.22 kB - -

View base workflow run

@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Optional E2E tests

Some optional checks were not successful. They do not block the merge. 2 not successful, 51 successful. See the run.

Check Status
❌ CI: Optional E2E Tests / E2E cloudflare-pi-durable (latest) Test (optional) Failing after 50s Details
❌ CI: Optional E2E Tests / E2E node-flue (latest) Test (optional) Failing after 26s Details
Successful checks
Check Status
✅ CI: Optional E2E Tests / E2E cloudflare-think Test (optional) Successful in 55s Details
✅ CI: Optional E2E Tests / E2E cloudflare-pi-durable Test (optional) Successful in 28s Details
✅ CI: Optional E2E Tests / E2E debug-id-sourcemaps Test (optional) Successful in 25s Details
✅ CI: Optional E2E Tests / E2E cloudflare-workers-send-to-sentry Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E cloudflare-think (ai v6) Test (optional) Successful in 57s Details
✅ CI: Optional E2E Tests / E2E cloudflare-flue-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries Test (optional) Successful in 32s Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E astro-7 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-mastra Test (optional) Successful in 39s Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (latest) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E astro-7-static Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries (latest) Test (optional) Successful in 49s Details
✅ CI: Optional E2E Tests / E2E deno (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-eve (orchestrion) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (deno) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-pi-durable (latest) Test (optional) Successful in 24s Details
✅ CI: Optional E2E Tests / E2E node-openai-decisions (latest) Test (optional) Successful in 17s Details
✅ CI: Optional E2E Tests / E2E node-mastra Test (optional) Successful in 50s Details
✅ CI: Optional E2E Tests / E2E nextjs-16-tunnel (cloudflare) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v2 Test (optional) Successful in 16s Details
✅ CI: Optional E2E Tests / E2E node-eve (latest) Test (optional) Successful in 44s Details
✅ CI: Optional E2E Tests / E2E node-fastify-6 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-fastify-6 (tracer provider) Test (optional) Successful in 21s Details
✅ CI: Optional E2E Tests / E2E solidstart-2 Test (optional) Successful in 36s Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps Test (optional) Successful in 31s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (static / nuxt generate) Test (optional) Successful in 21s Details
✅ CI: Optional E2E Tests / E2E cloudflare-agent Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-mastra (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v1 Test (optional) Successful in 50s Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-cloudflare Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-send-to-sentry-static Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-flue Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (cloudflare, latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-orpc Test (optional) Successful in 54s Details
✅ CI: Optional E2E Tests / E2E react-router-7-framework-instrumentation Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries (cloudflare) Test (optional) Successful in 57s Details
✅ CI: Optional E2E Tests / E2E nextjs-14 (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5 Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext, bun) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nestjs-12 (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E supabase-nextjs Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (client source maps kept) Test (optional) Successful in 21s Details
✅ CI: Optional E2E Tests / E2E node-express-send-to-sentry Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-pi-durable Test (optional) Successful in 23s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps Test (optional) Successful in 23s Details
✅ CI: Optional E2E Tests / E2E node-eve Test (optional) Successful in 51s Details
✅ CI: Optional E2E Tests / E2E node-openai-decisions Test (optional) Successful in 17s Details
✅ CI: Optional E2E Tests / E2E nuxt-5 (tunnel-dev) Test (optional) Successful in 29s Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (turbo) Test (optional) Successful in 2m Details

@RulaKhaled
RulaKhaled added this pull request to stack #25993 November 2, 2026 11:53
@RulaKhaled
RulaKhaled force-pushed the fix/anthropic-1x-thread-chat-spans branch 2 times, most recently from b953034 to 4198705 Compare November 2, 2026 14:30
@RulaKhaled

Copy link
Copy Markdown
Collaborator Author

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@RulaKhaled

Copy link
Copy Markdown
Collaborator Author

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit aedfeab. Configure here.

@RulaKhaled
RulaKhaled marked this pull request as ready for review November 2, 2026 15:06
@RulaKhaled
RulaKhaled requested a review from a team as a code owner November 2, 2026 15:06
@RulaKhaled
RulaKhaled requested review from andreiborza, isaacs and nicohrubec and removed request for a team November 2, 2026 15:06
Base automatically changed from fix/anthropic-sdk-1x to develop November 4, 2026 13:35
@RulaKhaled
RulaKhaled requested review from a team as code owners November 4, 2026 13:35
@RulaKhaled
RulaKhaled requested review from Lms24, logaretm and s1gr1d and removed request for a team November 4, 2026 13:35
RulaKhaled and others added 3 commits November 4, 2026 14:36
…nner streams on 1.2+

On @anthropic-ai/sdk 1.2.0+, streamed turns from
beta.messages.toolRunner({ stream: true }) and beta.messages.thread().stream()
had no gen_ai.chat spans. The thread parses the response of each create() call
through a new APIPromise our hook never sees, so the span never ended. Hook
BetaMessageStream._start(), match each request to its stream by abort signal,
and end the span from the stream's events.

instrumentMessageStream now also ends the span on 'end', so aborted or truncated
streams no longer leave it open.

Fixes #25816

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
A later create() sent with a finished thread stream's signal was wired to that
stream and its span never ended. Drop the entry once an attempt succeeds, since
only rejected attempts are retried. Also cover the 'end' listener with a unit
test and update the wrapStreamResult JSDoc.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Each mode and turn is now its own test.each case, with no conditionals, so a
failure points at one path. The cases share one scenario run per env, since
every run is a Node process.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@RulaKhaled
RulaKhaled force-pushed the fix/anthropic-1x-thread-chat-spans branch from de27780 to 7fe1266 Compare November 4, 2026 13:36

This branch has not been deployed

No deployments
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.

Anthropic tool runner and message threads drop gen_ai.chat spans on @anthropic-ai/sdk >=1.2.0

1 participant