Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions sentry_sdk/integrations/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class _RecordedUsage:
input_tokens: int = 0
cache_write_input_tokens: "Optional[int]" = 0
cache_read_input_tokens: "Optional[int]" = 0
output_tokens_reasoning: int = 0


class _StreamSpanContext:
Expand Down Expand Up @@ -330,15 +331,16 @@ def _capture_exception(exc: "Any") -> None:
sentry_sdk.capture_event(event, hint=hint)


def _get_token_usage(result: "Messages") -> "tuple[int, int, int, int]":
def _get_token_usage(result: "Messages") -> "tuple[int, int, int, int, int]":
"""
Get token usage from the Anthropic response.
Returns: (input_tokens, output_tokens, cache_read_input_tokens, cache_write_input_tokens)
Returns: (input_tokens, output_tokens, cache_read_input_tokens, cache_write_input_tokens, output_tokens_reasoning)
"""
input_tokens = 0
output_tokens = 0
cache_read_input_tokens = 0
cache_write_input_tokens = 0
output_tokens_reasoning = 0
if hasattr(result, "usage"):
usage = result.usage
if hasattr(usage, "input_tokens") and isinstance(usage.input_tokens, int):
Expand All @@ -353,6 +355,10 @@ def _get_token_usage(result: "Messages") -> "tuple[int, int, int, int]":
usage.cache_creation_input_tokens, int
):
cache_write_input_tokens = usage.cache_creation_input_tokens
if hasattr(usage, "output_tokens_details"):
reasoning = getattr(usage.output_tokens_details, "thinking_tokens", None)
if isinstance(reasoning, int):
output_tokens_reasoning = reasoning

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Absent reasoning tokens recorded as zero

Medium Severity

_get_token_usage and _RecordedUsage default missing reasoning tokens to 0 rather than None. _set_output_data only omits gen_ai.usage.output_tokens.reasoning when the value is None, so every Anthropic span records 0 when thinking_tokens is absent, including older SDKs that used thinking but did not report a breakdown.

Additional Locations (2)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit cf28b45. Configure here.


# Anthropic's input_tokens excludes cached/cache_write tokens.
# Normalize to total input tokens so downstream cost calculations
Expand All @@ -364,6 +370,7 @@ def _get_token_usage(result: "Messages") -> "tuple[int, int, int, int]":
output_tokens,
cache_read_input_tokens,
cache_write_input_tokens,
output_tokens_reasoning,
)


Expand Down Expand Up @@ -417,6 +424,15 @@ def _collect_ai_data(
incoming_usage, "cache_read_input_tokens", None
)

output_tokens_details = getattr(
incoming_usage, "output_tokens_details", None
)
thinking_tokens = getattr(
output_tokens_details, "thinking_tokens", None
)
if thinking_tokens is not None:
usage.output_tokens_reasoning = thinking_tokens

return (
model,
usage,
Expand Down Expand Up @@ -445,6 +461,15 @@ def _collect_ai_data(
)
if cache_read_input_tokens is not None:
usage.cache_read_input_tokens = cache_read_input_tokens

output_tokens_details = getattr(
event.usage, "output_tokens_details", None
)
thinking_tokens = getattr(
output_tokens_details, "thinking_tokens", None
)
if thinking_tokens is not None:
usage.output_tokens_reasoning = thinking_tokens
# TODO: Record event.usage.server_tool_use

if event.delta.stop_reason is not None:
Expand Down Expand Up @@ -726,6 +751,7 @@ def _set_output_data(
content_blocks: "list[dict[str, Any]]",
response_id: "str | None" = None,
finish_reason: "str | None" = None,
output_tokens_reasoning: "int | None" = None,
) -> None:
"""
Set output data for the span based on the AI response."""
Expand Down Expand Up @@ -789,6 +815,11 @@ def _set_output_data(
if output_tokens is not None:
set_on_span(SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens)

if output_tokens_reasoning is not None:
set_on_span(
SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING, output_tokens_reasoning
)

if input_tokens is not None and output_tokens is not None:
set_on_span(SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS, input_tokens + output_tokens)

Expand Down Expand Up @@ -867,6 +898,7 @@ def _sentry_patched_create_sync(f: "Any", *args: "Any", **kwargs: "Any") -> "Any
output_tokens,
cache_read_input_tokens,
cache_write_input_tokens,
output_tokens_reasoning,
) = _get_token_usage(result)

content_blocks = []
Expand All @@ -886,6 +918,7 @@ def _sentry_patched_create_sync(f: "Any", *args: "Any", **kwargs: "Any") -> "Any
output_tokens=output_tokens,
cache_read_input_tokens=cache_read_input_tokens,
cache_write_input_tokens=cache_write_input_tokens,
output_tokens_reasoning=output_tokens_reasoning,
content_blocks=content_blocks,
response_id=getattr(result, "id", None),
finish_reason=getattr(result, "stop_reason", None),
Expand Down Expand Up @@ -974,6 +1007,7 @@ async def _sentry_patched_create_async(
output_tokens,
cache_read_input_tokens,
cache_write_input_tokens,
output_tokens_reasoning,
) = _get_token_usage(result)

content_blocks = []
Expand All @@ -993,6 +1027,7 @@ async def _sentry_patched_create_async(
output_tokens=output_tokens,
cache_read_input_tokens=cache_read_input_tokens,
cache_write_input_tokens=cache_write_input_tokens,
output_tokens_reasoning=output_tokens_reasoning,
content_blocks=content_blocks,
response_id=getattr(result, "id", None),
finish_reason=getattr(result, "stop_reason", None),
Expand Down Expand Up @@ -1081,6 +1116,7 @@ def _set_streaming_output_data(
output_tokens=usage.output_tokens,
cache_read_input_tokens=usage.cache_read_input_tokens,
cache_write_input_tokens=usage.cache_write_input_tokens,
output_tokens_reasoning=usage.output_tokens_reasoning,
content_blocks=content_blocks,
response_id=response_id,
finish_reason=finish_reason,
Expand Down
Loading
Loading