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. 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 https://rubyllm.com 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 https://ollama.com 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 helper = RubyLLM.chat.with instructions "..." tell it your name, then ask for it