Class: Anthropic::Helpers::Tools::Runner Private

Inherits:
Object
  • Object
show all
Defined in:
lib/anthropic/helpers/tools/runner.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, params:, max_iterations: nil, compaction_control: nil) ⇒ Runner

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Runner.

Parameters:



678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# File 'lib/anthropic/helpers/tools/runner.rb', line 678

def initialize(client, params:, max_iterations: nil, compaction_control: nil)
  @client = client
  @params = params.to_h
  reject_compaction_param!(@params)
  @finished = false
  @max_iterations = max_iterations
  @iteration_count = 0
  @compaction_control = compaction_control
  @compaction_warned = false
  @last_response = nil
  @pending_tool_changes = []
  @tool_overrides = {}
  @pending_compaction = nil
  @compaction_phase = nil
end

Instance Attribute Details

#paramsAnthropic::Models::Beta::MessageCreateParams

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
# File 'lib/anthropic/helpers/tools/runner.rb', line 32

def params
  @params
end

Instance Method Details

#add_tools(*tools) ⇒ void

Sends the tools' definitions in tool_addition blocks with the next request, leaving params[:tools] and the prompt cache alone. A BaseTool is run under its name straight away, replacing a same-name tool even for a call already in the message being handled; a raw definition is never run here and stops a same-name tool from running. Needs the inline-tools-2026-09-15 beta.

Parameters:



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/anthropic/helpers/tools/runner.rb', line 68

def add_tools(*tools)
  # Converted in place, so the originals are kept apart to pair each with its definition.
  request = {tools: tools.dup}
  Anthropic::Helpers::Messages.distill_input_schema_models!(request, strict: nil)
  tools.zip(request.fetch(:tools)) do |tool, definition|
    block = {type: :tool_addition, tool: {type: :tool_definition, definition:}}
    runnable = tool if tool.is_a?(Anthropic::Helpers::Tools::BaseTool)
    name = changed_tool_name(block.fetch(:tool))
    @tool_overrides.store(name, runnable) unless name.nil?
    @pending_tool_changes << block
  end
end

#compact_before_next_turn(compaction = nil) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Compact the conversation before the model's next turn. Once the current turn has finished, including any tool calls, the runner asks the API for a summary and replaces its messages with the compaction response, which you get like any other message. Requires the compact-2026-09-04 beta.

Parameters:

Raises:

  • (ArgumentError)

    if context_management has a compaction edit



112
113
114
115
116
117
# File 'lib/anthropic/helpers/tools/runner.rb', line 112

def compact_before_next_turn(compaction = nil)
  return unless @compaction_phase.nil?

  check_can_compact!(params)
  @pending_compaction = compaction || {type: :summarize}
end

#each_message {|| ... } ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Yield Parameters:



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/anthropic/helpers/tools/runner.rb', line 140

def each_message(&blk)
  unless block_given?
    raise ArgumentError.new("A block must be given to ##{__method__}")
  end

  fold do
    message = @client.beta.messages.create(with_helper_header(_1, StainlessHelperHeader::BETA_TOOL_RUNNER))
    blk.call(message)
    [false, message]
  end
end

#each_streaming {|| ... } ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/anthropic/helpers/tools/runner.rb', line 153

def each_streaming(&blk)
  unless block_given?
    raise ArgumentError.new("A block must be given to ##{__method__}")
  end

  fold do
    stream = @client.beta.messages.stream(with_helper_header(_1, StainlessHelperHeader::BETA_TOOL_RUNNER))
    blk.call(stream)
    [false, stream.accumulated_message]
  ensure
    stream&.close
  end
end

#feed_messages(*messages) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • params (Array<Anthropic::Beta::BetaMessageParam>)


55
56
57
# File 'lib/anthropic/helpers/tools/runner.rb', line 55

def feed_messages(*messages)
  self.params = {**params.to_h, messages: params[:messages].to_a + messages}
end

#finished?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



52
# File 'lib/anthropic/helpers/tools/runner.rb', line 52

def finished? = @finished

#next_messageAnthropic::Models::BetaMessage?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



123
124
125
126
127
128
129
130
# File 'lib/anthropic/helpers/tools/runner.rb', line 123

def next_message
  message = nil
  fold do
    message = @client.beta.messages.create(with_helper_header(_1, StainlessHelperHeader::BETA_TOOL_RUNNER))
    [true, message]
  end
  message
end

#remove_tools(*tools) ⇒ void

Sends tool_removal blocks with the next request. The tools stop being run straight away, so a call to one gets the "not found" error result. Needs the inline-tools-2026-09-15 beta.

Parameters:



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/anthropic/helpers/tools/runner.rb', line 87

def remove_tools(*tools)
  tools.each do |tool|
    name =
      case tool
      in Anthropic::Helpers::Tools::BaseTool
        Anthropic::Helpers::Messages.tool_api_name(tool)
      in String | Symbol
        tool.to_s
      end
    @tool_overrides.store(name, nil)
    @pending_tool_changes << {type: :tool_removal, tool: {type: :tool_reference, name:}}
  end
end

#run_until_finishedArray<Anthropic::Models::BetaMessage>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



133
134
135
136
137
# File 'lib/anthropic/helpers/tools/runner.rb', line 133

def run_until_finished
  messages = []
  each_streaming { messages << _1.accumulated_message }
  messages
end