Skip to content
← Back to activity

Immutable solution version

stream.get_final_message() does not return the correct usage of output_tokens

Version 1Sign in to save

Complete solution

How to fix 'stream.get_final_message() does not return the correct usage of output_tokens' in Anthropic, straight from the verified resolution in gh:anthropics/anthropic-sdk-python#424. Root cause plus what to change.

The problem

Issue anthropics/anthropic-sdk-python#424 (closed, 9 comments): After streaming, stream.getfinalmessage() usage showed outputtokens that did not match expectations - the reporter expected to sum messagestart and message_delta usage.

The verified resolution

Contributor RobertCraigie: the current behavior is correct - the final messagedelta usage already equals the total, identical to a non-streaming request's usage; the initial messagestart usage can be ignored. Reporter confirmed understanding.

Rule of thumb

for streamed Anthropic requests, use the final usage block as the total; do not sum messagestart + messagedelta.

Evidence

Asked in gh:anthropics/anthropic-sdk-python#424 (https://github.com/anthropics/anthropic-sdk-python/issues/424): stream.getfinalmessage() does not return the correct usage of outputtokens. The issue report: Issue anthropics/anthropic-sdk-python#424 (closed, 9 comments): After streaming, stream.getfinalmessage() usage showed outputtokens that did not match expectations - the reporter expected to sum messagestart and messagedelta usage. The verified answer: Contributor RobertCraigie: the current behavior is correct - the final messagedelta usage already equals the total, identical to a non-streaming request's usage; the initial messagestart usage can be ignored. Reporter confirmed understanding. Lesson: for streamed Anthropic requests, use the final usage block as the total; do not sum messagestart + messagedelta.

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_UW8InT325kBCziW0hSLc3w" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">stream.get_final_message() does not return the correct usage of output_tokens <span>— Vectle</span></a>