class RubyLLM::AgentTool
The Tool an Agent class becomes when it goes wherever tools go, so a model can hand work to it: a subagent. Agent::to_tool returns one, and Chat#with_tools and Agent::tools convert an Agent class themselves.
class ResearchAgent < RubyLLM::Agent description "Researches a question across the web and reports back" tools WebSearch, ReadPage end chat.with_tools(ResearchAgent).ask "What changed in Ruby 4.0?"
The tool is named after the agent (research_agent, or billing_research_agent for Billing::ResearchAgent) and takes a task, everything the agent needs written to stand alone, plus the agent’s ::inputs that ::to_tool was not given. Each call runs a fresh instance of the agent on the task, under the owner of the chat that called it, and returns the content of its last answer.
The agent lives only as long as its call, so it has to finish within it. When it stops to wait instead, for an approval, a question, or a task, the call raises ConfigurationError. To let a subagent wait for a person, give it a chat of its own, as the Agents guide shows.
Attributes
The Agent class this tool runs.
Public Class Methods
Source
# File lib/ruby_llm/agent_tool.rb, line 56 def tool_name Support::Utils.underscore(agent_class.name.gsub('::', '_')) end
Returns the agent’s name in snake case, such as research_agent.
Public Instance Methods
Source
# File lib/ruby_llm/agent_tool.rb, line 70 def execute(task:, **inputs) agent = self.class.agent_class.new(**@inputs, **inputs.slice(*self.class.input_parameters)) owner = Accounting::Usage.owner agent.with_owner(owner) unless owner.nil? message = agent.ask(task) raise ConfigurationError, stopped(agent) if agent.waiting? message&.content end
Runs a fresh instance of the agent on task, with the inputs given to Agent::to_tool and those the model chose, and returns the content of its last answer. Raises ConfigurationError, naming what the agent waits for, when it stops to wait instead of answering.