-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(anthropic): emit gen_ai.usage.reasoning_tokens from extended thinking #4462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dharaneeshexe-web
wants to merge
3
commits into
traceloop:main
Choose a base branch
from
dharaneeshexe-web:feat/anthropic-reasoning-tokens
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+227
−2
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
107c587
feat(anthropic): emit gen_ai.usage.reasoning_tokens from extended thi…
dharaneeshexe-web 7c821c7
fix(anthropic): keep latest streamed reasoning-token count, not sum
dharaneeshexe-web b1fb3a9
docs(anthropic): raise docstring coverage on reasoning-usage paths
dharaneeshexe-web File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
packages/opentelemetry-instrumentation-anthropic/tests/test_reasoning_usage.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| """Unit tests for reasoning-token usage extraction (gen_ai.usage.reasoning_tokens). | ||
|
|
||
| Covers the three paths that consume Anthropic `usage.output_tokens_details`: | ||
| the sync and async non-streaming `_set_token_usage` helpers in `__init__.py`, | ||
| and the streaming path in `streaming.py`. | ||
| """ | ||
|
|
||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
|
|
||
| from opentelemetry.instrumentation.anthropic import _aset_token_usage, _set_token_usage | ||
| from opentelemetry.instrumentation.anthropic.streaming import ( | ||
| _process_response_item, | ||
| _set_token_usage as _set_streaming_token_usage, | ||
| ) | ||
| from opentelemetry.semconv._incubating.attributes import ( | ||
| gen_ai_attributes as GenAIAttributes, | ||
| ) | ||
| from opentelemetry.semconv_ai import SpanAttributes | ||
|
|
||
| REASONING_TOKENS = SpanAttributes.GEN_AI_USAGE_REASONING_TOKENS | ||
|
|
||
|
|
||
| def _make_usage(**overrides): | ||
| usage = { | ||
| "input_tokens": 10, | ||
| "output_tokens": 25, | ||
| "cache_creation_input_tokens": 0, | ||
| "cache_read_input_tokens": 0, | ||
| "output_tokens_details": SimpleNamespace(reasoning_tokens=15), | ||
| } | ||
| usage.update(overrides) | ||
| return SimpleNamespace(**usage) | ||
|
|
||
|
|
||
| def _make_response(usage): | ||
| return SimpleNamespace( | ||
| usage=usage, | ||
| content=[SimpleNamespace(type="text", text="hi")], | ||
| stop_reason="end_turn", | ||
| model="claude-3-7-sonnet-20250219", | ||
| ) | ||
|
|
||
|
|
||
| def _finished_span_attributes(span_exporter): | ||
| return dict(span_exporter.get_finished_spans()[0].attributes) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def tracer(tracer_provider): | ||
| return tracer_provider.get_tracer("test-reasoning-usage") | ||
|
|
||
|
|
||
| def test_set_token_usage_emits_reasoning_tokens(tracer, span_exporter): | ||
| with tracer.start_as_current_span("test") as span: | ||
| _set_token_usage(span, None, {}, _make_response(_make_usage())) | ||
|
|
||
| attributes = _finished_span_attributes(span_exporter) | ||
| assert attributes[REASONING_TOKENS] == 15 | ||
| assert attributes[GenAIAttributes.GEN_AI_USAGE_OUTPUT_TOKENS] == 25 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_aset_token_usage_emits_reasoning_tokens(tracer, span_exporter): | ||
| with tracer.start_as_current_span("test") as span: | ||
| await _aset_token_usage(span, None, {}, _make_response(_make_usage())) | ||
|
|
||
| attributes = _finished_span_attributes(span_exporter) | ||
| assert attributes[REASONING_TOKENS] == 15 | ||
|
|
||
|
|
||
| def test_set_token_usage_omits_reasoning_when_details_absent(tracer, span_exporter): | ||
| with tracer.start_as_current_span("test") as span: | ||
| _set_token_usage( | ||
| span, None, {}, _make_response(_make_usage(output_tokens_details=None)) | ||
| ) | ||
|
|
||
| attributes = _finished_span_attributes(span_exporter) | ||
| assert REASONING_TOKENS not in attributes | ||
|
|
||
|
|
||
| def test_process_response_item_merges_streaming_reasoning_tokens(): | ||
| complete_response = {"events": [], "model": "", "usage": {}, "id": ""} | ||
|
|
||
| _process_response_item( | ||
| SimpleNamespace( | ||
| type="message_start", | ||
| message=SimpleNamespace( | ||
| model="claude-3-7-sonnet-20250219", | ||
| id="msg_1", | ||
| usage={ | ||
| "input_tokens": 10, | ||
| "output_tokens": 0, | ||
| "cache_creation_input_tokens": 0, | ||
| "cache_read_input_tokens": 0, | ||
| }, | ||
| ), | ||
| ), | ||
| complete_response, | ||
| ) | ||
| _process_response_item( | ||
| SimpleNamespace( | ||
| type="message_delta", | ||
| delta=SimpleNamespace(stop_reason="end_turn"), | ||
| usage={ | ||
| "output_tokens": 25, | ||
| "output_tokens_details": {"reasoning_tokens": 15}, | ||
| }, | ||
| ), | ||
| complete_response, | ||
| ) | ||
|
|
||
| usage = complete_response["usage"] | ||
| assert usage["output_tokens"] == 25 | ||
| assert usage["output_tokens_details"]["reasoning_tokens"] == 15 | ||
|
|
||
|
|
||
| def test_streaming_set_token_usage_emits_reasoning_tokens(tracer, span_exporter): | ||
| complete_response = { | ||
| "events": [], | ||
| "model": "claude-3-7-sonnet-20250219", | ||
| "usage": { | ||
| "input_tokens": 10, | ||
| "output_tokens": 25, | ||
| "output_tokens_details": {"reasoning_tokens": 15}, | ||
| }, | ||
| "id": "msg_1", | ||
| } | ||
|
|
||
| with tracer.start_as_current_span("test") as span: | ||
| _set_streaming_token_usage(span, complete_response, 10, 25) | ||
|
|
||
| attributes = _finished_span_attributes(span_exporter) | ||
| assert attributes[REASONING_TOKENS] == 15 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.