fix(anthropic): capture extended-thinking reasoning tokens - #746
Merged
Luca Forstner (lforst) merged 3 commits intoSep 7, 2026
Merged
Conversation
The direct Anthropic integration's `extract_anthropic_usage` read only flat top-level usage fields, so it never surfaced extended-thinking usage. Anthropic reports thinking tokens nested under `usage.output_tokens_details.thinking_tokens` (a subset of `output_tokens`), so `completion_reasoning_tokens` was always absent on thinking-model spans. Extract the nested field as the normalized `completion_reasoning_tokens` metric, matching the OpenAI/pydantic-ai integrations. Gated on `include_output` so the streaming input-only usage path is unaffected, and degrades cleanly when the field is absent (non-thinking requests, older SDKs). Cassette-backed regression test recorded against a real thinking response. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Luca Forstner (lforst)
approved these changes
Sep 7, 2026
Luca Forstner (lforst)
deleted the
fix/anthropic-direct-reasoning-tokens
branch
September 7, 2026 14:00
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
The direct Anthropic integration (
integrations/anthropic/) never records reasoning tokens.extract_anthropic_usage(_utils.py) only reads flat, top-level usage fields:But Anthropic reports extended-thinking usage nested, under
usage.output_tokens_details.thinking_tokens(a subset ofoutput_tokens). Socompletion_reasoning_tokenswas always absent on thinking-model spans.Flagged by the same customer as the sibling pydantic-ai bug ("the direct Anthropic integration records no reasoning tokens at all"). This is the follow-up called out in #745 / SDK-328.
Fix
Extract the nested
output_tokens_details.thinking_tokensas the normalizedcompletion_reasoning_tokensmetric, matching the OpenAI/pydantic-ai integrations.Verification
claude-haiku-4-5,thinkingenabled), usage came back withoutput_tokens_details.thinking_tokens: 168, and the span metric now matches.KeyError: 'completion_reasoning_tokens'.Blast radius
include_output, so the streaming input-only usage path (include_output=False) is unaffected — verified it stays absent there.output_tokens_detailsis absent: non-thinking requests don't return it, and older SDKs (e.g. anthropic 0.48.0) have no such field → skipped. Test is version-gated tolatest, same pattern as the existing prompt-cache-TTL tests (no0.48.0cassette needed).Relates to #745