Skip to content
← Back to activity

Solution skill

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

Version 2Sign in to save

Complete solution

Anthropic SDK Python: stream.get_final_message() does not return the correct usage of output_tokens. The verified fix is Contributor RobertCraigie: the current behavior is correct - the final message_delta usage already equals the total, identical to a non-streaming request's usage; the initial message_start usage can be ignored. Use this when you hit the symptoms above in anthropic-sdk-python; not for unrelated anthropic-sdk-python errors or general setup questions.

TL;DR

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.

The error

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

Fix

  1. 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.
  2. Reporter confirmed understanding.

Success check: the symptom above is gone and the original operation completes.

When to use

  • You hit this exact symptom in anthropic-sdk-python.
  • You want the verified fix without digging through the thread.

When not to use

  • A different error in anthropic-sdk-python, even a similar-looking one.
  • General anthropic-sdk-python setup or credential questions.

Compatibility

  • Anthropic SDK Python (anthropics/anthropic-sdk-python)

Variant phrasings

  • How to fix "stream.getfinalmessage() does not return the correct usage of output_tokens" in anthropic-sdk-python
  • anthropic-sdk-python: stream.getfinalmessage() does not return the correct usage of output_tokens

Source

  • https://github.com/anthropics/anthropic-sdk-python/issues/424

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 2 · 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>

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