View Source Anthropix.Tool (Anthropix v0.1.0)

The Anthropix.Tool module allows you to let Claude call functions in your application.

The Anthropix.Tool.t/0 struct wraps around any function in your application - referenced functions, anonymous functions, or MFA style functions ({module(), atom(), list(term())}).

Remember, Clause is a language model, so all tools and parameters should be given clear descriptions to help Claude undertand how to call your functions correctly.

new/1 will validate that the wrapped function exists and has the correct arity.

Example

ticker_tool = Tool.new([
  name: "get_ticker_symbol",
  description: "Gets the stock ticker symbol for a company searched by name. Returns str: The ticker symbol for the company stock. Raises TickerNotFound: if no matching ticker symbol is found.",
  params: [
    %{name: "company_name", description: "The name of the company.", type: "string"}
  ],
  function: &MyApp.get_ticker_symbol/1
])

price_tool = Tool.new([
  name: "get_current_stock_price",
  description: "Gets the current stock price for a company. Returns float: The current stock price. Raises ValueError: if the input symbol is invalid/unknown.",
  params: [
    %{name: "symbol", description: "The stock symbol of the company to get the price for.", type: "string"}
  ],
  function: {MyApp, :get_current_stock_price, ["other", "args"]}
])

Note that when the MFA style is used, the list of arguments in the MFA tuple are treated as extra arguments positioned after the arguments defined in the tool params. Using the price_tool example above, the following function should exist:

defmodule MyApp do
  def get_current_stock_price(symbol, other, args) do
    # ...
  end
end

For a full example of function calling with Claude, see Anthropix.Agent.

Error handling

When used with Anthropix.Agent, when an exception is raised by the tool, the name of the exception is passed back to Claude. It is therefore recommended to describe your errors in the tool description.

case get_current_stock_price(ticker) do
  nil -> raise ValueError, "stock not found"
  price -> price
end

Summary

Types

Tool parameter

t()

Tool struct

Functions

Creates a new Tool struct from the given parameters.

Types

@type param() :: %{name: String.t(), description: String.t(), type: String.t()}

Tool parameter

@type t() :: %Anthropix.Tool{
  description: String.t(),
  function: function() | {atom(), atom(), list()},
  name: String.t(),
  params: [param()]
}

Tool struct

Functions

@spec new(keyword()) :: t()

Creates a new Tool struct from the given parameters.

Options

  • :name (String.t/0) - Required. The function name.
  • :description (String.t/0) - Required. A plaintext explanation of what the function does, including return value and type.
  • :params (list of map/0) - Required. The expected parameters, their types, and descriptions.
  • :function (term/0) - Required. The tool function.

Parameter structure

Each parameter is a map with the following fields:

  • :name (String.t/0) - Required. Parameter name.
  • :description (String.t/0) - Required. A plaintext explanation of what the function
  • :type (String.t/0) - Required. todo

See the example as the top of this page.