ExAgent

Hex Version Hex Docs License Release

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

Installation

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

Quick start

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 for OpenAI, Gemini, OpenRouter, Ollama, vLLM and per-tenant credentials). In Phoenix, mix igniter.install exagent --chat --yes scaffolds a complete LiveView 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.

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 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 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
Conversation ExAgent.Server, ExAgent.Conversations, ExAgent.Chat Supervised history, streaming & LiveView events Runtime and events
Durability & approvals store:, ExAgent.Continuation, ExAgent.resume/3 Crash-safe checkpoints & human approval pauses Durability and approvals
Coordination ExAgent.Coordination, ExAgent.Session, ExAgent.Handoff Delegation, shared state & durable workflows Coordination

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:

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

Documentation and examples

ExAgent separates human tutorials from LLM contract references:

Contributing

Bug reports and pull requests are welcome on GitHub. 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:

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.