|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Agents |
| 4 | +nav_order: 6 |
| 5 | +description: Define reusable agent configurations once and use them anywhere |
| 6 | +--- |
| 7 | + |
| 8 | +# {{ page.title }} |
| 9 | +{: .d-inline-block .no_toc } |
| 10 | + |
| 11 | +New in 1.12 |
| 12 | +{: .label .label-green } |
| 13 | + |
| 14 | +{{ page.description }} |
| 15 | +{: .fs-6 .fw-300 } |
| 16 | + |
| 17 | +## Table of contents |
| 18 | +{: .no_toc .text-delta } |
| 19 | + |
| 20 | +1. TOC |
| 21 | +{:toc} |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +After reading this guide, you will know: |
| 26 | + |
| 27 | +* Why agents exist and when to use them |
| 28 | +* How to define an agent with a class-based DSL |
| 29 | +* How to instantiate and use agents in your app |
| 30 | +* How to keep tools and instructions centralized |
| 31 | + |
| 32 | +## What Are Agents? |
| 33 | + |
| 34 | +Agents are a DSL that lets you define a chat configuration once and reuse it everywhere. They make agent definitions feel like first-class objects in your app: readable, discoverable, and easy to instantiate. |
| 35 | + |
| 36 | +This is especially helpful in Rails apps where you keep agent classes in `app/agents` and want to avoid re-specifying the same model, tools, and instructions every time you load a chat from the database. But it also works great in scripts, services, and background jobs—anywhere you want a clean, named place to put the “shape” of an agent. |
| 37 | + |
| 38 | +Instead of rebuilding configuration for each chat instance, you define it once on the agent class and instantiate it when needed. |
| 39 | + |
| 40 | +## Defining an Agent |
| 41 | + |
| 42 | +Create a class that inherits from `RubyLLM::Agent` and declare its configuration: |
| 43 | + |
| 44 | +```ruby |
| 45 | +# app/agents/chat_agent.rb |
| 46 | +class ChatAgent < RubyLLM::Agent |
| 47 | + model "gpt-5", provider: :azure, assume_model_exists: true |
| 48 | + tools MyTool, ThatTool |
| 49 | + instructions "Be awesome" |
| 50 | + temperature 0.2 |
| 51 | + thinking effort: :none |
| 52 | + params max_output_tokens: 256 |
| 53 | + headers "X-Request-Id" => "chat-agent" |
| 54 | + schema MySchema |
| 55 | +end |
| 56 | +``` |
| 57 | + |
| 58 | +Each class macro maps to the equivalent `RubyLLM.chat` or `Chat#with_*` setting. The values are applied when you instantiate the agent. |
| 59 | + |
| 60 | +## Using an Agent |
| 61 | + |
| 62 | +Instantiate the class and ask questions just like a normal chat: |
| 63 | + |
| 64 | +```ruby |
| 65 | +agent = ChatAgent.new |
| 66 | +response = agent.ask "hello" |
| 67 | + |
| 68 | +puts response.content |
| 69 | +``` |
| 70 | + |
| 71 | +You can also override any `RubyLLM.chat` arguments per instance: |
| 72 | + |
| 73 | +```ruby |
| 74 | +agent = ChatAgent.new(model: "gpt-5-mini") |
| 75 | +agent.ask "Use the faster model for this request." |
| 76 | +``` |
| 77 | + |
| 78 | +## Why This Helps in Rails |
| 79 | + |
| 80 | +When you load a chat from the database, you often need to reapply instructions and tools to get consistent behavior. Agents let you keep that configuration in one place: |
| 81 | + |
| 82 | +```ruby |
| 83 | +# app/agents/support_agent.rb |
| 84 | +class SupportAgent < RubyLLM::Agent |
| 85 | + model "{{ site.models.default_chat }}" |
| 86 | + instructions "You are a helpful support agent." |
| 87 | + tools SearchDocs, LookupAccount |
| 88 | +end |
| 89 | +``` |
| 90 | + |
| 91 | +That way, every part of your app uses the same settings without duplicating logic in controllers, jobs, or model callbacks. |
| 92 | + |
| 93 | +## When to Use Agents vs `RubyLLM.chat` |
| 94 | + |
| 95 | +Use `RubyLLM.chat` when you want a one-off conversation or quick, inline configuration: |
| 96 | + |
| 97 | +```ruby |
| 98 | +chat = RubyLLM.chat(model: "{{ site.models.default_chat }}") |
| 99 | +chat.with_instructions "Explain this clearly." |
| 100 | +``` |
| 101 | + |
| 102 | +Use agents when you want a named, reusable definition that you can instantiate consistently across your app: |
| 103 | + |
| 104 | +```ruby |
| 105 | +class SupportAgent < RubyLLM::Agent |
| 106 | + model "{{ site.models.default_chat }}" |
| 107 | + instructions "You are a helpful support agent." |
| 108 | + tools SearchDocs, LookupAccount |
| 109 | +end |
| 110 | +``` |
| 111 | + |
| 112 | +Think of `RubyLLM.chat` as the ad-hoc interface and agents as the reusable, shareable interface. |
| 113 | + |
| 114 | +## Next Steps |
| 115 | + |
| 116 | +* Learn about [Chat Basics]({% link _core_features/chat.md %}) |
| 117 | +* Explore [Tools]({% link _core_features/tools.md %}) |
| 118 | +* Review [Rails Integration]({% link _advanced/rails.md %}) |
0 commit comments