Class: LLM::Guard::Loop

Inherits:
LLM::Guard show all
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.

Returns:

  • (Integer)
3

Instance Attribute Summary

Attributes inherited from LLM::Guard

#ctx

Instance Method Summary collapse

Methods inherited from LLM::Guard

#initialize

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.

Parameters:

  • function (LLM::Function)

    The pending function being decided about. Only its id and name are used to build the return.

  • threshold (Integer) (defaults to: DEFAULT_THRESHOLD)

    How many repeated tool-call patterns must appear at the tail of the sequence before the guard returns a warning.

  • opts (Hash)

    Additional per-call options (ignored).

Returns:

  • (LLM::Function::Return, nil)

    Returns a tool return that blocks this function's pending call, or nil when execution should continue.



35
36
37
38
39
# File 'lib/llm/guard/loop.rb', line 35

def call(function:, threshold: DEFAULT_THRESHOLD, **opts)
  repetitions = detect(messages.to_a, threshold)
  return unless repetitions
  function.return(error: true, type: "guard_error", message: warning(repetitions))
end