Class: Riffer::TokenUsage

Inherits:
Object
  • Object
show all
Defined in:
lib/riffer/token_usage.rb

Overview

Represents token usage data from an LLM API call.

Tracks input tokens, output tokens, and optional cache statistics.

token_usage = Riffer::TokenUsage.new(input_tokens: 100, output_tokens: 50)
token_usage.total_tokens  # => 150

combined = token_usage1 + token_usage2  # Combine multiple token usage objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_tokens:, output_tokens:, cache_creation_tokens: nil, cache_read_tokens: nil) ⇒ TokenUsage

– : (input_tokens: Integer, output_tokens: Integer, ?cache_creation_tokens: Integer?, ?cache_read_tokens: Integer?) -> void



28
29
30
31
32
33
# File 'lib/riffer/token_usage.rb', line 28

def initialize(input_tokens:, output_tokens:, cache_creation_tokens: nil, cache_read_tokens: nil)
  @input_tokens = input_tokens
  @output_tokens = output_tokens
  @cache_creation_tokens = cache_creation_tokens
  @cache_read_tokens = cache_read_tokens
end

Instance Attribute Details

#cache_creation_tokensObject (readonly)

Number of tokens written to cache (Anthropic-specific).



21
22
23
# File 'lib/riffer/token_usage.rb', line 21

def cache_creation_tokens
  @cache_creation_tokens
end

#cache_read_tokensObject (readonly)

Number of tokens read from cache (Anthropic-specific).



24
25
26
# File 'lib/riffer/token_usage.rb', line 24

def cache_read_tokens
  @cache_read_tokens
end

#input_tokensObject (readonly)

Number of tokens in the input/prompt.



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

def input_tokens
  @input_tokens
end

#output_tokensObject (readonly)

Number of tokens in the output/response.



18
19
20
# File 'lib/riffer/token_usage.rb', line 18

def output_tokens
  @output_tokens
end

Instance Method Details

#+(other) ⇒ Object

Combines two TokenUsage objects for cumulative tracking.

– : (Riffer::TokenUsage) -> Riffer::TokenUsage



47
48
49
50
51
52
53
54
# File 'lib/riffer/token_usage.rb', line 47

def +(other)
  Riffer::TokenUsage.new(
    input_tokens: input_tokens + other.input_tokens,
    output_tokens: output_tokens + other.output_tokens,
    cache_creation_tokens: add_nullable(cache_creation_tokens, other.cache_creation_tokens),
    cache_read_tokens: add_nullable(cache_read_tokens, other.cache_read_tokens)
  )
end

#to_hObject

Converts the token usage to a hash representation.

Cache tokens are omitted if nil.

– : () -> Hash[Symbol, Integer]



62
63
64
65
66
67
# File 'lib/riffer/token_usage.rb', line 62

def to_h
  hash = {input_tokens: input_tokens, output_tokens: output_tokens}
  hash[:cache_creation_tokens] = cache_creation_tokens if cache_creation_tokens
  hash[:cache_read_tokens] = cache_read_tokens if cache_read_tokens
  hash
end

#total_tokensObject

Returns the total number of tokens (input + output).

– : () -> Integer



39
40
41
# File 'lib/riffer/token_usage.rb', line 39

def total_tokens
  input_tokens + output_tokens
end