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

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

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, thread: 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:



751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
# File 'lib/anthropic/helpers/tools/runner.rb', line 751

def initialize(client, params:, max_iterations: nil, thread: nil)
  @client = client
  @thread = thread
  @given_count = thread&.messages.to_a.length
  @params = params.to_h
  reject_compaction_param!(@params)
  @finished = false
  @max_iterations = max_iterations
  @iteration_count = 0
  @last_response = nil
  @pending_tool_changes = []
  @tool_overrides = {}
  @pending_compaction = nil
  @compaction_phase = nil
  @tool_runner_otel_context = nil
  @app_otel_context = nil
  @stopped_by_app = false
end

Instance Attribute Details

#params ⇒ Anthropic::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

This method returns an undefined value.

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:



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/anthropic/helpers/tools/runner.rb', line 71

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



115
116
117
118
119
120
# File 'lib/anthropic/helpers/tools/runner.rb', line 115

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

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

#each_message {|, arg0| ... } ⇒ 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.

Yields:

Yield Parameters:

Yield Returns:

  • (void)


151
152
153
154
155
156
157
158
159
160
161
# File 'lib/anthropic/helpers/tools/runner.rb', line 151

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

  fold do |params|
    message = create_message(params)
    in_app { blk.call(message) }
    [false, message]
  end
end

#each_streaming {|, arg0| ... } ⇒ 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.



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/anthropic/helpers/tools/runner.rb', line 164

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

  fold do |params|
    stream = stream_message(params)
    in_app { 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.

This method returns an undefined value.



58
59
60
# File 'lib/anthropic/helpers/tools/runner.rb', line 58

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:



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

def finished? = @finished

#next_message ⇒ 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.



126
127
128
129
130
131
132
133
# File 'lib/anthropic/helpers/tools/runner.rb', line 126

def next_message
  message = nil
  fold do |params|
    message = create_message(params)
    [true, message]
  end
  message
end

#remove_tools(*tools) ⇒ void

This method returns an undefined value.

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:



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/anthropic/helpers/tools/runner.rb', line 90

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_finished ⇒ Array<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:



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/anthropic/helpers/tools/runner.rb', line 136

def run_until_finished
  messages = []
  # Not `each_streaming`: an error from this block is the runner's, not the application's.
  fold do |params|
    stream = stream_message(params)
    message = stream.accumulated_message
    messages << message
    [false, message]
  ensure
    stream&.close
  end
  messages.compact
end