Class: LLM::Anthropic

Inherits:
Provider show all
Defined in:
lib/llm/providers/anthropic.rb,
lib/llm/providers/anthropic/files.rb,
lib/llm/providers/anthropic/format.rb,
lib/llm/providers/anthropic/models.rb,
lib/llm/providers/anthropic/error_handler.rb,
lib/llm/providers/anthropic/stream_parser.rb

Overview

The Anthropic class implements a provider for Anthropic.

Examples:

#!/usr/bin/env ruby
require "llm"

llm = LLM.anthropic(key: ENV["KEY"])
bot = LLM::Bot.new(llm)
bot.chat ["Tell me about this photo", File.open("/images/dog.jpg", "rb")]
bot.messages.select(&:assistant?).each { print "[#{_1.role}]", _1.content, "\n" }

Defined Under Namespace

Modules: Response Classes: Files, Models

Constant Summary collapse

HOST =
"api.anthropic.com"

Instance Method Summary collapse

Methods inherited from Provider

#audio, #chat, #chat!, clients, #embed, #images, #inspect, #moderations, mutex, #respond, #respond!, #responses, #schema, #tool, #vector_stores, #with

Constructor Details

#initializeAnthropic

Returns a new instance of Anthropic.

Parameters:

  • key (String, nil)

    The secret key for authentication



30
31
32
# File 'lib/llm/providers/anthropic.rb', line 30

def initialize(**)
  super(host: HOST, **)
end

Instance Method Details

#web_search(query:) ⇒ LLM::Response

A convenience method for performing a web search using the Anthropic web search tool.

Examples:

llm = LLM.anthropic(key: ENV["KEY"])
res = llm.web_search(query: "summarize today's news")
res.search_results.each { |item| print item.title, ": ", item.url, "\n" }

Parameters:

  • query (String)

    The search query.

Returns:



111
112
113
114
# File 'lib/llm/providers/anthropic.rb', line 111

def web_search(query:)
  complete(query, tools: [tools[:web_search]])
    .extend(LLM::Anthropic::Response::WebSearch)
end

#complete(prompt, params = {}) ⇒ LLM::Response

Provides an interface to the chat completions API

Examples:

llm = LLM.openai(key: ENV["KEY"])
messages = [{role: "system", content: "Your task is to answer all of my questions"}]
res = llm.complete("5 + 2 ?", messages:)
print "[#{res.choices[0].role}]", res.choices[0].content, "\n"

Parameters:

  • prompt (String)

    The input prompt to be completed

  • params (Hash) (defaults to: {})

    The parameters to maintain throughout the conversation. Any parameter the provider supports can be included and not only those listed here.

Returns:

Raises:

See Also:



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/llm/providers/anthropic.rb', line 44

def complete(prompt, params = {})
  params = {role: :user, model: default_model, max_tokens: 1024}.merge!(params)
  params = [params, format_tools(params)].inject({}, &:merge!).compact
  role, stream = params.delete(:role), params.delete(:stream)
  params[:stream] = true if stream.respond_to?(:<<) || stream == true
  req = Net::HTTP::Post.new("/v1/messages", headers)
  messages = [*(params.delete(:messages) || []), Message.new(role, prompt)]
  body = JSON.dump({messages: [format(messages)].flatten}.merge!(params))
  set_body_stream(req, StringIO.new(body))
  res = execute(request: req, stream:)
  LLM::Response.new(res).extend(LLM::Anthropic::Response::Completion)
end

#modelsLLM::Anthropic::Models

Provides an interface to Anthropic’s models API



61
62
63
# File 'lib/llm/providers/anthropic.rb', line 61

def models
  LLM::Anthropic::Models.new(self)
end

#filesLLM::Anthropic::Files

Provides an interface to Anthropic’s files API

Returns:

See Also:



69
70
71
# File 'lib/llm/providers/anthropic.rb', line 69

def files
  LLM::Anthropic::Files.new(self)
end

#assistant_roleString

Returns the role of the assistant in the conversation. Usually “assistant” or “model”

Returns:

  • (String)

    Returns the role of the assistant in the conversation. Usually “assistant” or “model”



75
76
77
# File 'lib/llm/providers/anthropic.rb', line 75

def assistant_role
  "assistant"
end

#default_modelString

Returns the default model for chat completions

Returns:

  • (String)

See Also:



83
84
85
# File 'lib/llm/providers/anthropic.rb', line 83

def default_model
  "claude-sonnet-4-20250514"
end

#toolsString => LLM::Tool

Note:

This method includes certain tools that require configuration through a set of options that are easier to set through the LLM::Provider#tool method.

Returns:

See Also:



94
95
96
97
98
99
100
# File 'lib/llm/providers/anthropic.rb', line 94

def tools
  {
    bash: tool(:bash, type: "bash_20250124"),
    web_search: tool(:web_search, type: "web_search_20250305", max_uses: 5),
    text_editor: tool(:str_replace_based_edit_tool, type: "text_editor_20250728", max_characters: 10_000)
  }
end