ExAgent.Patterns.Subagents (ExAgent v0.2.0)

Copy Markdown View Source

Centralized orchestration pattern.

A main agent invokes specialized subagents as tool calls. Subagents are transient, stateless processes with isolated contexts — they run a single LLM call and return the result without maintaining state.

Summary

Functions

Converts subagent specifications into Tool structs for use by an orchestrator agent.

Invokes a subagent synchronously with a single query.

Invokes multiple subagents in parallel using Task.async_stream.

Types

subagent_spec()

@type subagent_spec() :: %{
  name: String.t(),
  description: String.t(),
  provider: struct(),
  system_prompt: String.t() | nil,
  tools: [ExAgent.Tool.t()]
}

Functions

build_orchestrator_tools(specs)

@spec build_orchestrator_tools([subagent_spec()]) :: [ExAgent.Tool.t()]

Converts subagent specifications into Tool structs for use by an orchestrator agent.

Each subagent becomes a tool whose function spawns an ephemeral LLM call. The tool's parameters accept a "query" string.

invoke_subagent(spec, query)

@spec invoke_subagent(subagent_spec(), String.t()) ::
  {:ok, String.t()} | {:error, term()}

Invokes a subagent synchronously with a single query.

Creates a fresh context, calls Provider.chat/3 directly (no GenServer), and returns the assistant's response content.

invoke_subagents_parallel(specs_with_inputs, opts \\ [])

@spec invoke_subagents_parallel(
  [{subagent_spec(), String.t()}],
  keyword()
) :: [{String.t(), {:ok, String.t()} | {:error, term()}}]

Invokes multiple subagents in parallel using Task.async_stream.

Returns a list of {spec_name, result} tuples.