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
end

Then wire them into an agent:

agent = ExAgent.new(model: "test", tools: MyApp.Tools.tools())

Rules:

  • deftool — the first argument is the RunContext (named ctx by 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 in ExAgent.Schema. Untyped params collapse to an unconstrained schema. Use named variables in the signature and match patterns in the body. For an optional argument, use ExAgent.Tool.new/1, omit the property from required, and supply its fallback in the callback.
  • A when guard applies to the generated Elixir function. A call that passes schema validation but misses the guard raises FunctionClauseError if no clause matches. An annotation such as value :: 1..5 advertises 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} (or raise 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

__using__(opts)

(macro)

Import the tool definition macros and generate the module's tools/0.

deftool(head, list)

(macro)

Define a tool that receives the RunContext as its first argument.

deftool(head, opts, list)

(macro)

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.

tool_plain(head, list)

(macro)

Define a tool that takes only its parameters (no context).