Class: RubyPi::LLM::Anthropic

Inherits:
BaseProvider show all
Defined in:
lib/ruby_pi/llm/anthropic.rb

Overview

Anthropic Claude provider implementation. Communicates with the Anthropic Messages API to generate text completions, handle tool_use blocks, and stream responses via Server-Sent Events.

Examples:

Basic usage

provider = RubyPi::LLM::Anthropic.new(
  model: "claude-sonnet-4-20250514",
  api_key: ENV["ANTHROPIC_API_KEY"]
)
response = provider.complete(messages: [{ role: "user", content: "Hello!" }])
puts response.content

Constant Summary collapse

BASE_URL =

Base URL for the Anthropic Messages API.

"https://api.anthropic.com"
API_VERSION =

Anthropic API version header value.

"2023-06-01"
DEFAULT_MAX_TOKENS =

Default maximum tokens for a response.

4096

Instance Attribute Summary

Attributes inherited from BaseProvider

#max_retries, #retry_base_delay, #retry_max_delay

Instance Method Summary collapse

Methods inherited from BaseProvider

#complete

Constructor Details

#initialize(model: nil, api_key: nil, max_tokens: DEFAULT_MAX_TOKENS, **options) ⇒ Anthropic

Creates a new Anthropic provider instance.

Parameters:

  • model (String) (defaults to: nil)

    the Claude model identifier (e.g., “claude-sonnet-4-20250514”)

  • api_key (String, nil) (defaults to: nil)

    Anthropic API key (falls back to global config)

  • max_tokens (Integer) (defaults to: DEFAULT_MAX_TOKENS)

    maximum tokens to generate (default: 4096)

  • options (Hash)

    additional options passed to BaseProvider



38
39
40
41
42
43
44
# File 'lib/ruby_pi/llm/anthropic.rb', line 38

def initialize(model: nil, api_key: nil, max_tokens: DEFAULT_MAX_TOKENS, **options)
  super(**options)
  config = RubyPi.configuration
  @model = model || config.default_anthropic_model
  @api_key = api_key || config.anthropic_api_key
  @max_tokens = max_tokens
end

Instance Method Details

#model_nameString

Returns the Claude model identifier.

Returns:

  • (String)


49
50
51
# File 'lib/ruby_pi/llm/anthropic.rb', line 49

def model_name
  @model
end

#provider_nameSymbol

Returns :anthropic as the provider identifier.

Returns:

  • (Symbol)


56
57
58
# File 'lib/ruby_pi/llm/anthropic.rb', line 56

def provider_name
  :anthropic
end