SDKs & Developer Experience

Ruby SDK Quickstart: Gem, Bundler, and First Call

Plenty of enterprise workhorses — internal tools, billing systems, whole product lines — still run Rails. The official Ruby SDK lets them call Claude without leaving the Gemfile.

Claude 3P 101 · Updated July 2026 · Unofficial guide

Anthropic ships an official Ruby SDK as the anthropic gem. If your organization runs Rails applications or Ruby-based internal tooling, this is the supported path to Claude — no shelling out to Python, no hand-rolled HTTP client to maintain. It requires Ruby 3.2.0 or newer, so older applications may need a runtime upgrade first; that is worth knowing before you promise a delivery date.

Step 1: Add the gem

Add the gem to your Gemfile with a pessimistic version constraint, then run bundle install as usual:

gem "anthropic", "~> 1.55.0"

The ~> constraint is the Bundler idiom for "take patch updates, not surprises" — appropriate here, because you want SDK upgrades to be deliberate, tested events rather than side effects of an unrelated bundle update. The version shown is current as of this writing; check the official repository for the latest release.

Step 2: Create the client

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

The client class is Anthropic::Client. Passing the key explicitly from ENV makes the dependency visible; calling Anthropic::Client.new with no arguments does the same thing implicitly, reading ANTHROPIC_API_KEY from the environment. Either way, the key itself — a long-lived secret created in the Claude Console under Settings → API keys — should come from your credential store (Rails encrypted credentials, or your platform's secret manager), never from a committed file. API keys have no expiry, so rotate them periodically and revoke immediately on a suspected leak. For production workloads on cloud platforms or CI/CD, Anthropic recommends Workload Identity Federation (short-lived, automatically refreshed tokens) over long-lived keys.

Step 3: First call and the response object

message = anthropic.messages.create(
  max_tokens: 1024,
  messages: [{ role: "user", content: "Hello, Claude" }],
  model: "claude-sonnet-5"
)

Three parameters carry the request. model selects the Claude model — claude-sonnet-5 is the balanced default, claude-opus-4-8 the choice for complex enterprise and agentic work, claude-haiku-4-5 the fast, low-cost tier. max_tokens caps the reply length (tokens are the billing unit — roughly a short word each). messages is the conversation history as an array of role/content hashes.

What comes back is not a bare string. The SDK returns a structured message object mirroring the Messages API response: the reply arrives as a list of content blocks (because responses can mix text with other block types, such as tool-use requests), alongside metadata — which model actually answered, why generation stopped, and the input/output token counts. Handle it accordingly: iterate the content blocks rather than assuming a single text field, branch on the stop reason before trusting output in automated flows, and log the usage numbers so finance can attribute cost per feature later. The gem's README documents the full response shape and the streaming variant.

Rails tip: wrap the client in a small service object rather than calling it from controllers directly. You get one place to set the model, log usage, and stub responses in tests — and swapping platforms later becomes a one-file change.

Ruby and the third-party platforms

The anthropic gem targets Anthropic's first-party Claude API. For Claude Platform on AWS — Anthropic-operated, running inside AWS — a beta AWS client is available (installed alongside aws-sdk-core, with an Anthropic::AWSClient class handling SigV4 signing and workspace configuration). For Amazon Bedrock, Google Vertex AI, or Microsoft Foundry from Ruby, check each platform's own documentation for supported clients; the concepts covered in the Bedrock and Vertex AI setup guides — credentials, regions, model ID formats — apply in any language.

Where to go next

Set up API key authentication properly, read streaming basics for user-facing features, and see the SDK language overview for how Ruby compares to the other official SDKs.

Sources