Module: RubyCoded::Chat::State::ContextWindow

Included in:
RubyCoded::Chat::State
Defined in:
lib/ruby_coded/chat/state/context_window.rb

Overview

Resolves current model context window and computes live context usage based on the last turn’s server-reported ‘usage` block. This reflects the size of the prompt actually sent to the model on the most recent request, not the total tokens spent in the session.

Instance Method Summary collapse

Instance Method Details

#current_model_context_windowObject



14
15
16
17
18
19
20
21
22
# File 'lib/ruby_coded/chat/state/context_window.rb', line 14

def current_model_context_window
  model_name = @model
  return unless model_name

  codex_model = CodexModels.find(model_name)
  return codex_model.context_window if codex_model.respond_to?(:context_window)

  resolve_ruby_llm_context_window(model_name)
end

#session_context_tokens_usedObject



24
25
26
# File 'lib/ruby_coded/chat/state/context_window.rb', line 24

def session_context_tokens_used
  last_turn_context_tokens
end

#session_context_usage_percentageObject



28
29
30
31
32
33
34
# File 'lib/ruby_coded/chat/state/context_window.rb', line 28

def session_context_usage_percentage
  context_window = current_model_context_window
  return nil unless context_window.to_i.positive?

  percentage = ((session_context_tokens_used.to_f / context_window) * 100).round
  percentage.clamp(0, 100)
end