Packages

A layered agent framework for Elixir: structured output, tool-calling, streaming, stateful supervised agents, multi-agent sessions, durable persistence, compaction/cost/permissions and MCP — powered by the BEAM.

Current section

Files

Jump to
exagent README.md
Raw

README.md

# ExAgent

[![Hex Version](https://img.shields.io/hexpm/v/exagent.svg)](https://hex.pm/packages/exagent)
[![Hex Docs](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/exagent)
[![License](https://img.shields.io/hexpm/l/exagent.svg)](https://github.com/akorda-software/exagent/blob/main/LICENSE)
[![Release](https://github.com/akorda-software/exagent/actions/workflows/release.yml/badge.svg)](https://github.com/akorda-software/exagent/actions/workflows/release.yml)

<!-- MDOC -->

**A production agent framework for Elixir.** Define tools as ordinary functions,
get Ecto-validated structs back, stream live updates into Phoenix, and pause
sensitive actions for human approval across restarts. Start with one function
call — every OTP, persistence and coordination layer is opt-in.

### What ExAgent simplifies

- **Zero hand-written JSON Schemas:** `deftool` derives tool schemas from Elixir
  type annotations and validates arguments before your function ever runs.
- **Typed, self-correcting outputs:** Pass an `Ecto.Schema` `embedded_schema`
  module (with optional `changeset/2`) as `output:`; JSON Schema and changeset
  errors feed back to the model automatically (up to `output_retries`) until a
  valid struct is returned.
- **One loop for buffered, streamed & local models:** Powered by stock
  [ReqLLM](https://hex.pm/packages/req_llm) across OpenAI, Anthropic, Google
  Gemini, OpenRouter and local Ollama / vLLM / llama.cpp endpoints (including
  thinking models and malformed tool-envelope auto-retry).
  [Supported provider profiles](docs/guides/support.md)
- **LiveView-ready supervised conversations:** `ExAgent.Server` and
  `ExAgent.Conversations` handle history, bounded queues, turn steering,
  cancellation, demand-paced events and SSE / AI SDK / AG-UI adapters.
- **Crash-safe human approvals:** `ExAgent.Continuation` checkpoints runs in
  PostgreSQL or ETS and never replays an unconfirmed external side effect.
- **Multi-agent coordination with shared budgets:** Scoped delegation, handoffs,
  shared-state `Session`s and durable `Composition` / `Flow` workflows inherit
  ancestor permissions, deadlines and spend limits.
- **Skills, MCP, observability & offline CI:** On-demand Agent Skills, MCP
  client & server, `:telemetry` + optional OpenTelemetry (Langfuse / Opik),
  recorded cassettes and `mix exagent.eval` quality gates.

## Installation

```elixir
def deps do
  [{:exagent, "~> 3.0.0"}]
end
```

Verified on Elixir 1.18.4 / OTP 28 and Elixir 1.20.0 / OTP 29. A one-shot run
needs no database. [Credentials and `.env` loading](docs/guides/getting-started.md)

## Quick start

```elixir
agent = ExAgent.new(model: "test", instructions: "Be concise.")
{:ok, result} = ExAgent.run(agent, "Hello!")

%{output: result.output, status: result.status, requests: result.request_count}
# => %{output: "a test response", status: :succeeded, requests: 1}
```

`"test"` runs 100% offline using the same execution loop as real providers. With
`ANTHROPIC_API_KEY` set, swap to `model: "anthropic:claude-sonnet-5-5"` (see
[Models and limits](docs/guides/models-and-limits.md) for OpenAI, Gemini,
OpenRouter, Ollama, vLLM and per-tenant credentials). In Phoenix,
`mix igniter.install exagent --chat --yes` scaffolds a
[complete LiveView chat](docs/guides/getting-started.md#generate-a-phoenix-chat)
with PostgreSQL checkpoints and durable approvals.

## Tools and typed output

For the `MyApp.Forecast` Ecto output schema below, add
`{:ecto, "~> 3.14 and >= 3.14.3"}` to your application's dependencies.

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

  @doc "Get the weather for a city."
  deftool get_weather(_ctx, city :: String.t(), days :: integer()) do
    {:ok, "#{city}: sunny for #{days} day(s)"}
  end
end

defmodule MyApp.Forecast do
  use Ecto.Schema

  @primary_key false
  embedded_schema do
    field :city, :string
    field :temp_c, :float
  end

  def changeset(forecast, attrs) do
    forecast
    |> Ecto.Changeset.cast(attrs, [:city, :temp_c])
    |> Ecto.Changeset.validate_required([:city, :temp_c])
    |> Ecto.Changeset.validate_number(:temp_c, greater_than: -100, less_than: 100)
  end
end

alias ExAgent.Message.Part.ToolCall

# Offline; with a key use model = "anthropic:claude-sonnet-5-5"
model = %ExAgent.Models.Test{
  script: [
    {:tool_calls, [%ToolCall{tool_name: "get_weather", args: %{"city" => "Madrid", "days" => 1}}]},
    {:tool_calls,
     [%ToolCall{tool_name: "final_result", args: %{"city" => "Madrid", "temp_c" => 21.5}}]}
  ]
}

agent = ExAgent.new(model: model, tools: MyApp.Weather.tools(), output: MyApp.Forecast)
{:ok, result} = ExAgent.run(agent, "What is the weather in Madrid?")
result.output
# => %MyApp.Forecast{city: "Madrid", temp_c: 21.5}
```

ExAgent derives both JSON Schemas automatically, validates `get_weather`'s
arguments before invoking it, checks `MyApp.Forecast.changeset/2` on the model's
answer (sending validation errors back for correction), and returns a real
`%MyApp.Forecast{}` struct.
[Tools and structured output](docs/guides/tools-and-output.md) covers
permissions, guardrails, sandboxes and approval gates.

## Layers at a glance

Start with the top row and add layers only as your application needs them — see
[Mental model and architecture](docs/guides/mental-model.md) for a full pattern
decision table.

| Layer | Entry points | What it simplifies | Guide |
|---|---|---|---|
| Run | `ExAgent.run/3`, `ExAgent.run_stream/3` | Stateless tool loops & typed Ecto outputs | [Tools and output](docs/guides/tools-and-output.md) |
| Conversation | `ExAgent.Server`, `ExAgent.Conversations`, `ExAgent.Chat` | Supervised history, streaming & LiveView events | [Runtime and events](docs/guides/runtime-and-events.md) |
| Durability & approvals | `store:`, `ExAgent.Continuation`, `ExAgent.resume/3` | Crash-safe checkpoints & human approval pauses | [Durability and approvals](docs/guides/durability-and-approvals.md) |
| Coordination | `ExAgent.Coordination`, `ExAgent.Session`, `ExAgent.Handoff` | Delegation, shared state & durable workflows | [Coordination](docs/guides/coordination.md) |

### Sync coding-agent guidance (LLMs)

ExAgent ships a dedicated machine-optimized documentation track for coding
agents (`usage-rules.md`, 11 package skills, `llms.txt`, single-fetch
`llms-full.txt`, and contract sheets). To sync ExAgent's rules and skills into
your app automatically, add `{:usage_rules, "~> 1.3.1", only: :dev, runtime: false}`
to `deps/0` and configure `project/0`:

```elixir
usage_rules: [
  file: "AGENTS.md",
  usage_rules: [:exagent],
  skills: [location: ".agents/skills", package_skills: [:exagent]]
]
```

Run `mix deps.get && mix usage_rules.sync --yes`. Codex and Cursor read
`AGENTS.md` and `.agents/skills`; for Claude Code use `file: "CLAUDE.md"` and
`location: ".claude/skills"`. Re-sync after upgrading ExAgent. The installed
dependency also includes `usage-rules.md` and
`usage-rules/skills/<name>/SKILL.md` for direct reading.
[UsageRules package skills](https://github.com/ash-project/usage_rules/blob/v1.3.1/README.md#pre-built-skills)

<!-- MDOC -->

## Documentation and examples

ExAgent separates **human tutorials** from **LLM contract references**:

- **For humans:** Start at [Welcome](docs/home.md),
  [Mental model and architecture](docs/guides/mental-model.md),
  [Getting started](docs/guides/getting-started.md) or the
  [Documentation index](docs/guides/index.md). Upgrading? Read
  [Migration](docs/guides/migration.md) and the
  [changelog](https://github.com/akorda-software/exagent/blob/main/docs/changelog.md).
- **For LLMs & coding agents:** Open
  [Coding-agent integration notes](docs/guides/agents.md),
  `llms.txt` / `llms-full.txt` on [HexDocs](https://hexdocs.pm/exagent), or
  sync the bundled skills with `mix usage_rules.sync`.
- **Runnable examples:** From a checkout, `mix run examples/demo.exs` runs
  offline; the [`examples/`](https://github.com/akorda-software/exagent/tree/main/examples)
  directory covers Server, Sessions, Flow, durable approvals and evaluations.

## Contributing

Bug reports and pull requests are welcome on
[GitHub](https://github.com/akorda-software/exagent). Include a minimal
public-API example, the package and Elixir/OTP versions and a sanitized error;
keep credentials and application data out of reports. Source changes follow
`AGENTS.md` and pass the offline checks:

```bash
EXAGENT_OFFLINE=1 MIX_ENV=test mix compile --warnings-as-errors
EXAGENT_OFFLINE=1 MIX_ENV=test mix test --warnings-as-errors
```

## License

Copyright (c) 2025 kukapu. Licensed under the MIT License; see [LICENSE](./LICENSE).