Your First Chat

The heart of RubyLLM is RubyLLM.chat. Three lines of code is all you need:

chat = RubyLLM.chat
response = chat.ask("What is Ruby?")
puts response.content

RubyLLM picks a sensible default model, sends your prompt, and returns a response object.

The Response Object

Every call to chat.ask(...) returns a response with useful metadata:

PropertyWhat it contains
response.contentThe AI’s text reply
response.model_idWhich model generated the response
response.input_tokensTokens your prompt used
response.output_tokensTokens the reply used

Your Task

Open first_chat.rb and:

  1. Create a chat with RubyLLM.chat
  2. Ask a question with chat.ask("...")
  3. Print the response content and metadata
first_chat.rb
chat = RubyLLM.chat
response = chat.ask("What makes Ruby special as a programming language? Answer in 2-3 sentences.")
puts response.content
puts
puts "Model: #{response.model_id}"
puts "Input tokens: #{response.input_tokens}"
puts "Output tokens: #{response.output_tokens}"

Run it:

Terminal window
$ ruby first_chat.rb

This requires a configured API key. If you don’t have one, the code pattern is still the important takeaway.

Choosing a Model

Pass a model ID to use a specific model:

chat = RubyLLM.chat(model: "claude-sonnet-4-6") # Anthropic
chat = RubyLLM.chat(model: "gpt-4o") # OpenAI
chat = RubyLLM.chat(model: "gemini-2.0-flash") # Google

You can even switch models mid-conversation:

chat = RubyLLM.chat(model: "gpt-4o")
chat.ask("What is Ruby?")
chat.with_model("claude-sonnet-4-6")
chat.ask("Now explain it differently")

The conversation history carries over — the new model sees everything the previous one said.

Built by <a href="https://chamoylabs.com">Chamoy Labs</a> based on <a href="https://rubyllm.com/">RubyLLM</a> by <a href="https://paolino.me/">Carmine Paolino</a>
Files
Preparing Environment
  • Preparing Ruby runtime