RubyLLM – one Ruby API for every language model #
Claude, GPT, Gemini, Llama: language models write text, answer questions, sort emails and call your programs. Each provider has its own web API, with its own JSON. The gem RubyLLM by Carmine Paolino puts one Ruby interface in front of all of them – OpenAI, Anthropic, Gemini, Mistral, DeepSeek, local models with Ollama and many more. You write chat.ask "...", and switching to another model means changing one name, not your code.
A real model needs an API key, and every question costs a little money. So this page has a model of its own: Chunky Mini. It lives in this page, knows English and German and not much else – it is no neural network, just a few hundred lines of Ruby rules. But it answers in the JSON of OpenAI's API, so everything else here is the real RubyLLM 2.2: the chat, the messages, the tools, the tokens and the prices. The code works the same with Claude.
install_gem "ruby_llm"
require "ruby_llm"
require "chunky_llm" # Chunky Mini, the little model in this page
chat = RubyLLM.chat # the first time takes a few seconds
response = chat.ask "Hello!"
response.content
RubyLLM.chat starts a conversation with the default model, here Chunky Mini. ask sends a question and returns the answer as a RubyLLM::Message: content is the text, role who wrote it, model_id which model:
[response.role, response.model_id]
A chat remembers
A language model itself remembers nothing between two requests. The chat does: it keeps every message, and ask sends the whole conversation along each time. So the model can refer to what was said before:
chat.ask "My name is Ada."
answer = chat.ask "What's my name?"
puts answer.content
chat.messages.map(&:role)
Six messages: three questions (:user), three answers (:assistant). A new chat starts empty – and knows no Ada:
RubyLLM.chat.ask("What's my name?").content
Instructions
with_instructions tells the model how to behave for the whole chat: its role, its tone, the length of its answers. Instructions go first, as a message with the role :system (the “system prompt”):
pirate = RubyLLM.chat.with_instructions("You are a pirate. Answer in one short sentence.")
puts pirate.ask("Tell me about foxes.").content
pirate.messages.map(&:role)
Streaming
Big models take seconds for a long answer. So that nobody stares at an empty screen, they send it in pieces as they write. Give ask a block, and RubyLLM hands you each piece (a Chunk) as it arrives:
pieces = []
reply = RubyLLM.chat.ask("What is Ruby?") do |chunk|
pieces << chunk.content if chunk.content
end
puts "#{pieces.size} pieces, the first: #{pieces.first(4).inspect}"
reply.content
The last chunks carry no text, only why the model stopped and how many tokens it used – hence the if. In a terminal you would write print chunk.content and watch the words appear one by one; in a web app (Rails with Turbo Streams) each piece goes to the browser. At the end ask still returns the whole message.
Tokens and cost
Models read and write tokens: pieces of words, about four letters each in English. Providers charge per million tokens, input and output separately. Every message knows its count and its price, and the chat adds them up:
[response.tokens.input, response.tokens.output, response.cost.total, chat.cost.total]
Fractions of a cent – Chunky Mini's prices are pretend, but close to a small real model: $1 per million tokens in, $5 out. The input grows with every question, because the whole conversation goes along each time. RubyLLM takes the prices from its model registry, a list of almost 2000 models with their providers, context windows and prices:
mini = RubyLLM.models.find("chunky-mini")
puts "#{mini.name}: #{mini.context_window} tokens of context"
RubyLLM.models.by_provider(:anthropic).map(&:id).first(5)
gem install ruby_llm (or bundle add ruby_llm), then configure the provider whose key you have – the key comes from an environment variable, never from the code:
require "ruby_llm"
RubyLLM.configure do |config|
config.anthropic_api_key = ENV.fetch("ANTHROPIC_API_KEY")
end
chat = RubyLLM.chat(model: "claude-sonnet-5-5")
chat.ask("Hello!") { |chunk| print chunk.content }
Without a key and without costs: Ollama runs open models on your computer, config.ollama_api_base = "http://localhost:11434/v1" and RubyLLM.chat(model: "llama3.2", provider: :ollama). In Rails, bin/rails generate ruby_llm:install saves chats in the database and ruby_llm:chat_ui generates a complete chat page with streaming.
Task: Make a chat
helper whose instructions say it should answer briefly. Tell it your name (“My name is …”), then ask it what your name is. The cell yields the last reply.
install_gem "ruby_llm"
require "ruby_llm"
require "chunky_llm" # Chunky Mini, the little model in this page