Class: LLM::LoopGuard
- Inherits:
-
Object
- Object
- LLM::LoopGuard
- Defined in:
- lib/llm/loop_guard.rb
Overview
LLM::LoopGuard is the built-in implementation of llm.rb’s ‘guard` capability.
A guard is a context-level supervisor for agentic execution. It can inspect the current runtime state and return a warning string when pending tool work should be blocked before the loop keeps going.
LLM::LoopGuard detects when a context is repeating the same tool-call pattern instead of making progress. It is directly inspired by General Intelligence Systems and its doom-loop detection approach.
The public interface is intentionally small:
-
‘call(ctx)` returns `nil` when no intervention is needed
-
‘call(ctx)` returns a warning string when pending tool execution should be blocked
LLM::Context can use that warning to return in-band LLM::GuardError tool errors, and LLM::Agent enables this guard by default through its wrapped context.
Constant Summary collapse
- DEFAULT_THRESHOLD =
The default number of repeated tool-call patterns required before the guard intervenes.
3
Instance Attribute Summary collapse
-
#threshold ⇒ Integer
readonly
Returns the repetition threshold.
Instance Method Summary collapse
-
#call(ctx) ⇒ String?
Checks the current context for repeated tool-call patterns.
-
#initialize(config = {}) ⇒ LoopGuard
constructor
A new instance of LoopGuard.
Constructor Details
#initialize(config = {}) ⇒ LoopGuard
Returns a new instance of LoopGuard.
41 42 43 |
# File 'lib/llm/loop_guard.rb', line 41 def initialize(config = {}) @threshold = config.fetch(:threshold, DEFAULT_THRESHOLD) end |
Instance Attribute Details
#threshold ⇒ Integer (readonly)
Returns the repetition threshold.
34 35 36 |
# File 'lib/llm/loop_guard.rb', line 34 def threshold @threshold end |
Instance Method Details
#call(ctx) ⇒ String?
Checks the current context for repeated tool-call patterns.
This method inspects assistant tool calls only. It reduces each call to a ‘[tool_name, arguments]` signature and checks whether the tail of the sequence is repeating.
56 57 58 59 |
# File 'lib/llm/loop_guard.rb', line 56 def call(ctx) repetitions = detect(ctx..to_a) repetitions ? warning(repetitions) : nil end |