Legion.Tool behaviour (Legion v0.5.0)

View Source

use Legion.Tool to mark a module as a tool available to agents.

By default, description/0 returns the module's source code so the LLM knows what functions are available.

Overridable

  • description/0 — override to return a hand-written summary instead of the source code. Defaults to the module's source code.
  • description/1 — like description/0, but receives the active sandbox module, for tools whose usage differs by generated language. Preferred over description/0 when defined.
  • extra_allowed_modules/0 — override to return additional modules that the sandbox should alias and permit when this tool is available. Defaults to []. Useful for tools like Legion.Tools.AgentTool that dispatch to other modules the agent needs to reference by name.

Example

defmodule MyApp.WeatherTool do
  use Legion.Tool

  def description do
    """
    WeatherTool — fetches current weather data.

    ## Functions
    - `current(city)` — returns weather JSON for the given city name.
    """
  end

  @doc "Returns current weather for a city."
  def current(city) do
    Req.get!("https://wttr.in/#{city}?format=j1").body
  end
end

External modules as tools

A module that does not use Legion.Tool (e.g. Req) can still be listed as a tool if its source code is registered at compile time, so the LLM can read what it offers:

config :legion, extra_source_modules: [Req]

Summary

Callbacks

Description of the tool shown to the LLM. Defaults to the module's source code.

Description of the tool for the given sandbox, shown to the LLM. Preferred over description/0 when defined, so a tool can tailor its usage notes to the generated language.

Callbacks

description()

@callback description() :: String.t()

Description of the tool shown to the LLM. Defaults to the module's source code.

description(sandbox)

(optional)
@callback description(sandbox :: module()) :: String.t()

Description of the tool for the given sandbox, shown to the LLM. Preferred over description/0 when defined, so a tool can tailor its usage notes to the generated language.

extra_allowed_modules()

@callback extra_allowed_modules() :: [module()]