Class: Riffer::StreamEvents::TokenUsageDone

Inherits:
Base
  • Object
show all
Defined in:
lib/riffer/stream_events/token_usage_done.rb

Overview

Represents completion of token usage tracking during streaming.

Emitted when the LLM has finished and token usage data is available.

event.token_usage.input_tokens   # => 100
event.token_usage.output_tokens  # => 50
event.token_usage.total_tokens   # => 150

Instance Attribute Summary collapse

Attributes inherited from Base

#role

Instance Method Summary collapse

Constructor Details

#initialize(token_usage:, role: :assistant) ⇒ TokenUsageDone

Creates a new token usage done event.

token_usage

Riffer::TokenUsage - the token usage data

role

Symbol - the message role (defaults to :assistant)



21
22
23
24
# File 'lib/riffer/stream_events/token_usage_done.rb', line 21

def initialize(token_usage:, role: :assistant)
  super(role: role)
  @token_usage = token_usage
end

Instance Attribute Details

#token_usageObject (readonly)

The token usage data for this response.

Returns Riffer::TokenUsage.



15
16
17
# File 'lib/riffer/stream_events/token_usage_done.rb', line 15

def token_usage
  @token_usage
end

Instance Method Details

#to_hObject

Converts the event to a hash.

Returns Hash with :role and :token_usage keys.



29
30
31
# File 'lib/riffer/stream_events/token_usage_done.rb', line 29

def to_h
  {role: @role, token_usage: @token_usage.to_h}
end