Class: LLM::Anthropic

Inherits:
Provider show all
Includes:
RequestAdapter
Defined in:
lib/llm/providers/anthropic.rb,
lib/llm/providers/anthropic/files.rb,
lib/llm/providers/anthropic/models.rb,
lib/llm/providers/anthropic/error_handler.rb,
lib/llm/providers/anthropic/stream_parser.rb,
lib/llm/providers/anthropic/request_adapter.rb,
lib/llm/providers/anthropic/response_adapter.rb

Overview

The Anthropic class implements a provider for [Anthropic](www.anthropic.com).

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: RequestAdapter, ResponseAdapter Classes: ErrorHandler, Files, Models, StreamParser

Constant Summary collapse

HOST =
"api.anthropic.com"

Instance Method Summary collapse

Methods included from RequestAdapter

#adapt

Methods inherited from Provider

#audio, #chat, clients, #embed, #images, #inspect, #moderations, #respond, #responses, #schema, #server_tool, #vector_stores, #with

Constructor Details

#initializeAnthropic

Returns a new instance of Anthropic.

Parameters:

  • key (String, nil)

    The secret key for authentication



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

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

Instance Method Details

#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”



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

def assistant_role
  "assistant"
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:



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

def complete(prompt, params = {})
  params = {role: :user, model: default_model, max_tokens: 1024}.merge!(params)
  tools  = resolve_tools(params.delete(:tools))
  params = [params, adapt_tools(tools)].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 = LLM.json.dump({messages: [adapt(messages)].flatten}.merge!(params))
  set_body_stream(req, StringIO.new(body))
  res = execute(request: req, stream:)
  ResponseAdapter.adapt(res, type: :completion)
    .extend(Module.new { define_method(:__tools__) { tools } })
end

#default_modelString

Returns the default model for chat completions

Returns:

  • (String)

See Also:



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

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

#filesLLM::Anthropic::Files

Provides an interface to Anthropic’s files API

Returns:

See Also:



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

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

#modelsLLM::Anthropic::Models

Provides an interface to Anthropic’s models API



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

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

#server_toolsString => LLM::ServerTool

Note:

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

Returns:

See Also:



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

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

#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:



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

def web_search(query:)
  ResponseAdapter.adapt(complete(query, tools: [server_tools[:web_search]]), type: :web_search)
end