Module: LLM::ActiveRecord::ActsAsAgent

Included in:
ActiveRecord::Base
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.

The model implements set_provider (required) and set_context (optional), and the wrapper resolves them by name. A tracer is not one of those callbacks: give it to acts_as_agent as tracer:, or set it on the agent in the block with agent.tracer.

Defined Under Namespace

Modules: ClassMethods, Hooks, InstanceMethods

Instance Method Summary collapse

Instance Method Details

#acts_as_agent(options = EMPTY_HASH) {|agent| ... }

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. There is no set_tracer callback: the tracer is given here, or on the agent in the block.

  • :provider (Proc, Symbol, LLM::Provider) —

    Must resolve to an LLM::Provider instance for the current record.

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.

Yield Parameters:



100
101
102
103
104
105
106
# File 'lib/llm/active_record/acts_as_agent.rb', line 100

def acts_as_agent(options = EMPTY_HASH, &block)
  options = DEFAULTS.merge(options)
  class_attribute :llm_plugin_options, instance_accessor: false, default: DEFAULTS unless respond_to?(:llm_plugin_options)
  self.llm_plugin_options = options.freeze
  extend Hooks
  block_given? ? class_exec(agent, &block) : nil
end