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/agents/support_agent_test.rb
test/agents/previews/support_agent_preview.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
Browser previews
In development, open /rails/agents to list your agent previews. Choose an action to inspect the request as escaped Markdown, or download it as a .md file. A preview builds the request without calling the provider. It uses the agent's normal provider configuration.
The generator writes ActiveAgent::Preview subclasses under test/agents/previews. Existing previews under test/docs/previews are also discovered. Add another directory before the application initializes:
# config/application.rb
config.active_agent.preview_paths << Rails.root.join("lib/agent_previews").to_sGenerated preview methods pass their query parameters through with(params) to the agent. An action that needs parameters can be opened with, for example, /rails/agents/support_agent/respond?message=Hello. Namespaced previews use /rails/agents/admin/user_agent/create. Preview files reload with the application in development.
Set config.active_agent.show_previews = false to omit the routes. Previews default to disabled outside development. When enabled, the controller uses the same local-request restriction as Rails' own diagnostic pages. Setting config.consider_all_requests_local = true bypasses that restriction, so remote requests can access previews when config.active_agent.show_previews = true. Preview content can include agent instructions and interpolated parameters; keep config.consider_all_requests_local = false in any remotely accessible environment where previews are enabled.
The actionagent dashboard gem is not required.