Skip to content

Commit bdaaf70

Browse files
committed
added the stream class to
1 parent 63fdd7f commit bdaaf70

File tree

1 file changed

+27
-0
lines changed
  • agentops/instrumentation/anthropic/attributes

1 file changed

+27
-0
lines changed

agentops/instrumentation/anthropic/attributes/message.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,16 @@ def get_message_attributes(
6060
MessageStopEvent,
6161
MessageStreamEvent,
6262
)
63+
from anthropic import Stream
6364

6465
if isinstance(return_value, Message):
6566
attributes.update(get_message_response_attributes(return_value))
6667

6768
if hasattr(return_value, "content"):
6869
attributes.update(get_tool_attributes(return_value.content))
70+
elif isinstance(return_value, Stream):
71+
for event in return_value:
72+
attributes.update(get_stream_event_attributes(event))
6973
elif isinstance(return_value, MessageStreamEvent):
7074
attributes.update(get_stream_attributes(return_value))
7175
elif isinstance(
@@ -511,4 +515,27 @@ def get_stream_event_attributes(event: Any) -> AttributeMap:
511515
attributes[SpanAttributes.LLM_RESPONSE_FINISH_REASON] = stop_reason
512516
attributes[MessageAttributes.COMPLETION_FINISH_REASON.format(i=0)] = stop_reason
513517

518+
elif event_type == "RawMessageStartEvent":
519+
if hasattr(event, "message"):
520+
if hasattr(event.message, "usage"):
521+
usage = event.message.usage
522+
if hasattr(usage, "input_tokens"):
523+
input_tokens = usage.input_tokens
524+
attributes[SpanAttributes.LLM_USAGE_PROMPT_TOKENS] = input_tokens
525+
526+
if hasattr(usage, "output_tokens"):
527+
output_tokens = usage.output_tokens
528+
attributes[SpanAttributes.LLM_USAGE_COMPLETION_TOKENS] = output_tokens
529+
530+
if hasattr(usage, "input_tokens") and hasattr(usage, "output_tokens"):
531+
total_tokens = usage.input_tokens + usage.output_tokens
532+
attributes[SpanAttributes.LLM_USAGE_TOTAL_TOKENS] = total_tokens
533+
534+
elif event_type == "RawMessageDeltaEvent":
535+
if hasattr(event, "delta"):
536+
if hasattr(event.delta, "stop_reason"):
537+
stop_reason = event.delta.stop_reason
538+
attributes[SpanAttributes.LLM_RESPONSE_STOP_REASON] = stop_reason
539+
attributes[SpanAttributes.LLM_RESPONSE_FINISH_REASON] = stop_reason
540+
attributes[MessageAttributes.COMPLETION_FINISH_REASON.format(i=0)] = stop_reason
514541
return attributes

0 commit comments

Comments
 (0)