Class: LlmCostTracker::TokenUsage
- Inherits:
-
Data
- Object
- Data
- LlmCostTracker::TokenUsage
- Defined in:
- lib/llm_cost_tracker/token_usage.rb
Instance Attribute Summary collapse
-
#audio_input_tokens ⇒ Object
readonly
Returns the value of attribute audio_input_tokens.
-
#audio_output_tokens ⇒ Object
readonly
Returns the value of attribute audio_output_tokens.
-
#cache_read_input_tokens ⇒ Object
readonly
Returns the value of attribute cache_read_input_tokens.
-
#cache_write_extended_input_tokens ⇒ Object
readonly
Returns the value of attribute cache_write_extended_input_tokens.
-
#cache_write_input_tokens ⇒ Object
readonly
Returns the value of attribute cache_write_input_tokens.
-
#hidden_output_tokens ⇒ Object
readonly
Returns the value of attribute hidden_output_tokens.
-
#image_input_tokens ⇒ Object
readonly
Returns the value of attribute image_input_tokens.
-
#image_output_tokens ⇒ Object
readonly
Returns the value of attribute image_output_tokens.
-
#input_tokens ⇒ Object
readonly
Returns the value of attribute input_tokens.
-
#output_tokens ⇒ Object
readonly
Returns the value of attribute output_tokens.
-
#total_tokens ⇒ Object
readonly
Returns the value of attribute total_tokens.
Class Method Summary collapse
- .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, image_input_tokens: 0, image_output_tokens: 0, total_tokens: nil, hidden_output_tokens: 0) ⇒ Object
- .build_from_tokens(tokens) ⇒ Object
- .non_negative_int(value) ⇒ Object
- .warn_on_unknown_keys(values) ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#audio_input_tokens ⇒ Object (readonly)
Returns the value of attribute 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_tokens ⇒ Object (readonly)
Returns the value of attribute 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_tokens ⇒ Object (readonly)
Returns the value of attribute 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_tokens ⇒ Object (readonly)
Returns the value of attribute 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_tokens ⇒ Object (readonly)
Returns the value of attribute 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_tokens ⇒ Object (readonly)
Returns the value of attribute hidden_output_tokens
11 12 13 |
# File 'lib/llm_cost_tracker/token_usage.rb', line 11 def hidden_output_tokens @hidden_output_tokens end |
#image_input_tokens ⇒ Object (readonly)
Returns the value of attribute image_input_tokens
11 12 13 |
# File 'lib/llm_cost_tracker/token_usage.rb', line 11 def image_input_tokens @image_input_tokens end |
#image_output_tokens ⇒ Object (readonly)
Returns the value of attribute image_output_tokens
11 12 13 |
# File 'lib/llm_cost_tracker/token_usage.rb', line 11 def image_output_tokens @image_output_tokens end |
#input_tokens ⇒ Object (readonly)
Returns the value of attribute input_tokens
11 12 13 |
# File 'lib/llm_cost_tracker/token_usage.rb', line 11 def input_tokens @input_tokens end |
#output_tokens ⇒ Object (readonly)
Returns the value of attribute output_tokens
11 12 13 |
# File 'lib/llm_cost_tracker/token_usage.rb', line 11 def output_tokens @output_tokens end |
#total_tokens ⇒ Object (readonly)
Returns the value of attribute 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, image_input_tokens: 0, image_output_tokens: 0, total_tokens: nil, hidden_output_tokens: 0) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/llm_cost_tracker/token_usage.rb', line 59 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, image_input_tokens: 0, image_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) image_input = non_negative_int(image_input_tokens) image_output = non_negative_int(image_output_tokens) hidden_output = non_negative_int(hidden_output_tokens) calculated_total = input + cache_read + cache_write + cache_write_extended + audio_input + image_input + output + audio_output + image_output total = total_tokens ? [non_negative_int(total_tokens), calculated_total].max : calculated_total 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, image_input_tokens: image_input, output_tokens: output, audio_output_tokens: audio_output, image_output_tokens: image_output, total_tokens: total, hidden_output_tokens: hidden_output ) end |
.build_from_tokens(tokens) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/llm_cost_tracker/token_usage.rb', line 28 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
55 56 57 |
# File 'lib/llm_cost_tracker/token_usage.rb', line 55 def self.non_negative_int(value) [value.to_i, 0].max end |
.warn_on_unknown_keys(values) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/llm_cost_tracker/token_usage.rb', line 45 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 |
Instance Method Details
#priced_quantities ⇒ Object
24 25 26 |
# File 'lib/llm_cost_tracker/token_usage.rb', line 24 def priced_quantities Billing::Components::TOKEN_PRICED.to_h { |component| [component.key, public_send(component.token_key)] } end |