Module: RubyLLM::Agents::CallbacksDSL

Included in:
Base
Defined in:
lib/ruby_llm/agents/core/base/callbacks.rb

Overview

DSL and execution support for before_call/after_call hooks

Provides callbacks that run before and after the LLM call, allowing custom preprocessing (redaction, moderation, validation) and postprocessing (logging, transformation) logic.

Examples:

Using callbacks

class MyAgent < ApplicationAgent
  before_call :sanitize_input
  after_call :log_response

  # Or with blocks
  before_call { |context| context.params[:timestamp] = Time.current }
  after_call { |context, response| notify(response) }

  private

  def sanitize_input(context)
    # Mutate context as needed
    # Raise to block execution
  end

  def log_response(context, response)
    Rails.logger.info("Response: #{response}")
  end
end

Instance Method Summary collapse

Instance Method Details

#after {|context, response| ... } ⇒ void

This method returns an undefined value.

Simplified alias for after_call (block-only)

This is the preferred method in the simplified DSL.

Examples:

after { |ctx, result| Rails.logger.info("Completed: #{result}") }
after { |ctx, result| notify_slack(result) if result.confidence < 0.5 }

Yields:

  • (context, response)

    Block to execute after the LLM call

Yield Parameters:

  • context (Pipeline::Context)

    The execution context

  • response (Object)

    The LLM response



107
108
109
# File 'lib/ruby_llm/agents/core/base/callbacks.rb', line 107

def after(&block)
  after_call(&block)
end

#after_call(method_name = nil) {|context, response| ... } ⇒ void

This method returns an undefined value.

Add a callback to run after the LLM call

Callbacks receive the pipeline context and the response. Return value is ignored.

Examples:

With method name

after_call :log_response

With block

after_call { |context, response| notify_completion(response) }

Parameters:

  • method_name (Symbol, nil) (defaults to: nil)

    Instance method to call

Yields:

  • (context, response)

    Block to execute

Yield Parameters:

  • context (Pipeline::Context)

    The execution context

  • response (Object)

    The LLM response



73
74
75
76
# File 'lib/ruby_llm/agents/core/base/callbacks.rb', line 73

def after_call(method_name = nil, &block)
  @callbacks ||= {before: [], after: []}
  @callbacks[:after] << (block || method_name)
end

#before {|context| ... } ⇒ void

This method returns an undefined value.

Simplified alias for before_call (block-only)

This is the preferred method in the simplified DSL.

Examples:

before { |ctx| ctx.params[:timestamp] = Time.current }
before { |ctx| validate_input!(ctx.params[:query]) }

Yields:

  • (context)

    Block to execute before the LLM call

Yield Parameters:



90
91
92
# File 'lib/ruby_llm/agents/core/base/callbacks.rb', line 90

def before(&block)
  before_call(&block)
end

#before_call(method_name = nil) {|context| ... } ⇒ void

This method returns an undefined value.

Add a callback to run before the LLM call

Callbacks receive the pipeline context and can:

  • Mutate the context (params, prompts, etc.)

  • Raise an exception to block execution

  • Return value is ignored

Examples:

With method name

before_call :validate_input

With block

before_call { |context| context.params[:sanitized] = true }

Parameters:

  • method_name (Symbol, nil) (defaults to: nil)

    Instance method to call

Yields:

  • (context)

    Block to execute

Yield Parameters:



51
52
53
54
# File 'lib/ruby_llm/agents/core/base/callbacks.rb', line 51

def before_call(method_name = nil, &block)
  @callbacks ||= {before: [], after: []}
  @callbacks[:before] << (block || method_name)
end

#callbacksHash

Get all registered callbacks

Returns:

  • (Hash)

    Hash with :before and :after arrays



114
115
116
# File 'lib/ruby_llm/agents/core/base/callbacks.rb', line 114

def callbacks
  @callbacks ||= {before: [], after: []}
end