An agentic runtime for system programmers - in Ruby.
An agentic runtime for system programmers - in Ruby.
The runtime supports concurrent and parallel tool execution.
15+ providers.
Parallel execution is provided by Ractor, and the
fork system call.
Concurrent execution is provided by Thread, Fiber, and
the 'async' gem (optional).
Works with ActiveRecord, and Sequel.
Works with and without Rails.
It requires nothing but Ruby's standard library.
It can be extended with optional, opt-in dependencies.
It ships builtin tools that are loaded on-demand.
All builtin tools and MCP/A2A tools are Ractor-safe by default.
The v17 series requires Ruby 3.4 or later.
This version works with older rubies and dependencies (ie: json v2).
gem install llm.rb -v 17.0.1
v22 is the recommended release.
Requires Ruby4+, JSON v3, and the latest versions of
ActiveRecord|Sequel (optional).
gem install llm.rb
r.uby.dev is the place to go if you have a question the README doesn't answer or want to see the runtime in action. Great way to learn about the runtime and see it in action at the same time. API docs also exists for people who prefer learning the traditional way. For everything else, there's the README.
The
LLM::Agent
class is the default high-level interface,
and it is recommended for most use-cases. It manages the tool loop
and provides configurable features on top of it. For example you can
manage the tool loop with a retry budget and a tool call budget -
alongside other features.
The runtime is designed to keep the tool loop alive and it will rescue exceptions. When an exception is encountered in a tool it is reported back to the model as an in-band error that allows the model to change course or retry with different parameters.
A tool call requires a tool return (or response), and the lack of one can corrupt the conversation and lead to API-level errors from a provider. But sometimes it is unavoidable (for example, via an interrupt or power loss) so the runtime automatically closes tool calls that fall into that category by telling the model the tool call(s) were cancelled.
The environment variable
$DEEPSEEK_API_KEY is assumed to have been set and reference
a valid DeepSeek API key, otherwise you can provide a key explicitly
(eg: LLM.deepseek(key: ENV["MYKEY"])) and you could swap
deepseek for any of the other 14 providers as well
(eg: llm = LLM.openai):
require "llm"
class GetRepo < LLM::Tool
name "get-repo"
description "discover clone URL for different repos"
parameter :repo, Enum["mruby", "llm.rb"], "the repo name"
required %i[repo]
def call(repo:)
{repo: repos[repo]}
end
private
def repos
{"mruby" => "https://github.com/mruby/mruby",
"llm.rb" => "https://github.com/r-uby-dev/llm"}
end
end
llm = LLM.deepseek
tools = [*LLM::Tool.git, GetRepo]
tracer = LLM::Tracer.enhanced(llm)
options = {concurrency: :ractor, stream: StringIO.new, tracer:, tools:}
agent = LLM::Agent.new(llm, options)
agent.talk "bare clone the #{['mruby', 'llm.rb'].sample} repo into /tmp/repo"
agent.talk "summarize recent changes from the repo"
puts options[:stream].string
An eval is like a unit test but for your agent. The runtime includes its own tiny eval framework and a runner that can run your evals. The evals emit both text and json reports that provide deep analysis of what an agent did, what a model said, and what tool calls were called.
An eval score starts at 1.0. The score method (which receives
an instance of LLM::Agent) can then reduce this score
from a perfect 1.0 all the way down to zero - based on the
results. What a "pass" means is up to you, you might require
1.0 for a pass, or 0.8 could be acceptable.
The criteria with which you score an agent and the logic behind it is also yours to define.
An eval is defined as a single directory with an eval.rb
file, one or more stepX.{txt,md} files, and any other
optional files that support the eval. Let's take a look
at one. The following example can be run with
llm.rb eval -p openai evals/simple-eval/ or checkout a
simulated eval that challenges the model to solve a
FreeBSD-specific problem: evals/freebsd-update-eval/.
This work was completed for the micro1 hackathon:
llm.rb eval -p openai -m gpt-6-sol evals/freebsd-update-eval/
llm.rb eval -p openai -m gpt-6-sol -n 3 evals/freebsd-update-eval/
Delete /tmp/file.txt
LLM::Eval.new do
set name: "simple-eval",
description: "can the agent delete a file?"
step "step1.txt"
setup do
require "fileutils"
FileUtils.touch "/tmp/file.txt"
end
score do |agent|
if File.exist?("/tmp/file.txt")
penalize!(by: 1.0, why: "/tmp/file.txt should have been deleted")
elsif agent.returns.size > 3
penalize!(by: 0.5, why: "too many tool calls for this eval")
end
end
configure do
agent do
##
# The agent being tested.
# Any subclass of {LLM::Agent} works.
# Also works with ActiveRecord|Sequel models.
LLM::Agent.new(
llm,
tools: [LLM::Tool::Ls, LLM::Tool::Pwd, LLM::Tool::Exec],
tracer:,
intercept:,
bug: the suite segfaults in GC marking through oj on Ruby 4.1
https://github.com/r-uby-dev/llm/issues/318
Labels: bug, ruby-bug, oj
parsers: the ractor examples are skipped under Oj
https://github.com/r-uby-dev/llm/issues/249
Labels: bug
No CONTRIBUTING document was found. Please check the README and open issues.