Class: LLM::Guard::Loop
- Inherits:
-
LLM::Guard
- Object
- LLM::Guard
- LLM::Guard::Loop
- Defined in:
- lib/llm/guard/loop.rb
Overview
LLM::Guard::Loop is the built-in loop-detection guard. It detects when a context is repeating the same tool-call pattern instead of making progress, and returns an in-band LLM::Function::Return so LLM::Context can block the pending tool work.
Constant Summary collapse
- DEFAULT_THRESHOLD =
The default number of repeated tool-call patterns required before the guard intervenes.
3
Instance Attribute Summary
Attributes inherited from LLM::Guard
Instance Method Summary collapse
-
#call(function:, threshold: DEFAULT_THRESHOLD, **opts) ⇒ LLM::Function::Return?
Checks the current context for repeated tool-call patterns.
Methods inherited from LLM::Guard
Constructor Details
This class inherits a constructor from LLM::Guard
Instance Method Details
#call(function:, threshold: DEFAULT_THRESHOLD, **opts) ⇒ LLM::Function::Return?
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.
35 36 37 38 39 |
# File 'lib/llm/guard/loop.rb', line 35 def call(function:, threshold: DEFAULT_THRESHOLD, **opts) repetitions = detect(.to_a, threshold) return unless repetitions function.return(error: true, type: "guard_error", message: warning(repetitions)) end |