fumito-ito / AnthropicSwiftSDK

Yet another Anthropic API client for Swift.
Apache License 2.0
7 stars 3 forks source link

Question about getting usage from stream response #7

Closed hyperlink closed 7 months ago

hyperlink commented 7 months ago

Hello!

I have a question about and would appreciate any help.

According to Anthropic's docs the message delta is supposed to have a usage with the message_delta event.

event: message_delta data: {"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence":null, "usage":{"output_tokens": 15}}}

When I look the usage (on .messageDelta type) it appears to always be nil.

Any ideas why that would happen?

fumito-ito commented 7 months ago

Hi @hyperlink. Thanks for the feedback.

When I look the usage (on .messageDelta type) it appears to always be nil.

I have checked your point, and the Anthropic documentation explains that the response to a message_delta event has the following type.

event: message_delta
data: {"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence":null, "usage":{"output_tokens": 15}}}

However, when I checked the actual response, I found the following

event: message_delta
data: {"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence":null}, "usage":{"output_tokens": 15}}

The difference between the two is whether usage is under delta or not. Perhaps the documentation is wrong, but we are reserving our response at this time to contact Anthropic.

hyperlink commented 7 months ago

Yes @fumito-ito I also see the same the console. Appears to be a bug in their documentation!

hyperlink commented 7 months ago

I reported the issue to Anthropic's support. I have it fixed locally and I'll try to create a PR.

fumito-ito commented 7 months ago

Fixed by #8