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

Instance Method Details

#last_turn_context_tokensObject

Live size of the model’s context window as reported by the last turn that carried usage info. Bridges (both API and Codex) are effectively stateless: every request re-sends the full history, so the server-reported ‘input_tokens` of the latest turn already represents the full live prompt. Summing across turns would double-count. We fall back to 0 when no turn has reported usage yet.



45
46
47
48
49
50
51
52
# File 'lib/ruby_coded/chat/state/message_token_tracking.rb', line 45

def last_turn_context_tokens
  @mutex.synchronize do
    last = @messages.reverse_each.find { |m| m[:input_tokens].to_i.positive? }
    return 0 unless last

    last[:input_tokens].to_i + last[:output_tokens].to_i + last[:thinking_tokens].to_i
  end
end

#token_usage_by_modelObject



54
55
56
57
58
# File 'lib/ruby_coded/chat/state/message_token_tracking.rb', line 54

def token_usage_by_model
  @mutex.synchronize do
    @token_usage_by_model.transform_values(&:dup)
  end
end

#total_input_tokensObject



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 { |message| message[:input_tokens] }
  end
end

#total_output_tokensObject



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 { |message| message[:output_tokens] }
  end
end

#total_thinking_tokensObject



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 { |message| message[: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 update_last_message_tokens(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