-
Notifications
You must be signed in to change notification settings - Fork 208
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
stream.get_final_message() does not return the correct usage of output_tokens #424
Comments
Adding a line under
|
Thanks for the report & the PR! |
Is there a timeline on this being fixed? What's the blocker on merging the PR? |
This was fixed 3 weeks ago: anthropics/anthropic-sdk-typescript#361 |
I’m wondering if this fix is mistaken. Shouldn’t the output_tokens be the sum of what appears in the message_start (output_token = 1) and the final message_delta (output_token = 6)? (there was no discussion on the PR so I’m adding this comment to where it seemed like the real discussion was happening) |
@krschacht what behavior are you seeing? |
@rattrayalex Right now the output_tokens is being set to the final output_token contained within the final message_delta. Is that the total output_token for the full stream? I’ve been assuming we need to sum up the first output_token and the final one to get the total, but the docs don’t actually specify the meaning of those fields. |
@krschacht I believe the current behaviour is correct, if you make a non-streaming request with the same inputs, the import asyncio
from anthropic import AsyncAnthropic
client = AsyncAnthropic()
async def main() -> None:
async with client.messages.stream(
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Say hello there!",
}
],
model="claude-3-opus-20240229",
) as stream:
await stream.until_done()
accumulated = await stream.get_final_message()
print("accumulated message: ", accumulated.to_json())
api_message = await client.messages.create(
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Say hello there!",
}
],
model="claude-3-opus-20240229",
)
print('api message', api_message.to_json())
asyncio.run(main()) Running this script gives this output for me:
I'll let the Anthropic team know that the docs weren't helpful here! Which docs were you looking at? |
Ohh, got it, so that final count is indeed the total and the initial one can be ignored. I wonder why the API includes the initial one then? Anyway, thanks for the quick reply! |
Like the title said,
stream.get_final_message()
always return theoutput_tokens
with the value of1
.running the exmaple code examples/messages_stream.py, the output would look like:
However the actual
output_tokens
should be6
according to the raw HTTP stream responseSo, is this a bug or a feature? I've seen someone in issue #417 using
stream.get_final_message()
to obtain the usage information. Ifoutput_tokens
always returns 1, this won't work properly, I guess.The text was updated successfully, but these errors were encountered: