Tools – giving the model hands
A language model only writes text. It cannot look up today's weather, an order in your database or the price in your shop – it knows only what it learned in training. Tools change that: you describe a Ruby class, the model may ask for it to be called, RubyLLM runs it and sends the result back, and the model writes its answer with it.
A tool is a subclass of RubyLLM::Tool with a description (what it is for – the model reads it), its parameters and an execute method:
install_gem "ruby_llm"
require "ruby_llm"
require "chunky_llm" # Chunky Mini, the little model in this page
class Weather < RubyLLM::Tool
description "Gets the current weather for a city"
parameter :city, description: "Name of the city, such as Bern"
def execute(city:)
reports = { "Bern" => "sunny, 21 °C", "Lugano" => "rain, 17 °C", "Basel" => "cloudy, 19 °C" }
reports.fetch(city) { { error: "No weather data for #{city}" } }
end
end
chat = RubyLLM.chat.with_tools(Weather)
chat.ask("What's the weather in Bern?").contentOur weather station is a hash, but execute could just as well ask a real weather service. What happened? The messages show it:
chat.messages.each do |m|
calls = m.tool_calls.to_h.values.map { |call| "#{call.name}(#{call.arguments})" }
puts "#{m.role.to_s.ljust(9)} #{calls.join(', ')} #{m.content}"
end
nilFour messages for one question:
user– the question.assistant– no text, but a tool call: the model asks forweatherwithcity: "Bern". It picked the tool by its description and took the argument from the question.tool– RubyLLM ranWeather#executeand put the result into the chat.assistant– with the result, the model writes the answer.
RubyLLM goes round this loop by itself until the model answers without asking for a tool. A question the tool does not fit gets no call:
chat.ask("Who created Ruby?").contentA model may call several tools at once, or one tool several times – here once per city:
RubyLLM.chat.with_tools(Weather).ask("What's the weather in Basel and Lugano?").contentWatching the loop
before_tool_call and after_tool_result are callbacks: blocks the chat calls around each tool. Apps use them to show “Checking the weather…” or to log what the model did:
log = []
watched = RubyLLM.chat.with_tools(Weather)
watched.before_tool_call { |call| log << "calls #{call.name} with #{call.arguments}" }
watched.after_tool_result { |result| log << "gets #{result.inspect}" }
watched.ask("Is it raining in Atlantis?")
logThere is no weather in Atlantis, so execute returns { error: ... }. That is the right way for an expected failure: the model reads the error and can tell the user, instead of the program crashing.
Parameters with types
Without a type a parameter is a string. type: :integer, :number or :boolean tell the model what to send – RubyLLM describes the tool as a JSON Schema:
class Dice < RubyLLM::Tool
description "Rolls a die and returns the number it shows"
parameter :sides, type: :integer, description: "How many sides the die has"
def execute(sides:)
rand(1..sides)
end
end
puts Dice.new.parameters_schema
RubyLLM.chat.with_tools(Dice).ask("Roll a die with 20 sides!").contentAgents
The same model, instructions and tools in many places of an app? Put them into an agent: a class that says how its chats are set up. new makes a chat that has it all:
class WeatherDesk < RubyLLM::Agent
instructions "You answer questions about the weather in one short sentence."
tools Weather
end
WeatherDesk.new.ask("Weather in Basel?").contentReal tools reach the world: a weather API with Faraday (which RubyLLM brings along), your Active Record models, a search. A tool acts with the model's arguments, so check them as you would a form's input – a tool that deletes or pays something gets requires_approval, and the chat waits for a person's yes. bin/rails generate ruby_llm:tool Weather writes a tool class, ruby_llm:agent an agent. RubyLLM also takes tools from MCP servers (RubyLLM::MCP) and lets a model run code in a sandbox (code_execution).
Shipping with the description “Calculates the shipping cost of a parcel from its weight” and a parameter :kilos of type :number; execute returns the price: 7 francs plus 2 francs per kilo. Then ask a chat with the tool: “What does shipping a 3 kg parcel cost?” The cell yields the reply.install_gem "ruby_llm"
require "ruby_llm"
require "chunky_llm" # Chunky Mini, the little model in this page
# class Shipping < RubyLLM::Tool
# description "..."
# parameter :kilos, type: :number, description: "..."
#
# def execute(kilos:)
# end
# end