Rails Integration
Installation
Install ActiveAgent in your Rails application:
rails generate active_agent:installCreates:
config/active_agent.yml- Provider configurationapp/agents/application_agent.rb- Base agent class
Skip configuration file creation:
rails generate active_agent:install --skip-configConfiguration
Automatic Loading
ActiveAgent's Railtie automatically loads config/active_agent.yml when your Rails app starts. No manual initialization required.
# config/active_agent.yml
openai: &openai
service: "OpenAI"
access_token: <%= Rails.application.credentials.dig(:openai, :access_token) %>
model: "gpt-4o"
development:
openai:
<<: *openai
model: "gpt-4o-mini" # Cheaper model for development
production:
openai:
<<: *openaiSee Configuration for details on provider setup and configuration hierarchy.
Logger
ActiveAgent inherits Rails.logger automatically:
# config/environments/development.rb
config.log_level = :debug # Show all ActiveAgent instrumentation events
# config/environments/production.rb
config.log_level = :info # Only important operationsSee Instrumentation for event monitoring and custom logging.
Generators
Creating Agents
Generate an agent with actions:
rails generate active_agent:agent support respondCreates:
app/agents/support_agent.rb
app/views/agents/support/instructions.md
app/views/agents/support/respond.md.erb
test/docs/support_agent_test.rbMultiple actions:
rails generate active_agent:agent inventory search update deleteNamespaced agents:
rails generate active_agent:agent admin/user createTemplate Formats
Default markdown format:
rails generate active_agent:agent support respond
# Creates: respond.md.erbText format:
rails generate active_agent:agent support respond --format=text
# Creates: respond.text.erbJSON Response Formats
Generate with JSON schema validation:
rails generate active_agent:agent data parse --json-schemaCreates:
# app/agents/data_agent.rb
class DataAgent < ApplicationAgent
def parse
prompt(params[:message], response_format: :json_schema)
end
endAnd generates schema file:
app/views/agents/data/parse.jsonJSON object without schema validation:
rails generate active_agent:agent data parse --json-objectCreates agent with response_format: :json_object but no schema file.
Background Jobs
Active Job Integration
Queue generations for background processing with any Active Job backend (Sidekiq, Resque, etc.):
class SupportAgent < ApplicationAgent
generate_with :openai
def respond
prompt message: params[:message]
end
end
# Queue for background processing
SupportAgent.with(message: "Help!").respond.generate_later(queue: :agents)Custom Queue Configuration
class PriorityAgent < ApplicationAgent
self.generate_later_queue_name = :high_priority
endConfigure your Active Job adapter in config/application.rb:
config.active_job.queue_adapter = :sidekiqControllers
Integrate agents into controllers for user-facing features:
class ChatController < ApplicationController
def create
generation = ChatAgent
.with(message: params[:message], user_id: current_user.id)
.respond
.generate_later
render json: { status: "processing" }
end
endModels and Services
Call agents from existing business logic:
class Document < ApplicationRecord
after_create :extract_metadata
private
def extract_metadata
DataExtractionAgent
.with(content: body, document_id: id)
.extract
.generate_later
end
endRelated Documentation
- Configuration - Environment-specific settings and YAML configuration
- Instrumentation - Rails.logger integration and event monitoring