LangEx.Prebuilt.Swarm (LangEx v0.11.3)

Copy Markdown View Source

Builds a peer-to-peer team of agents that hand control to one another.

Every agent can transfer the conversation to any other agent. The currently active agent is tracked in the :active_agent state key and persisted across invocations via the graph's checkpointer, so a follow-up message resumes with whichever agent last held the conversation.

graph =
  LangEx.Prebuilt.Swarm.create(
    agents: [
      [name: :router, model: "gpt-4o", system_prompt: "Route the user."],
      [name: :refunds, model: "gpt-4o", system_prompt: "Handle refunds."]
    ],
    default_active_agent: :router,
    checkpointer: LangEx.Checkpointer.Memory
  )

{:ok, state} =
  LangEx.invoke(graph, %{messages: [LangEx.Message.human("I want a refund")]},
    config: [thread_id: "t-1"]
  )

Each agent is given a handoff tool (transfer_to_<peer>) for every other agent automatically; no manual wiring is required.

Summary

Functions

Builds and compiles a swarm graph.

Functions

create(opts)

@spec create(keyword()) :: LangEx.Graph.Compiled.t()

Builds and compiles a swarm graph.

Options

  • :agents (required) - list of member specs (keyword lists) forwarded to LangEx.Prebuilt.Member.build/1; each must include :name
  • :default_active_agent (required) - the agent that handles the first turn when no active agent is set
  • :checkpointer - persists :active_agent and messages across turns
  • :store - long-term memory backend shared by all members
  • :handoff_tool_prefix - prefix for generated handoff tool names (default names are "transfer_to_<peer>")
  • :add_agent_name - when true, each agent's replies are prefixed with "[<name>] " so peers can tell who said what (default false)