Module: LLM::ActiveRecord::ActsAsAgent

Defined in:
lib/llm/active_record/acts_as_agent.rb

Overview

ActiveRecord integration for persisting LLM::Agent state.

This wrapper reuses the same record-backed runtime surface as ActsAsLLM, but builds an LLM::Agent instead of an LLM::Context. Agent defaults such as model, tools, schema, instructions, and concurrency are configured on the model class and forwarded to an internal agent subclass.

Defined Under Namespace

Modules: ClassMethods, Hooks, InstanceMethods

Constant Summary collapse

EMPTY_HASH =
LLM::ActiveRecord::ActsAsLLM::EMPTY_HASH
DEFAULT_USAGE_COLUMNS =
LLM::ActiveRecord::ActsAsLLM::DEFAULT_USAGE_COLUMNS
DEFAULTS =
LLM::ActiveRecord::ActsAsLLM::DEFAULTS

Instance Method Summary collapse

Instance Method Details

#acts_as_agent(options = EMPTY_HASH) { ... } ⇒ void

This method returns an undefined value.

Installs the ‘acts_as_agent` wrapper on an ActiveRecord model.

Parameters:

  • options (Hash) (defaults to: EMPTY_HASH)

Options Hash (options):

  • :format (Symbol)

    Storage format for the serialized agent state. Use ‘:string` for text columns, or `:json` / `:jsonb` for structured JSON columns with ActiveRecord JSON typecasting enabled.

  • :tracer (Proc, Symbol, LLM::Tracer, nil)

    Optional tracer, method name, or proc that resolves to one and is assigned through ‘llm.tracer = …` on the resolved provider.

Yields:

  • Evaluated in the model class after the wrapper is installed, so agent DSL methods such as ‘model`, `tools`, `schema`, `instructions`, and `concurrency` can be configured inline.



79
80
81
82
83
84
85
86
# File 'lib/llm/active_record/acts_as_agent.rb', line 79

def acts_as_agent(options = EMPTY_HASH, &block)
  options = DEFAULTS.merge(options)
  usage_columns = DEFAULT_USAGE_COLUMNS.merge(options[:usage_columns] || EMPTY_HASH)
  class_attribute :llm_agent_options, instance_accessor: false, default: DEFAULTS unless respond_to?(:llm_agent_options)
  self.llm_agent_options = options.merge(usage_columns: usage_columns.freeze).freeze
  extend Hooks
  class_exec(&block) if block
end