class

Anthropic::Client

Inherits Reference / Object

The Anthropic::Client is the entrypoint for using the Anthropic API in Crystal.

claude = Anthropic::Client.new # get API key from the ENV
puts claude.messages.create(
  model: Anthropic.model_name(:haiku),
  messages: [Anthropic::Message.new("Write a haiku about the Crystal programming language")],
  max_tokens: 4096,
)
# Sparkling Crystal code,
# Elegant and swift syntax,
# Shines with precision.

The client is concurrency-safe, so you don't need to wrap requests in a mutex or manage a connection pool yourself.

Constructors

new(api_key : String = ENV["ANTHROPIC_API_KEY"], base_uri : URI = URI.parse(ENV.fetch("ANTHROPIC_BASE_URL", "https://api.anthropic.com")), log : Log = Log.for(self.class))

Instantiate a new client with the API key provided either directly or via the ANTHROPIC_API_KEY environment variable. You can optionally provide a base URI to connect to if you are using a different but compatible API provider.

Source

Instance methods

api_key
Source
base_uri
Source
messages
Source
models
Source

Nested types