Class: LLM::Guard

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/guard.rb,
lib/llm/guard/loop.rb,
lib/llm/guard/null.rb

Overview

LLM::Guard is the superclass for context-level supervisors in llm.rb.

A guard is bound to a context and decides whether a tool call should be blocked before it runs. Each subclass implements #call and returns an LLM::Function::Return that closes the pending tool call, or nil when execution should continue. The guard is stamped onto the functions the context binds, so it runs whenever a task is spawned — including tool calls queued from a stream. The built-in implementation is LLM::Guard::Loop, which detects repeated tool-call patterns. LLM::Guard::Null is a no-op and the default.

LLM::Agent enables LLM::Guard::Loop by default through its wrapped context.

Direct Known Subclasses

Loop, Null

Defined Under Namespace

Classes: Loop, Null

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctx) ⇒ LLM::Guard

Parameters:



31
32
33
# File 'lib/llm/guard.rb', line 31

def initialize(ctx)
  @ctx = LLM::Agent === ctx ? ctx.instance_variable_get(:@ctx) : ctx
end

Instance Attribute Details

#ctxLLM::Context (readonly)

Returns:



26
27
28
# File 'lib/llm/guard.rb', line 26

def ctx
  @ctx
end

Instance Method Details

#call(function:, **opts) ⇒ LLM::Function::Return?

This method is abstract.

Returns A tool return that closes this function's pending call when it should be blocked, or nil when execution should continue.

Parameters:

  • function (LLM::Function)

    The pending function the guard is deciding about.

  • opts (Hash)

    Per-call options

Returns:

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

    A tool return that closes this function's pending call when it should be blocked, or nil when execution should continue.

Raises:

  • (NotImplementedError)


43
44
45
# File 'lib/llm/guard.rb', line 43

def call(function:, **opts)
  raise NotImplementedError
end