Skip to content

Token counting in Anthropic #5391

Description

How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.51.0

Steps to Reproduce

import os
import sys
from anthropic import Anthropic
import sentry_sdk
from sentry_sdk.integrations.anthropic import AnthropicIntegration

sentry_sdk.init(
    dsn=...,
    traces_sample_rate=1.0,
    integrations=[AnthropicIntegration(include_prompts=True)],
    send_default_pii=True,
)

client = Anthropic(api_key="...")

messages = [{"role": "user", "content": "Say hello in exactly 5 words."}]

with sentry_sdk.start_transaction() as transaction:
    with client.messages.create(
        max_tokens=1024,
        messages=[{"role": "user", "content": "Hello"}],
        model="claude-sonnet-4-5",
        stream=True,
    ) as stream:
        for text in stream:
            pass

Expected Result

RawMessageDeltaEvent is used for billing.

Actual Result

Input tokens from RawMessageStartEvent are inappropriately added.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions