Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ def _process_response_item(item, complete_response):
event["finish_reason"] = item.delta.stop_reason
if item.usage:
if "usage" in complete_response:
item_output_tokens = dict(item.usage).get("output_tokens", 0)
existing_output_tokens = complete_response["usage"].get(
"output_tokens", 0
)
complete_response["usage"]["output_tokens"] = (
item_output_tokens + existing_output_tokens
)
# message_delta carries the running total for the whole message,
# not an increment, so it replaces the partial count reported by
# message_start rather than adding to it.
item_output_tokens = dict(item.usage).get("output_tokens")
if item_output_tokens is not None:
complete_response["usage"]["output_tokens"] = item_output_tokens
else:
complete_response["usage"] = dict(item.usage)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ def test_anthropic_message_streaming_legacy(
assert text_parts[0]["content"] == response_content
assert output_messages[-1]["role"] == "assistant"
assert anthropic_span.attributes[GenAIAttributes.GEN_AI_USAGE_INPUT_TOKENS] == 17
assert anthropic_span.attributes[GenAIAttributes.GEN_AI_USAGE_OUTPUT_TOKENS] == 171
assert (
anthropic_span.attributes[GenAIAttributes.GEN_AI_USAGE_OUTPUT_TOKENS]
+ anthropic_span.attributes[GenAIAttributes.GEN_AI_USAGE_INPUT_TOKENS]
Expand Down