ExAgent. Tools
(ExAgent v3.0.0)
View Source
Define tools as plain Elixir functions, with their JSON Schema derived from
:: type annotations and @doc strings — no hand-written schemas.
Stability — Stable.
Define a module of tools with derived schemas:
defmodule MyApp.Tools do
use ExAgent.Tools
@doc "Get the weather for a city."
deftool get_weather(ctx, city :: String.t()) do
{:ok, ~s(#{city}: 22C, clear)}
end
@doc "Add two numbers."
tool_plain add(a :: integer(), b :: integer()) do
{:ok, a + b}
end
endThen wire them into an agent:
agent = ExAgent.new(model: "test", tools: MyApp.Tools.tools())Rules:
deftool— the first argument is theRunContext(namedctxby convention); remaining typed args become the tool's parameters.tool_plain— no context; every typed arg is a parameter.- Each parameter is described by
name :: type; supported types live inExAgent.Schema. Untyped params collapse to an unconstrained schema. Use named variables in the signature and match patterns in the body. For an optional argument, useExAgent.Tool.new/1, omit the property fromrequired, and supply its fallback in the callback. - A
whenguard applies to the generated Elixir function. A call that passes schema validation but misses the guard raisesFunctionClauseErrorif no clause matches. An annotation such asvalue :: 1..5advertises the inclusive integer range to the model and validates it before the call. - The function returns an
ExAgent.Tool.result/0:value,{:ok, value},{:tool_error, message}for a final outcome the model can act on,{:retry, message}(orraise ExAgent.ModelRetry) to ask for corrected input, or{:error, reason}for a failure that ends the run.
Summary
Functions
Import the tool definition macros and generate the module's tools/0.
Define a tool that receives the RunContext as its first argument.
Define a context-aware tool with execution controls. Pass effect: :read
for a read-only tool or effect: :write (the default) for a write.
Pass requires_deferral: true for a tool that needs an inbox for its result.
The flag defaults to false. Unknown, repeated or invalid controls raise
ArgumentError when the host tool module compiles.
Define a tool that takes only its parameters (no context).
Functions
Import the tool definition macros and generate the module's tools/0.
Define a tool that receives the RunContext as its first argument.
Define a context-aware tool with execution controls. Pass effect: :read
for a read-only tool or effect: :write (the default) for a write.
Pass requires_deferral: true for a tool that needs an inbox for its result.
The flag defaults to false. Unknown, repeated or invalid controls raise
ArgumentError when the host tool module compiles.
Define a tool that takes only its parameters (no context).