Class: LlmCostTracker::TokenUsage

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#audio_input_tokensObject (readonly)

Returns the value of attribute audio_input_tokens

Returns:

  • (Object)

    the current value of audio_input_tokens



11
12
13
# File 'lib/llm_cost_tracker/token_usage.rb', line 11

def audio_input_tokens
  @audio_input_tokens
end

#audio_output_tokensObject (readonly)

Returns the value of attribute audio_output_tokens

Returns:

  • (Object)

    the current value of audio_output_tokens



11
12
13
# File 'lib/llm_cost_tracker/token_usage.rb', line 11

def audio_output_tokens
  @audio_output_tokens
end

#cache_read_input_tokensObject (readonly)

Returns the value of attribute cache_read_input_tokens

Returns:

  • (Object)

    the current value of cache_read_input_tokens



11
12
13
# File 'lib/llm_cost_tracker/token_usage.rb', line 11

def cache_read_input_tokens
  @cache_read_input_tokens
end

#cache_write_extended_input_tokensObject (readonly)

Returns the value of attribute cache_write_extended_input_tokens

Returns:

  • (Object)

    the current value of cache_write_extended_input_tokens



11
12
13
# File 'lib/llm_cost_tracker/token_usage.rb', line 11

def cache_write_extended_input_tokens
  @cache_write_extended_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



11
12
13
# File 'lib/llm_cost_tracker/token_usage.rb', line 11

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



11
12
13
# File 'lib/llm_cost_tracker/token_usage.rb', line 11

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



11
12
13
# File 'lib/llm_cost_tracker/token_usage.rb', line 11

def input_tokens
  @input_tokens
end

#output_tokensObject (readonly)

Returns the value of attribute output_tokens

Returns:

  • (Object)

    the current value of output_tokens



11
12
13
# File 'lib/llm_cost_tracker/token_usage.rb', line 11

def output_tokens
  @output_tokens
end

#total_tokensObject (readonly)

Returns the value of attribute total_tokens

Returns:

  • (Object)

    the current value of total_tokens



11
12
13
# File 'lib/llm_cost_tracker/token_usage.rb', line 11

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_extended_input_tokens: 0, audio_input_tokens: 0, audio_output_tokens: 0, total_tokens: nil, hidden_output_tokens: 0) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/llm_cost_tracker/token_usage.rb', line 53

def self.build(input_tokens:, output_tokens:, cache_read_input_tokens: 0,
               cache_write_input_tokens: 0, cache_write_extended_input_tokens: 0,
               audio_input_tokens: 0, audio_output_tokens: 0,
               total_tokens: nil, hidden_output_tokens: 0)
  input = non_negative_int(input_tokens)
  output = non_negative_int(output_tokens)
  cache_read = non_negative_int(cache_read_input_tokens)
  cache_write = non_negative_int(cache_write_input_tokens)
  cache_write_extended = non_negative_int(cache_write_extended_input_tokens)
  audio_input = non_negative_int(audio_input_tokens)
  audio_output = non_negative_int(audio_output_tokens)
  hidden_output = non_negative_int(hidden_output_tokens)
  calculated_total = input + cache_read + cache_write + cache_write_extended + audio_input + output + audio_output
  total = total_tokens.nil? ? calculated_total : [non_negative_int(total_tokens), calculated_total].max

  new(
    input_tokens: input,
    cache_read_input_tokens: cache_read,
    cache_write_input_tokens: cache_write,
    cache_write_extended_input_tokens: cache_write_extended,
    audio_input_tokens: audio_input,
    output_tokens: output,
    audio_output_tokens: audio_output,
    total_tokens: total,
    hidden_output_tokens: hidden_output
  )
end

.build_from_tokens(tokens) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/llm_cost_tracker/token_usage.rb', line 22

def self.build_from_tokens(tokens)
  return tokens if tokens.is_a?(self)
  raise ArgumentError, "tokens must be a Hash, got #{tokens.class}" unless tokens.respond_to?(:to_h)

  values = tokens.to_h.transform_keys { |key| key.to_s.to_sym }
  warn_on_unknown_keys(values)
  token_attributes = Billing::Components::TOKEN_PRICED.to_h do |component|
    [component.token_key, values.fetch(component.key, 0)]
  end

  build(
    **token_attributes,
    total_tokens: values[:total],
    hidden_output_tokens: values.fetch(:hidden_output, 0)
  )
end

.non_negative_int(value) ⇒ Object



49
50
51
# File 'lib/llm_cost_tracker/token_usage.rb', line 49

def self.non_negative_int(value)
  [value.to_i, 0].max
end

.warn_on_unknown_keys(values) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/llm_cost_tracker/token_usage.rb', line 39

def self.warn_on_unknown_keys(values)
  return if values.empty?
  return if values.keys.intersect?(KNOWN_TOKEN_KEYS)

  Logging.warn(
    "tokens hash contains no recognized keys (#{values.keys.inspect}); " \
    "expected one of #{KNOWN_TOKEN_KEYS.inspect}. Did you pass a raw provider response?"
  )
end