Getting Started
From install to a working agent in a few minutes.
Docs home: Rails Agents · Gem: rails-agent-stack on RubyGems · Source: GitHub
After this guide
You will know how to:
- Install the gem and run the generator
- Configure API keys
- Define an agent and call
.run
1. Install
Add the gem to your Gemfile:
ruby
# Gemfile
gem "rails-agent-stack"bash
bundle install
bin/rails generate rails_agents:installThe gem name is rails-agent-stack; the Ruby API is still RailsAgents (require "rails_agents").
This creates:
config/initializers/rails_agents.rb— API keys onlyapp/agents/andapp/agents/tools/— where your agents live
2. Configure API keys
The initializer holds API keys only. Models are set on each agent.
ruby
# config/initializers/rails_agents.rb
RailsAgents.configure do |config|
config.openai_api_key = ENV["OPENAI_API_KEY"]
config.anthropic_api_key = ENV["ANTHROPIC_API_KEY"]
config.openrouter_api_key = ENV["OPENROUTER_API_KEY"]
config.grok_api_key = ENV["XAI_API_KEY"]
endSet only the keys you use. See Configuration for all options.
3. Define and run an agent
Every agent needs three things — nothing more to get started:
ruby
# app/agents/support_agent.rb
class SupportAgent < RailsAgents::Agent
provider :openai # :openai, :anthropic, :openrouter, :grok
model "gpt-4o-mini" # required — set per agent
description "Answers customer questions using docs and account data."
tools "SearchDocs", "LookupAccount" # optional
endbash
export OPENAI_API_KEY=sk-...
bin/rails consoleruby
result = SupportAgent.run("How do I reset my password?")
result.output # => the agent's reply
result.success # => true/falseThat's it. Add tools and skills when you need them — not before.
Free models via OpenRouter
Don't want to pay while learning? Use OpenRouter with a free model:
ruby
class CheapAgent < RailsAgents::Agent
provider :openrouter
model "meta-llama/llama-3.3-70b-instruct:free"
description "Answer briefly."
end