Skip to content

Anthropic streaming spans keep message_start input/cache token counts instead of the final message_delta usage #25621

Closed

Description

@pcbeingused333

Is there an existing issue for this?

  • I have checked for existing issues
  • I have reviewed the documentation
  • I am using the latest SDK release

How do you use Sentry?

Sentry SaaS (sentry.io)

Which SDK are you using?

@sentry/node

SDK Version

11.8.0 (also develop at 6fb6b07)

Framework Version

Node 24, @anthropic-ai/sdk with stream: true

Link to Sentry event

No response

Reproduction Example/SDK Setup

Sentry.init({ integrations: [Sentry.anthropicAIIntegration()], tracesSampleRate: 1 });
const stream = client.messages.stream({ model, max_tokens: 1024, tools: [{ type: 'web_search_20250305', name: 'web_search' }], messages });
for await (const _ of stream) {}

Steps to Reproduce

  1. Stream an Anthropic message whose message_delta.usage reports larger cumulative input_tokens / cache_creation_input_tokens / cache_read_input_tokens than message_start (server tools such as web search do this; a mocked stream with message_start input 50 / cache 10 / 20 and a final message_delta with input 120 / cache 30 / 70 shows it too).
  2. Look at the gen_ai.usage.input_tokens and cache attributes of the resulting span.

Expected Result

The span reports the final cumulative counts from message_delta (120 / 30 / 70 in the mocked stream), since Anthropic's message_delta usage is the running total.

Actual Result

The span keeps the message_start values (50 / 10 / 20). handleMessageMetadata in packages/server-utils/src/ai/anthropic-ai/streaming.ts reads only output_tokens from message_delta.

Additional Context

Proposed fix: when message_delta.usage carries numeric input_tokens / cache_* values, overwrite the message_start values with them (they are totals, not increments) and recompute the prompt total; keep the message_start values when the delta omits them. A patch with tests is ready; happy to open the PR.

Activity

  1. linear-code commented on Oct 21, 2026

    @linear-code
  2. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Oct 21, 2026
  3. andreiborza commented on Oct 22, 2026

    @andreiborza
    Member

    Hi @pcbeingused333, thanks for filing this one too :). We'll look into it.

  4. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Oct 22, 2026
  5. added a commit that references this issue on Oct 27, 2026
    2181398
  6. github-actions commented on Oct 28, 2026

    @github-actions
    Contributor

    A PR closing this issue has just been released 🚀

    This issue was referenced by PR #25623, which was included in the 11.10.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions