{"slug": "rubyllm-one-ruby-api-for-every-language-model", "title": "RubyLLM – one Ruby API for every language model", "summary": "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.", "body_md": "## RubyLLM – one Ruby API for every language model\n\nClaude, 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.\n\nA 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.\n\n```\ninstall_gem \"ruby_llm\"\nrequire \"ruby_llm\"\nrequire \"chunky_llm\"   # Chunky Mini, the little model in this page\n\nchat = RubyLLM.chat   # the first time takes a few seconds\nresponse = chat.ask \"Hello!\"\nresponse.content\n```\n\n`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:\n\n```\n[response.role, response.model_id]\n```\n\n### A chat remembers\n\nA 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:\n\n```\nchat.ask \"My name is Ada.\"\nanswer = chat.ask \"What's my name?\"\nputs answer.content\nchat.messages.map(&:role)\n```\n\nSix messages: three questions (`:user`), three answers (`:assistant`). A new chat starts empty – and knows no Ada:\n\n```\nRubyLLM.chat.ask(\"What's my name?\").content\n```\n\n### Instructions\n\n`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”):\n\n```\npirate = RubyLLM.chat.with_instructions(\"You are a pirate. Answer in one short sentence.\")\nputs pirate.ask(\"Tell me about foxes.\").content\npirate.messages.map(&:role)\n```\n\n### Streaming\n\nBig 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:\n\n```\npieces = []\nreply = RubyLLM.chat.ask(\"What is Ruby?\") do |chunk|\n  pieces << chunk.content if chunk.content\nend\n\nputs \"#{pieces.size} pieces, the first: #{pieces.first(4).inspect}\"\nreply.content\n```\n\nThe 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.\n\n### Tokens and cost\n\nModels 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:\n\n```\n[response.tokens.input, response.tokens.output, response.cost.total, chat.cost.total]\n```\n\nFractions 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:\n\n```\nmini = RubyLLM.models.find(\"chunky-mini\")\nputs \"#{mini.name}: #{mini.context_window} tokens of context\"\nRubyLLM.models.by_provider(:anthropic).map(&:id).first(5)\n```\n\n`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:\n\n```\nrequire \"ruby_llm\"\n\nRubyLLM.configure do |config|\n  config.anthropic_api_key = ENV.fetch(\"ANTHROPIC_API_KEY\")\nend\n\nchat = RubyLLM.chat(model: \"claude-sonnet-5-5\")\nchat.ask(\"Hello!\") { |chunk| print chunk.content }\n```\n\nWithout 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.\n\n**Task:** Make a chat\n\n`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.\n\n```\ninstall_gem \"ruby_llm\"\nrequire \"ruby_llm\"\nrequire \"chunky_llm\"   # Chunky Mini, the little model in this page\n\n# helper = RubyLLM.chat.with_instructions(\"...\")\n# tell it your name, then ask for it\n```\n\n", "url": "https://wpnews.pro/news/rubyllm-one-ruby-api-for-every-language-model", "canonical_source": "https://letsruby.com/en/rubyllm-talking-to-a-language-model", "published_at": "2026-12-06 16:38:31+00:00", "updated_at": "2026-12-06 16:45:13.828900+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "developer-tools", "ai-tools"], "entities": ["RubyLLM", "Carmine Paolino", "OpenAI", "Anthropic", "Gemini", "Mistral", "DeepSeek", "Ollama"], "also_reported_by": [], "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.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/rubyllm-one-ruby-api-for-every-language-model", "markdown": "https://wpnews.pro/news/rubyllm-one-ruby-api-for-every-language-model.md", "text": "https://wpnews.pro/news/rubyllm-one-ruby-api-for-every-language-model.txt", "jsonld": "https://wpnews.pro/news/rubyllm-one-ruby-api-for-every-language-model.jsonld"}}