Module: RubyCoded::Chat::State::MessageTokenTracking
- Included in:
- RubyCoded::Chat::State
- Defined in:
- lib/ruby_coded/chat/state/message_token_tracking.rb
Overview
Tracks per-message and per-model token usage counters.
Constant Summary collapse
- TOKEN_KEYS =
%i[input_tokens output_tokens thinking_tokens cached_tokens cache_creation_tokens].freeze
Instance Method Summary collapse
- #token_usage_by_model ⇒ Object
- #total_input_tokens ⇒ Object
- #total_output_tokens ⇒ Object
- #total_thinking_tokens ⇒ Object
- #update_last_message_tokens(model: nil, **token_counts) ⇒ Object
Instance Method Details
#token_usage_by_model ⇒ Object
38 39 40 41 42 |
# File 'lib/ruby_coded/chat/state/message_token_tracking.rb', line 38 def token_usage_by_model @mutex.synchronize do @token_usage_by_model.transform_values(&:dup) end end |
#total_input_tokens ⇒ Object
20 21 22 23 24 |
# File 'lib/ruby_coded/chat/state/message_token_tracking.rb', line 20 def total_input_tokens @mutex.synchronize do @messages.sum { || [:input_tokens] } end end |
#total_output_tokens ⇒ Object
26 27 28 29 30 |
# File 'lib/ruby_coded/chat/state/message_token_tracking.rb', line 26 def total_output_tokens @mutex.synchronize do @messages.sum { || [:output_tokens] } end end |
#total_thinking_tokens ⇒ Object
32 33 34 35 36 |
# File 'lib/ruby_coded/chat/state/message_token_tracking.rb', line 32 def total_thinking_tokens @mutex.synchronize do @messages.sum { || [:thinking_tokens] } end end |
#update_last_message_tokens(model: nil, **token_counts) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/ruby_coded/chat/state/message_token_tracking.rb', line 10 def (model: nil, **token_counts) @mutex.synchronize do return if @messages.empty? counts = TOKEN_KEYS.to_h { |key| [key, token_counts[key].to_i] } apply_token_counts(@messages.last, counts) accumulate_token_counts(model || @model, counts) end end |