Claude Ruby SDK — Quickstart

Calling the Claude API from Ruby in 2026 — the official anthropic gem, streaming, prompt caching, tool use, and how to integrate with Rails background jobs.

🔥 Launch tonight — Power Prompts PDF 50p (just 50p tonight)30 battle-tested Claude Code prompts · 8 pages · paste into CLAUDE.md · price reverts to £5

Anthropic ships an official Ruby SDK — anthropic on rubygems.org, source at github.com/anthropics/anthropic-sdk-ruby. It supports Ruby 3.0+. Feature parity with the Python and TypeScript SDKs: messages, streaming, batch, prompt caching, tool use, vision.

Install

# Gemfile
gem "anthropic"

# or directly
gem install anthropic

Minimum-correct call

require "anthropic"

client = Anthropic::Client.new(api_key: ENV["ANTHROPIC_API_KEY"])

message = client.messages.create(
  model: "claude-sonnet-4-6",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Explain Ruby blocks in 3 bullets." }]
)

puts message.content.first.text

Streaming

client.messages.stream(
  model: "claude-sonnet-4-6",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Write a limerick." }]
) do |event|
  if event.type == "content_block_delta" && event.delta.type == "text_delta"
    print event.delta.text
  end
end

Prompt caching

client.messages.create(
  model: "claude-sonnet-4-6",
  max_tokens: 1024,
  system: [
    {
      type: "text",
      text: long_system_prompt,           # 20k+ tokens
      cache_control: { type: "ephemeral" }
    }
  ],
  messages: [{ role: "user", content: question }]
)

Cached reads cost 10% of input price after the first request. Inspect the usage field on responses for cache_creation_input_tokens and cache_read_input_tokens. Background: prompt caching explained.

Rails integration: Sidekiq background job

class ClaudeCompletionJob
  include Sidekiq::Job
  sidekiq_options retry: 5, queue: "claude"

  def perform(prompt_id)
    prompt = Prompt.find(prompt_id)
    client = Anthropic::Client.new
    response = client.messages.create(
      model: "claude-sonnet-4-6",
      max_tokens: 2048,
      messages: [{ role: "user", content: prompt.text }]
    )
    prompt.update!(
      completion: response.content.first.text,
      input_tokens: response.usage.input_tokens,
      output_tokens: response.usage.output_tokens
    )
  rescue Anthropic::RateLimitError => e
    retry_in = e.response.headers["retry-after"].to_i
    raise Sidekiq::RetryableError.new(e.message, retry_in)
  end
end

Sidekiq's retry-with-backoff plus the SDK's built-in retries (configurable with Anthropic::Client.new(max_retries: 5)) covers most 429s without custom logic.

Tool use (function calling)

tools = [{
  name: "get_weather",
  description: "Get current weather for a location.",
  input_schema: {
    type: "object",
    properties: { location: { type: "string" } },
    required: ["location"]
  }
}]

response = client.messages.create(
  model: "claude-sonnet-4-6",
  max_tokens: 1024,
  tools: tools,
  messages: [{ role: "user", content: "What's the weather in Tokyo?" }]
)

response.content.each do |block|
  if block.type == "tool_use"
    result = WeatherService.fetch(block.input["location"])
    # feed result back on the next turn
  end
end

Common Rails patterns

Cost tracking in Ruby services

Persist response.usage per call to a claude_usage_events table and roll up daily. The Claude Cost Calculator models per-MTok rates so you can forecast monthly bills from your usage logs.

Alternative SDK paths: Python SDK, TypeScript SDK, Go SDK.

Frequently asked questions

Is there an official Anthropic Ruby SDK?
Yes — the anthropic gem on rubygems.org, source at github.com/anthropics/anthropic-sdk-ruby. It is the recommended way to call Claude from Ruby applications and supports Ruby 3.0 and later.
How do I stream Claude responses in Ruby?
Use client.messages.stream with a block. The block receives event objects; check event.type == 'content_block_delta' and append event.delta.text to your output buffer.
Can I use the anthropic gem in Rails background jobs?
Yes — and you should. LLM calls take seconds, which is the wrong shape for a controller action. Wrap them in Sidekiq, GoodJob, or ActiveJob workers. The SDK client is thread-safe; share one instance across workers.
Does the Ruby SDK retry 429s automatically?
Yes. Anthropic::Client.new(max_retries: 5) retries 429 and 5xx with exponential backoff. For job-runner integration, also rescue Anthropic::RateLimitError and re-raise as a retryable job error so your queue's own backoff takes over.
Does prompt caching work the same way in Ruby as in Python?
Yes. Set cache_control: { type: 'ephemeral' } on any system or user content block. The response.usage exposes cache_creation_input_tokens and cache_read_input_tokens to confirm hits.

Free tools

Cost Calculator → Prompt-Pricing Recommender → Diff Summarizer → Skills Browser →

Related

Claude Opus 4.7 vs Sonnet 4.6 Pricing (2026 Comparison)How Much Does Claude Cost? (2026 API Pricing Guide)Claude Prompt Caching: 90% Cost Savings Explained (2026)Claude API Cost Calculator: Estimate Your Anthropic BillClaude vs GPT-4 Pricing: 2026 API Cost Comparison