Class: LlmCostTracker::TokenUsage

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

Constant Summary collapse

STORED_KEYS =
TokenUsage.members.freeze
COMPONENT_TOKEN_KEYS =
(TokenUsage.members - %i[total_tokens]).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cache_read_input_tokensObject (readonly)

Returns the value of attribute cache_read_input_tokens

Returns:

  • (Object)

    the current value of cache_read_input_tokens



6
7
8
# File 'lib/llm_cost_tracker/token_usage.rb', line 6

def cache_read_input_tokens
  @cache_read_input_tokens
end

#cache_write_1h_input_tokensObject (readonly)

Returns the value of attribute cache_write_1h_input_tokens

Returns:

  • (Object)

    the current value of cache_write_1h_input_tokens



6
7
8
# File 'lib/llm_cost_tracker/token_usage.rb', line 6

def cache_write_1h_input_tokens
  @cache_write_1h_input_tokens
end

#cache_write_input_tokensObject (readonly)

Returns the value of attribute cache_write_input_tokens

Returns:

  • (Object)

    the current value of cache_write_input_tokens



6
7
8
# File 'lib/llm_cost_tracker/token_usage.rb', line 6

def cache_write_input_tokens
  @cache_write_input_tokens
end

#hidden_output_tokensObject (readonly)

Returns the value of attribute hidden_output_tokens

Returns:

  • (Object)

    the current value of hidden_output_tokens



6
7
8
# File 'lib/llm_cost_tracker/token_usage.rb', line 6

def hidden_output_tokens
  @hidden_output_tokens
end

#input_tokensObject (readonly)

Returns the value of attribute input_tokens

Returns:

  • (Object)

    the current value of input_tokens



6
7
8
# File 'lib/llm_cost_tracker/token_usage.rb', line 6

def input_tokens
  @input_tokens
end

#output_tokensObject (readonly)

Returns the value of attribute output_tokens

Returns:

  • (Object)

    the current value of output_tokens



6
7
8
# File 'lib/llm_cost_tracker/token_usage.rb', line 6

def output_tokens
  @output_tokens
end

#total_tokensObject (readonly)

Returns the value of attribute total_tokens

Returns:

  • (Object)

    the current value of total_tokens



6
7
8
# File 'lib/llm_cost_tracker/token_usage.rb', line 6

def total_tokens
  @total_tokens
end

Class Method Details

.build(input_tokens:, output_tokens:, cache_read_input_tokens: 0, cache_write_input_tokens: 0, cache_write_1h_input_tokens: 0, total_tokens: nil, hidden_output_tokens: 0) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/llm_cost_tracker/token_usage.rb', line 15

def self.build(input_tokens:, output_tokens:, cache_read_input_tokens: 0,
               cache_write_input_tokens: 0, cache_write_1h_input_tokens: 0,
               total_tokens: nil, hidden_output_tokens: 0)
  input = input_tokens.to_i
  output = output_tokens.to_i
  cache_read = cache_read_input_tokens.to_i
  cache_write = cache_write_input_tokens.to_i
  cache_write_1h = cache_write_1h_input_tokens.to_i
  calculated_total = input + cache_read + cache_write + cache_write_1h + output
  total = total_tokens.nil? ? calculated_total : [total_tokens.to_i, calculated_total].max

  new(
    input_tokens: input,
    cache_read_input_tokens: cache_read,
    cache_write_input_tokens: cache_write,
    cache_write_1h_input_tokens: cache_write_1h,
    output_tokens: output,
    total_tokens: total,
    hidden_output_tokens: hidden_output_tokens.to_i
  )
end

.from_hash(attributes) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/llm_cost_tracker/token_usage.rb', line 37

def self.from_hash(attributes)
  attributes = attributes.to_h.symbolize_keys
  values = TokenUsage::COMPONENT_TOKEN_KEYS.to_h { |key| [key, attributes[key]] }
  build(
    **values,
    total_tokens: attributes[:total_tokens]
  )
end

Instance Method Details

#price_quantitiesObject



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

def price_quantities
  {
    input: input_tokens,
    cache_read_input: cache_read_input_tokens,
    cache_write_input: cache_write_input_tokens,
    cache_write_1h_input: cache_write_1h_input_tokens,
    output: output_tokens
  }
end

#stored_attributesObject



56
57
58
# File 'lib/llm_cost_tracker/token_usage.rb', line 56

def stored_attributes
  to_h.slice(*self.class::STORED_KEYS)
end

#to_hObject



60
61
62
# File 'lib/llm_cost_tracker/token_usage.rb', line 60

def to_h
  super.compact
end