Class: RubyLLM::Protocols::Anthropic

Inherits:
RubyLLM::Protocol show all
Includes:
Anthropic::Chat, Anthropic::Embeddings, Anthropic::Media, Anthropic::Models, Anthropic::Streaming, Anthropic::Tools
Defined in:
lib/ruby_llm/protocols/anthropic.rb,
lib/ruby_llm/protocols/anthropic/chat.rb,
lib/ruby_llm/protocols/anthropic/files.rb,
lib/ruby_llm/protocols/anthropic/media.rb,
lib/ruby_llm/protocols/anthropic/tools.rb,
lib/ruby_llm/protocols/anthropic/models.rb,
lib/ruby_llm/protocols/anthropic/batches.rb,
lib/ruby_llm/protocols/anthropic/streaming.rb,
lib/ruby_llm/protocols/anthropic/embeddings.rb

Overview

The Anthropic Messages API.

Defined Under Namespace

Modules: Batches, Chat, Embeddings, Media, Models, Streaming, Tools Classes: Files

Constant Summary collapse

MAX_PAUSE_TURN_CONTINUATIONS =

How many times a turn that stops with pause_turn is automatically continued before RubyLLM returns what it has.

8
SERVER_TOOL_ALIASES =

web_search/web_fetch pin allowed_callers to direct invocation: their 2026 tool versions otherwise default to requiring programmatic tool calling, which not every model supports.

{
  web_search: { tool: { type: 'web_search_20260318', name: 'web_search', allowed_callers: ['direct'] } },
  web_fetch: { tool: { type: 'web_fetch_20260318', name: 'web_fetch', allowed_callers: ['direct'] } },
  url_context: { tool: { type: 'web_fetch_20260318', name: 'web_fetch', allowed_callers: ['direct'] } },
  code_execution: { tool: { type: 'code_execution_20260521', name: 'code_execution' } },
  mcp: lambda do |options|
    options = Support::Utils.deep_symbolize_keys(options)
    name = options[:name] || 'mcp'
    server = { type: 'url', name: name }.merge(options.slice(:url, :authorization_token))
    {
      tool: { type: 'mcp_toolset', mcp_server_name: name }.merge(options.slice(:default_config, :configs)),
      payload: { mcp_servers: [server] },
      headers: { 'anthropic-beta' => 'mcp-client-2025-11-20' }
    }
  end
}.freeze

Instance Attribute Summary

Attributes inherited from RubyLLM::Protocol

#config, #connection, #model, #provider

Instance Method Summary collapse

Methods inherited from RubyLLM::Protocol

abstract, #animate_later, #apply_compaction, #apply_compaction_headers, #apply_end_user, #cache_content, #compact, #count_tokens, #delete_cache, #embed, #extend_cache, #find_cache, #initialize, #list_models, #moderate, #ocr, #paint, #parse_error, #parse_image_responses, #post_image, #post_video, #preprocess_message, #raise_transcription_streaming_unsupported, #refresh_video_job, #render, #render_embedding, #render_transcription_options, #render_video_extension_payload, #rerank, #speak, #stream_speech, #stream_speech_response, #stream_transcription, #supports_embedding_media?, #tokenize, #tool_approval_response, #transcribe, #video_extension_attachment, #video_extension_url, #video_request_url

Methods included from RubyLLM::Protocol::BinaryStreaming

#stream_binary

Methods included from RubyLLM::Protocol::Streaming

build_on_data_handler, build_stream_error_response, error_chunk?, failed_http_status, faraday_1?, handle_data, handle_error_chunk, handle_error_event, handle_failed_response, handle_json_error_chunk, handle_sse, handle_stream, json_error_payload?, parse_error_from_json, parse_streaming_error, process_stream_chunk, raise_stream_error, stream_events, stream_response, stream_state

Constructor Details

This class inherits a constructor from RubyLLM::Protocol

Instance Method Details

#complete(messages) ⇒ Object

Server tools can pause a long turn with stop_reason pause_turn; the API expects the conversation back verbatim to continue it. Segments are merged into the single Message the caller sees.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby_llm/protocols/anthropic.rb', line 45

def complete(messages, ...)
  segments = []
  current_messages = messages

  MAX_PAUSE_TURN_CONTINUATIONS.times do
    message = super(current_messages, ...)
    segments << message
    break unless message.finish_reason == :pause_turn

    current_messages = current_messages + [message] # rubocop:disable Style/SelfAssignment
  end

  merge_turn_segments(segments)
end

#server_tool_aliasesObject



38
39
40
# File 'lib/ruby_llm/protocols/anthropic.rb', line 38

def server_tool_aliases
  SERVER_TOOL_ALIASES
end