Getting Started
Build AI agents with Rails in minutes. This guide covers installation, configuration, and creating your first agent.
Prerequisites
- Ruby 3.0+
- Rails 7.0+
- API key for your chosen provider (OpenAI, Anthropic, or Ollama)
Installation
Add activeagent and your provider gem:
bundle add activeagentAdd your provider gem:
bundle add openaibundle add anthropicbundle add openai # Ollama uses OpenAI-compatible APIbundle add openai # OpenRouter uses OpenAI-compatible APIRun the install generator:
rails generate active_agent:installThis creates:
config/active_agent.yml- Provider configurationapp/agents/application_agent.rb- Base agent class
Configuration
Configure your provider in config/active_agent.yml:
openai:
service: "OpenAI"
access_token: <%= ENV['OPENAI_API_KEY'] %>
model: "gpt-4o-mini"See Configuration for environment-specific settings, multiple providers, and advanced options.
Quick Start
Test your setup with direct generation:
response = ApplicationAgent.prompt(message: "Hello, world!").generate_now
puts response.message
# => "Hello! How can I help you today?"This is perfect for testing your setup or quick experiments. For production apps, define custom agents with actions (shown below).
Your First Agent
Generate an Agent
Create an agent with the Rails generator:
rails generate active_agent:agent SupportAgent helpThis creates:
app/agents/support_agent.rb- Agent class with actionsapp/views/agents/support/help.text.erb- View template
Define the Agent
The generated agent defines actions as public methods:
class SupportAgent < ApplicationAgent
def help
@message = params[:message]
@user_id = params[:user_id]
prompt
end
endCreate a message template at app/views/agents/support/help.text.erb:
Help Request from User <%= @user_id %>
=====================================
The user needs assistance with: <%= @message %>
Please provide a helpful response.Use Your Agent
response = SupportAgent.with(
user_id: 123,
message: "How do I reset my password?"
).help.generate_now
puts response.message
# => "To reset your password, follow these steps..."Core Concepts
Three Invocation Patterns
ActiveAgent supports three ways to invoke agents:
1. Direct (testing/prototyping):
ApplicationAgent.prompt(message: "Hello").generate_now2. Parameterized (pass data to actions):
SupportAgent.with(user_id: 123, message: "Need help").help.generate_now3. Action-based (production pattern):
class SupportAgent < ApplicationAgent
def help
prompt(message: "User #{params[:user_id]} needs: #{params[:message]}")
end
endInstructions
Guide agent behavior with system instructions. Define them three ways:
In generate_with configuration:
class ApplicationAgent < ActiveAgent::Base
generate_with :openai, instructions: "You are a helpful assistant."
endIn an instructions.text template:
You are a support agent helping users with their questions.
Be concise, friendly, and provide clear solutions.Inline in the action:
def help
prompt(instructions: "Answer the user's question clearly and briefly")
endSee Instructions for complete details.
Execution
Generate responses synchronously or asynchronously:
# Synchronous
response = agent.generate_now
puts response.message
# Asynchronous (via Active Job)
agent.generate_later(wait: 5.minutes)See Generation for background jobs, callbacks, and response objects.
Next Steps
Core Features:
- Agents - Actions, callbacks, concerns, streaming
- Messages - Images, documents, conversation history
- Tools - Function calling and MCP integration
- Structured Output - Parse JSON with schemas
- Embeddings - Vector generation for semantic search
- Providers - OpenAI, Anthropic, Ollama, OpenRouter
Framework:
- Configuration - Environment settings, precedence
- Rails Integration - Generators, helpers, conventions
- Retries - Error handling and retry strategies
- Instrumentation - Logging and monitoring
- Testing - Test your agents with fixtures and VCR