Packages

Derive LLM tool-call schemas from Elixir @spec and @doc, with a runtime dispatcher.

Current section

Files

Jump to
tool_call README.md
Raw

README.md

# ToolCall

[![CI](https://github.com/gilbertwong96/tool_call/actions/workflows/ci.yml/badge.svg)](https://github.com/gilbertwong96/tool_call/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/gilbertwong96/tool_call/graph/badge.svg)](https://codecov.io/gh/gilbertwong96/tool_call)
[![Elixir](https://img.shields.io/badge/elixir-1.18+-purple.svg)](https://elixir-lang.org)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
[![Hex.pm](https://img.shields.io/hexpm/v/tool_call.svg)](https://hex.pm/packages/tool_call)

Derive LLM tool-call schemas from Elixir `@spec` and `@doc`, then dispatch the
call back to the function.

No DSL, no second schema, no runtime reading of `Docs`/`Dbgi` chunks.

```elixir
defmodule MyApp.Tools.Weather do
  use ToolCall

  @doc """
  Get the forecast for a city.

  ## Parameters
    * `city` - city name
    * `days` - how many days ahead
  """
  @spec get_weather(city :: String.t(), days :: pos_integer() | nil) ::
          {:ok, map()} | {:error, String.t()}
  def get_weather(city, days \\ 3) do
    {:ok, %{city: city, days: days, temp: 21.5}}
  end
end
```

```elixir
ToolCall.emit(MyApp.Tools.Weather, :openai)

ToolCall.run(MyApp.Tools.Weather, :get_weather, %{"city" => "Oslo"})
#=> {:ok, %{city: "Oslo", days: 3, temp: 21.5}}
```

## Installation

```elixir
def deps do
  [tool_call: "~> 0.4"]
end
```

Requires Elixir `~> 1.18` and Erlang/OTP 27.1+. The OTP floor is enforced at
compile time; there are no runtime dependencies.

## What you get

- `ToolCall.emit/3` — OpenAI, Anthropic, MCP and Gemini schemas; the
  provider-call targets are precompiled per module, MCP and Gemini on demand.
- `ToolCall.Schema.Gemini.to_gemini/2` — adapts a JSON Schema you already hold to
  Gemini's `parameters` subset, with an `:on_ref` policy for local `$defs`.
- `ToolCall.run/4` — required/unknown argument checks, coercion, exception
  wrapping, optional result validation, per-call timeouts, approval policies and
  dispatch audit hooks.
- `ToolCall.Runner.resolve/4` + `ToolCall.Runner.execute/2` — split validation
  and invocation; `resolve` returns a `%ToolCall.Call{}` that can be gated and
  executed later.
- `ToolCall.Approval` + `ToolCall.Approval.Request` — approval policy hook and a
  serializable pending request for human-in-the-loop flows.
- `ToolCall.Audit` + `ToolCall.Audit.Entry` — one dispatch audit entry per
  attempt.
- `ToolCall.Timeout` — timeout policy behaviour and per-invocation task runner.
- `ToolCall.tools/1` and `ToolCall.describe/2` — the IR and provider-neutral
  schema descriptions.
- `ToolCall.Stream` — folds streaming tool-call deltas (OpenAI, Anthropic,
  Gemini) into complete calls for `run/3`.
- `use ToolCall, types: [...]` — custom `ToolCall.TypeProvider` integrations;
  `ToolCall.Providers.URI` and `ToolCall.Providers.Version` are optional string
  providers for those standard-library structs.
- `use ToolCall, timeout: ...`, `approval: ...`, `audit: ...` — module-level
  policies; call `ToolCall.validate_policies!/1` at startup to check them.
- `ToolCall.PolicyValidation` — one-shot supervision child (one per supervisor,
  with all modules) that runs the startup policy validation before the rest of
  the tree.
- `mix tool_call.export` — JSON for OpenAI, Anthropic, MCP or Gemini.
- `mix tool_call.clean` — removes orphaned typedoc companion beams.

## Performance

A local microbenchmark compares the schema and dispatch paths with ReqLLM.
Numbers are medians from one reference run on an Apple M1 Pro, Elixir 1.20.5,
OTP 29; they are not end-to-end LLM timings.

| Case | ToolCall | ReqLLM |
|---|---:|---:|
| OpenAI schema, 1 tool | 0.46 µs / 72 B | 0.42 µs / 600 B |
| OpenAI schema, 10 tools | 0.46 µs / 72 B | 3.96 µs / 5.94 KB |
| `add`, string keys | 0.63 µs / 200 B | 0.54 µs / 1.47 KB |
| `add`, atom keys | 0.54 µs / 240 B | 0.50 µs / 1.42 KB |
| `describe`, generic path | 2.54 µs / 2.38 KB | 0.88 µs / 3.01 KB |
| `ReqLLM.Tool.new!` setup | compile-time | 3.33 µs / 5.03 KB |

ToolCall precompiles provider schemas and uses a generated fast path for simple
scalar arguments. Complex arguments use the generic runner, which also carries
key checks, coercion, timeout, approval and audit support. See
[`bench/README.md`](https://github.com/gilbertwong96/tool_call/blob/main/bench/README.md)
for the cases and reproduction steps.

## Documentation

The full documentation is generated by `mix docs`:

- [`CHANGELOG.md`](CHANGELOG.md) — release history.
- [`docs/usage.md`](docs/usage.md) — defining, emitting, dispatching, exporting,
  conventions, and error terms.
- [`docs/design.md`](docs/design.md) — architecture, type mapping, typedoc
  companions, provider subsets, and design boundaries.
- [`docs/projection.md`](docs/projection.md) — canonical target schema matrix
  and loss notes.
- [`docs/pitfalls.md`](docs/pitfalls.md) — compile-time traps and their
  trade-offs.
- [`docs/number-handling.md`](docs/number-handling.md) — large integers and JSON
  precision.
- [`docs/roadmap.md`](docs/roadmap.md) — current status and remaining work.

## Development

```sh
mix deps.get
mix ci
mix coveralls.html
```

`mix ci` runs compile/format checks, tests, `spec_lint`, Credo, Dialyzer,
ExDNA and Reach. `mix coveralls.html` writes the HTML coverage report to
`cover/`. CI uploads the report to Codecov when `CODECOV_TOKEN` is configured.

## License

MIT