Executes tool calls and returns result messages.
Calls run in the order the model made them. Consecutive calls to tools
that are safe to run concurrently (the default; see
Alloy.Tool.concurrent?/0) run in parallel; a call to a tool that
returns concurrent?: false waits for everything before it and runs
alone.
:tool_concurrency limits parallel calls (defaults to the scheduler count).
:tool_validation defaults to required-key checks; :schema enables optional
JSON Schema validation. Each dispatched call outcome passes through
:after_tool_call middleware before transcript and event publication. If the
run's :timeout_ms deadline expires before a batch starts, unstarted calls
(including middleware-blocked calls in that batch) still receive matching
error tool_result blocks in the transcript (preserving any :before_tool_call
block reason) without emitting :tool_start/:tool_end, running
:after_tool_call, or appearing in result.tool_calls.
Each call runs in an unlinked task under Alloy.TaskSupervisor, bounded
by the agent's :tool_timeout. A tool that raises, exits, throws or
times out produces an is_error tool result instead of crashing the
agent.
Summary
Functions
Executes tool_calls against tool_fns and returns a user Alloy.Message
holding matching tool_result blocks in call order, or {:halted, reason}
if :before_tool_call middleware halts the round.
Executes tool_calls against tool_fns and returns
{:ok, result_message, tool_call_metadata} or {:halted, reason}.
Executes a single tool call through the same pipeline as execute_all/4.
Functions
@spec execute_all( [map()], %{required(String.t()) => Alloy.Tool.Registry.tool()}, Alloy.Agent.State.t() ) :: Alloy.Message.t() | {:halted, String.t()}
Executes tool_calls against tool_fns and returns a user Alloy.Message
holding matching tool_result blocks in call order, or {:halted, reason}
if :before_tool_call middleware halts the round.
Equivalent to execute_all/4 with default options ([]), discarding the
per-call execution metadata list.
@spec execute_all( [map()], %{required(String.t()) => Alloy.Tool.Registry.tool()}, Alloy.Agent.State.t(), keyword() ) :: {:ok, Alloy.Message.t(), [map()]} | {:halted, String.t()}
Executes tool_calls against tool_fns and returns
{:ok, result_message, tool_call_metadata} or {:halted, reason}.
Consecutive calls to concurrency-safe tools (Alloy.Tool.concurrent?/0)
run in parallel up to :tool_concurrency; sequential tools wait for earlier
calls and run alone in call order. Accepts the same execution and event
coordination options as run_tool/3 (:tool_timeout, :run_deadline,
:cancel_token, :parent_call_id, :on_event, :event_subscriber?,
:event_seq_ref, :event_correlation_id, :event_turn).
@spec run_tool(map(), Alloy.Agent.State.t(), keyword()) :: {:ok, map(), map() | nil} | {:halted, String.t()}
Executes a single tool call through the same pipeline as execute_all/4.
Runs :before_tool_call middleware, input required-key and optional JSON
Schema validation, supervised worker execution bounded by :tool_timeout and
:run_deadline with cooperative :cancel_token cancellation, crash/exit
sanitization, incremental :tool_update marshalling back to the caller
process, :after_tool_call middleware, result truncation, and :tool_start /
:tool_update / :tool_end events.
tool_call is a map with :id, :name, and :input (the same shape
returned by Alloy.Message.tool_calls/1).
Options
:tool_fns— tool registry map overridingstate.tool_fns:tool_timeout— positive integer ms overridingstate.config.tool_timeout:run_deadline— monotonic ms deadline; clamped tostate.run_deadlinewhen the outer state already carries a deadline:cancel_token—Alloy.Cancel.t()ornil; combined withstate.config.cancel_tokenso passingnilor a child token never disarms outer cancellation:parent_call_id— optional non-empty UTF-8String.t()attached as:parent_call_idon emitted:tool_start,:tool_update, and:tool_endevents, tool telemetry metadata, and returned tool-call metadata:on_event—(event -> any())ornil, invoked in the caller process with raw{:tool_start | :tool_update | :tool_end, map()}tuples (not wrapped inAlloy.Eventsv1 envelopes and not rescued; to emit v1 envelopes inside an existing host stream, passon_event: &Alloy.Events.emit(event_opts, turn, &1)together with:event_subscriber?,:event_seq_ref,:event_correlation_id, and:event_turnfromAlloy.Events.normalize_opts/2):event_subscriber?— boolean ornilenabling:tool_updatestreaming (defaults tois_function(opts[:on_event], 1)):event_seq_ref,:event_correlation_id,:event_turn— event envelope coordination options (niluses defaults)
Returns
{:ok, result_block, tool_call_meta}—result_blockis a normalizedtool_resultcontent block (%{type: "tool_result", ...}), andtool_call_metais the execution metadata map (ornilif the call was not started because:cancel_tokenwas already cancelled or:run_deadlinehad already expired){:halted, reason}—:before_tool_callmiddleware halted execution