Skip to content

anthropics/anthropic-sdk-ruby

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Anthropic Ruby API library

The Anthropic Ruby library provides convenient access to the Anthropic REST API from any Ruby 3.1.0+ application.

Documentation

Documentation for released of this gem can be found on RubyDoc.

The underlying REST API documentation can be found on docs.anthropic.com.

Installation

ℹ️ The anthropic-sdk-beta gem name is temporary. @alexrudall will be transitioning the anthropic gem name to this repository. Here's the timeline:

  • Early April, 2025: This library is released under the anthropic-sdk-beta gem name. We'll be gathering feedback from the community.
  • Late April/early May: Bump the version 1.0, and transition the anthropic gem name to this repository.

To use this gem, install via Bundler by adding the following to your application's Gemfile:

gem "anthropic-sdk-beta", "~> 0.1.0.pre.beta.6"

Usage

require "bundler/setup"
require "anthropic"

anthropic = Anthropic::Client.new(
  api_key: "my-anthropic-api-key" # defaults to ENV["ANTHROPIC_API_KEY"]
)

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

puts(message.content)

Feedback

We're looking for as much feedback as possible while the SDK is in Beta. If you have recommendations, notice bugs, find things confusing, or anything else, create a github issue. Don't be shy -- we're very open to hearing any thoughts and musings you have!

Feel free to make an issue for more substantial issues. For smaller issues or stream-of-thought, you can use the pinned issue here.

Pagination

List methods in the Anthropic API are paginated.

This library provides auto-paginating iterators with each list response, so you do not have to request successive pages manually:

page = anthropic.beta.messages.batches.list(limit: 20)

# Fetch single item from page.
batch = page.data[0]
puts(batch.id)

# Automatically fetches more pages as needed.
page.auto_paging_each do |batch|
  puts(batch.id)
end

Streaming

We provide support for streaming responses using Server Side Events (SSE).

coming soon: anthropic.messages.stream will have Python SDK style streaming response helpers.

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

stream.each do |message|
  puts(message.type)
end

Errors

When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), a subclass of Anthropic::Errors::APIError will be thrown:

begin
  message = anthropic.messages.create(
    max_tokens: 1024,
    messages: [{
      role: "user",
      content: "Hello, Claude"
    }],
    model: "claude-3-5-sonnet-latest"
  )
rescue Anthropic::Errors::APIError => e
  puts(e.status) # 400
end

Error codes are as followed:

Cause Error Type
HTTP 400 BadRequestError
HTTP 401 AuthenticationError
HTTP 403 PermissionDeniedError
HTTP 404 NotFoundError
HTTP 409 ConflictError
HTTP 422 UnprocessableEntityError
HTTP 429 RateLimitError
HTTP >= 500 InternalServerError
Other HTTP error APIStatusError
Timeout APITimeoutError
Network error APIConnectionError

Retries

Certain errors will be automatically retried 2 times by default, with a short exponential backoff.

Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, >=500 Internal errors, and timeouts will all be retried by default.

You can use the max_retries option to configure or disable this:

# Configure the default for all requests:
anthropic = Anthropic::Client.new(
  max_retries: 0 # default is 2
)

# Or, configure per-request:
anthropic.messages.create(
  max_tokens: 1024,
  messages: [{
    role: "user",
    content: "Hello, Claude"
  }],
  model: "claude-3-5-sonnet-latest",
  request_options: {max_retries: 5}
)

Timeouts

By default, requests will time out after 600 seconds.

Timeouts are applied separately to the initial connection and the overall request time, so in some cases a request could wait 2*timeout seconds before it fails.

You can use the timeout option to configure or disable this:

# Configure the default for all requests:
anthropic = Anthropic::Client.new(
  timeout: nil # default is 600
)

# Or, configure per-request:
anthropic.messages.create(
  max_tokens: 1024,
  messages: [{
    role: "user",
    content: "Hello, Claude"
  }],
  model: "claude-3-5-sonnet-latest",
  request_options: {timeout: 5}
)

LSP Support

Solargraph

This library includes Solargraph support for both auto completion and go to definition.

gem "solargraph", group: :development

After Solargraph is installed, you must populate its index either via the provided editor command, or by running the following in your terminal:

bundle exec solargraph gems

Note: if you had installed the gem either using a git: or github: URL, or had vendored the gem using bundler, you will need to set up your .solargraph.yml to include the path to the gem's lib directory.

include:
  - 'vendor/bundle/ruby/*/gems/anthropic-sdk-beta-*/lib/**/*.rb'

Otherwise Solargraph will not be able to provide type information or auto-completion for any non-indexed libraries.

Sorbet

This library is written with Sorbet type definitions. However, there is no runtime dependency on the sorbet-runtime.

What this means is that while you can use Sorbet to type check your code statically, and benefit from the Sorbet Language Server in your editor, there is no runtime type checking and execution overhead from Sorbet itself.

Due to limitations with the Sorbet type system, where a method otherwise can take an instance of Anthropic::BaseModel class, you will need to use the ** splat operator to pass the arguments:

Please follow Sorbet's setup guides for best experience.

params = Anthropic::Models::MessageCreateParams.new(
    max_tokens: 1024,
    messages: [Anthropic::Models::MessageParam.new(role: :user, content: "hello")],
    model: "claude-3-5-sonnet-latest"
  )

anthropic.messages.create(**params)

Note: This library emits an intentional warning under the tapioca toolchain. This is normal, and does not impact functionality.

Ruby LSP

The Ruby LSP has best effort support for inferring type information from Ruby code, and as such it may not always be able to provide accurate type information.

AWS Bedrock

This library also provides support for the Anthropic Bedrock API if you install this library with the aws-sdk-bedrockruntime gem.

You can then instantiate a separate Anthropic::Bedrock::Client class, and use AWS's standard guide for configuring credentials (see the aws-sdk-ruby gem README or AWS Documentation). It has the same API as the base Anthropic::Client class.

Note that the model ID required is different for Bedrock models, and, depending on the model you want to use, you will need to use either the AWS's model ID for Anthropic models -- which can be found in AWS's Bedrock model catalog -- or an inference profile id (e.g. us.anthropic.claude-3-5-haiku-20241022-v1:0 for Claude 3.5 Haiku).

require "bundler/setup"
require "anthropic"

anthropic = Anthropic::Bedrock::Client.new

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

puts message

For more examples see examples/bedrock.

Google Vertex

This library also provides support for the Anthropic Vertex API if you install this library with the googleauth gem.

You can then import and instantiate a separate Anthropic::Vertex::Client class, and use Google's guide for configuring Application Default Credentials. It has the same API as the base Anthropic::Client class.

require "bundler/setup"
require "anthropic"

anthropic = Anthropic::Vertex::Client.new(region: "us-east5", project_id: "my-project-id")

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

puts message

For more examples see examples/vertex.

Advanced

Making custom/undocumented requests

This library is typed for convenient access to the documented API.

If you need to access undocumented endpoints, params, or response properties, the library can still be used.

Undocumented request params

If you want to explicitly send an extra param, you can do so with the extra_query, extra_body, and extra_headers under the request_options: parameter when making a requests as seen in examples above.

Undocumented endpoints

To make requests to undocumented endpoints, you can make requests using client.request. Options on the client will be respected (such as retries) when making this request.

response = client.request(
    method: :post,
    path: '/undocumented/endpoint',
    query: {"dog": "woof"},
    headers: {"useful-header": "interesting-value"},
    body: {"he": "llo"},
  )

Concurrency & Connection Pooling

The Anthropic::Client instances are thread-safe, and should be re-used across multiple threads. By default, each Client have their own HTTP connection pool, with a maximum number of connections equal to thread count.

When the maximum number of connections has been checked out from the connection pool, the Client will wait for an in use connection to become available. The queue time for this mechanism is accounted for by the per-request timeout.

Unless otherwise specified, other classes in the SDK do not have locks protecting their underlying data structure.

Currently, Anthropic::Client instances are only fork-safe if there are no in-flight HTTP requests.

Versioning

This package follows SemVer conventions. As the library is in initial development and has a major version of 0, APIs may change at any time.

This package considers improvements to the (non-runtime) *.rbi and *.rbs type definitions to be non-breaking changes.

Requirements

Ruby 3.1.0 or higher.

Acknowledgements

Thank you @alexrudall for giving feedback, donating the anthropic Ruby Gem name, and paving the way by building the first Anthropic Ruby SDK.