ScavioScavio
ProductPricingDocs
Sign InGet Started
  1. Home
  2. Tutorials
  3. How to Build AI Agents in Rails with RubyLLM
Tutorial

How to Build AI Agents in Rails with RubyLLM

Build a Rails AI agent with RubyLLM and Scavio for live web search. Integrate inside Rails controllers or Active Job background workers.

Get Free API KeyAPI Docs

RubyLLM made 2026 the year of Ruby AI agents. Rails teams building agent features inside existing apps want HTTP-first integrations, not heavy SDKs. This tutorial wires RubyLLM plus Scavio into a Rails app with a working search tool.

Prerequisites

  • Rails 8+
  • Ruby 3.3+
  • A Scavio API key
  • The ruby_llm gem

Walkthrough

Step 1: Add the ruby_llm gem

RubyLLM ships an agent DSL with tool support.

Bash
bundle add ruby_llm
bundle add faraday

Step 2: Create a Scavio tool class

Any Ruby callable works as a RubyLLM tool.

# app/agents/tools/scavio_search.rb
class ScavioSearch
  include RubyLLM::Tool

  description 'Search the web across Google, Reddit, and YouTube.'
  param :query, type: :string

  def execute(query:)
    res = Faraday.post('https://api.scavio.dev/api/v1/search',
      { query: query }.to_json,
      { 'x-api-key' => ENV['SCAVIO_API_KEY'], 'Content-Type' => 'application/json' })
    JSON.parse(res.body).dig('organic_results') || []
  end
end

Step 3: Define the agent

RubyLLM agent DSL binds tools to the chat loop.

# app/agents/research_agent.rb
class ResearchAgent < RubyLLM::Agent
  model 'claude-sonnet-4-6'
  tools ScavioSearch
  system_prompt 'Research agent. Cite every claim.'
end

Step 4: Call the agent from a controller

Works in a Rails controller or a background job.

# app/controllers/research_controller.rb
class ResearchController < ApplicationController
  def create
    result = ResearchAgent.chat(params[:question])
    render json: { answer: result }
  end
end

Step 5: Wire a test route

Verify end-to-end in the browser.

# config/routes.rb
post '/research', to: 'research#create'
# curl: curl -XPOST localhost:3000/research -d 'question=what is rubyllm'

Python Example

Python
# Ruby tutorial; Python parity call:
import os, requests
API_KEY = os.environ['SCAVIO_API_KEY']
r = requests.post('https://api.scavio.dev/api/v1/search',
    headers={'x-api-key': API_KEY},
    json={'query': 'rubyllm 2026'})
print(r.json().get('organic_results', [])[:3])

JavaScript Example

JavaScript
// Ruby tutorial; JS parity call:
const API_KEY = process.env.SCAVIO_API_KEY;
const r = await fetch('https://api.scavio.dev/api/v1/search', {
  method: 'POST',
  headers: { 'x-api-key': API_KEY, 'Content-Type': 'application/json' },
  body: JSON.stringify({ query: 'rubyllm 2026' })
});
console.log(((await r.json()).organic_results || []).slice(0, 3));

Expected Output

JSON
Rails endpoint returns agent answers grounded in Scavio search results. Works inside synchronous controllers or Sidekiq jobs.

Related Tutorials

  • How to Add Web Search to opencode CLI
  • How to Build a RAG Chatbot for Regulated Industries
  • How to Give Hermes Agent Web Search Access

Frequently Asked Questions

Most developers complete this tutorial in 15 to 30 minutes. You will need a Scavio API key (free tier works) and a working Python or JavaScript environment.

Rails 8+. Ruby 3.3+. A Scavio API key. The ruby_llm gem. A Scavio API key gives you 50 free credits on signup.

Yes. The free tier includes 50 credits on signup, which is more than enough to complete this tutorial and prototype a working solution.

Scavio has a native LangChain package (langchain-scavio), an MCP server, and a plain REST API that works with any HTTP client. This tutorial uses the raw REST API, but you can adapt to your framework of choice.

Related Resources

Use Case

MCP Search Gateway for Multi-Agent Systems

Read more
Use Case

Pi Coding Agent Multi-Platform Search

Read more
Best Of

Best Search API for Ruby AI Agents in 2026

Read more
Best Of

Best Pi Coding Agent Search Extensions (May 2026)

Read more
Solution

Verify Search Results Before Agent Takes Action

Read more
Workflow

Hourly Multi-Agent Search Refresh Workflow

Read more

Start Building

Build a Rails AI agent with RubyLLM and Scavio for live web search. Integrate inside Rails controllers or Active Job background workers.

Get Free API KeyRead the Docs
ScavioScavio

Real-time search API for AI agents. Search every platform, not just Google.

Product

  • Features
  • Pricing
  • Dashboard
  • Affiliates

Developers

  • Documentation
  • API Reference
  • Quickstart
  • MCP Integration
  • Python SDK

Alternatives

  • Tavily Alternative
  • SerpAPI Alternative
  • Firecrawl Alternative
  • Exa Alternative

Tools

  • JSON Formatter
  • cURL to Code
  • Token Counter
  • All Tools

© 2026 Scavio. All rights reserved.

Featured on TAAFT
Terms of ServicePrivacy Policy