Class: Pikuri::Agent::ContextWindowDetector
- Inherits:
-
Object
- Object
- Pikuri::Agent::ContextWindowDetector
- 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
-
override— the Agent.new(context_window:) kwarg. Wins over everything; an explicit value is the operator’s statement of truth. -
ruby_llm_reported— RubyLLM::Model::Info#context_window from #chat‘s resolved model. Populated for models in ruby_llm’s bundled registry (OpenAI, Anthropic, Gemini, …);nilfor custom local model ids that fall through toModel::Info.default. -
llama_probe_url— HTTP GET against llama.cpp’s non-standard/propsendpoint. The server exposes the launchedn_ctxatdefault_generation_settings.n_ctxthere. Probed only when the first two arenil. Provider-specific to llama.cpp; the caller (typicallybin/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.newand a wedged server should not stall startup noticeably. 2- READ_TIMEOUT =
2
Instance Method Summary collapse
-
#detect ⇒ Integer?
Resolved cap, or
nilif no source produced one. -
#initialize(override:, ruby_llm_reported:, llama_probe_url:) ⇒ ContextWindowDetector
constructor
A new instance of ContextWindowDetector.
Constructor Details
#initialize(override:, ruby_llm_reported:, llama_probe_url:) ⇒ ContextWindowDetector
Returns a new instance of ContextWindowDetector.
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
#detect ⇒ Integer?
Returns 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 |