class Anthropic::Client

Overview

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.

Defined in:

client.cr
messages.cr

Constructors

Instance Method Summary

Constructor Detail

def self.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.


[View source]

Instance Method Detail

def api_key : String #

[View source]
def base_uri : URI #

[View source]
def messages #

[View source]