cd /news/artificial-intelligence/rubyllm-one-ruby-api-for-every-langu… · home › topics › artificial-intelligence › article
[ARTICLE · art-182960] src=letsruby.com ↗ pub= topic=artificial-intelligence verified=true sentiment=↑ positive

RubyLLM – one Ruby API for every language model

Carmine Paolino's RubyLLM gem, at version 2.2, provides a single Ruby interface for OpenAI, Anthropic, Gemini, Mistral, DeepSeek and local Ollama models, letting developers switch providers by changing one model name rather than rewriting code. The library returns answers as RubyLLM::Message objects with content, role and model_id, keeps conversation history across ask calls, supports system-prompt instructions via with_instructions and streams responses chunk by chunk, and tracks input and output tokens and per-message cost from a registry of almost 2000 models with context windows and prices. The walkthrough demonstrates the API against Chunky Mini, a rule-based demo model that speaks English and German and answers in OpenAI's JSON format, with sample pricing of $1 per million input tokens and $5 per million output tokens.

key takeaway

RubyLLM 2.2 wraps OpenAI, Anthropic, Gemini, Mistral, DeepSeek and Ollama behind one RubyLLM.chat API, so switching models is a one-name change while streaming chunks, system instructions and token/cost totals stay the same.

by read4 min views12 publishedDec 6, 2026
RubyLLM – one Ruby API for every language model
Image: source

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

── more in #artificial-intelligence 4 stories · sorted by recency
── more on @rubyllm 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/rubyllm-one-ruby-api…] indexed:0 read:4min 2026-12-06 · —