Class: Pikuri::Agent::ContextWindowDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/pikuri/agent/context_window_detector.rb

Overview

Resolves the model’s context-window cap from three sources, in order: an explicit override, the value ruby_llm reports for the model, or a llama.cpp /props probe. Returns nil if none of those produce a value.

Used by #initialize at construction time to feed Listener::TokenLog a cap it can render alongside the running context size (so the ctx=12.2k/32.0k line tells the operator how close the conversation is to the limit).

Precedence

  1. override — the Agent.new(context_window:) kwarg. Wins over everything; an explicit value is the operator’s statement of truth.

  2. ruby_llm_reportedRubyLLM::Model::Info#context_window from #chat‘s resolved model. Populated for models in ruby_llm’s bundled registry (OpenAI, Anthropic, Gemini, …); nil for custom local model ids that fall through to Model::Info.default.

  3. llama_probe_url — HTTP GET against llama.cpp’s non-standard /props endpoint. The server exposes the launched n_ctx at default_generation_settings.n_ctx there. Probed only when the first two are nil. Provider-specific to llama.cpp; the caller (typically bin/pikuri-chat) derives the right URL from its configured base.

Failure handling

The probe is best-effort. HTTP error, timeout, non-JSON body, or a missing/invalid n_ctx field all return nil and log one warn line via Pikuri.logger_for(‘ContextWindowDetector’). This is the CLAUDE.md “secondary to the loop” carve-out — a wedged or non-llama.cpp server should not abort agent construction over a cosmetic readout.

Constant Summary collapse

LOGGER =
Pikuri.logger_for('ContextWindowDetector')
OPEN_TIMEOUT =

Probe timeouts in seconds. Short on purpose: this runs synchronously during Agent.new and a wedged server should not stall startup noticeably.

2
READ_TIMEOUT =
2

Instance Method Summary collapse

Constructor Details

#initialize(override:, ruby_llm_reported:, llama_probe_url:) ⇒ ContextWindowDetector

Returns a new instance of ContextWindowDetector.

Parameters:

  • override (Integer, nil)

    explicit cap from the caller; wins if non-nil

  • ruby_llm_reported (Integer, nil)

    value off RubyLLM::Chat#model.context_window

  • llama_probe_url (String, nil)

    full URL to llama.cpp /props; nil or empty string skips the probe



57
58
59
60
61
# File 'lib/pikuri/agent/context_window_detector.rb', line 57

def initialize(override:, ruby_llm_reported:, llama_probe_url:)
  @override = override
  @ruby_llm_reported = ruby_llm_reported
  @llama_probe_url = llama_probe_url
end

Instance Method Details

#detectInteger?

Returns resolved cap, or nil if no source produced one.

Returns:

  • (Integer, nil)

    resolved cap, or nil if no source produced one



65
66
67
68
69
70
71
# File 'lib/pikuri/agent/context_window_detector.rb', line 65

def detect
  return @override if @override
  return @ruby_llm_reported if @ruby_llm_reported
  return nil if @llama_probe_url.nil? || @llama_probe_url.empty?

  probe_llama_cpp
end