Skip to content

Multi-Agent API


GraphBuilder

python
from elsai.multiagent import GraphBuilder

Builder for constructing a Graph. Add nodes and edges, configure limits and hooks, then call .build().

GraphBuilder.add_node

python
builder.add_node(executor, node_id=None)

Add an Agent (or other MultiAgentBase instance) as a node. Returns the GraphNode instance. Auto-generates node_id from the executor's id or name when omitted.

GraphBuilder.add_edge

python
builder.add_edge(source, target, condition=None)

Create a directed edge so target runs after source and receives its output. Pass node ID strings or GraphNode objects returned from add_node() — not raw Agent instances.

GraphBuilder.set_max_node_executions

python
builder.set_max_node_executions(3)

Cap total node executions. Required for cyclic graphs.

GraphBuilder.set_session_manager

python
builder.set_session_manager(session_manager)

Attach a SessionManager for persistence across invocations.

GraphBuilder.set_hook_providers

python
builder.set_hook_providers([hook_provider])

Register HookProvider instances for multi-agent lifecycle events.

GraphBuilder.build

python
graph = builder.build()

Returns a runnable Graph instance.


Graph

python
from elsai.multiagent.graph import Graph

Deterministic, dependency-driven agent orchestration. Construct via GraphBuilder.build().

Graph.__call__

python
result = graph(prompt)

Execute the graph synchronously.

Graph.invoke_async

python
result = await graph.invoke_async(prompt)

Execute the graph asynchronously.

GraphResult

python
result.status              # "completed" | "failed" | "interrupted"
result.results             # dict[str, NodeResult]: per-node results
result.accumulated_usage   # Usage dict with inputTokens, outputTokens, totalTokens
result.accumulated_metrics # Metrics dict with latencyMs
result.execution_time      # int: total time in ms
result.total_nodes         # GraphResult only
result.execution_order     # GraphResult only: list of GraphNode

Each NodeResult exposes result (an AgentResult, nested MultiAgentResult, or Exception), status, execution_time, and accumulated_usage.


Swarm

python
from elsai.multiagent import Swarm

Autonomous, collaborative agent orchestration.

Swarm.__init__

python
Swarm(
    nodes,
    *,
    entry_point=None,
    max_handoffs=20,
    max_iterations=20,
    execution_timeout=900.0,
    node_timeout=300.0,
    id="default_swarm",
    hooks=None,
    plugins=None,
    session_manager=None,
    trace_attributes=None,
)
ParameterTypeDescription
nodeslist[Agent]Agents in the swarm
entry_pointAgent | NoneAgent to start with; defaults to the first node
max_handoffsintMaximum handoffs between agents (default 20)
max_iterationsintMaximum node executions within the swarm (default 20)
execution_timeoutfloatTotal swarm timeout in seconds (default 900.0)
node_timeoutfloatPer-node execution timeout in seconds (default 300.0)
idstrSwarm identifier for session management
hookslist | NoneLifecycle hooks
pluginslist | NonePlugins attached to the swarm
session_managerSessionManager | NoneSession backend for persistence
trace_attributesdict | NoneOpenTelemetry attributes for tracing

Swarm.__call__

python
result = swarm(prompt)

Swarm.invoke_async

python
result = await swarm.invoke_async(prompt)

SwarmResult

Extends GraphResult with:

python
result.node_history   # list[SwarmNode]: handoff sequence

Agent.as_tool

python
tool = agent.as_tool(
    *,
    name=None,
    description=None,
    preserve_context=False,
    mode="reject",
)

Wraps the agent as an AgentTool that can be passed to another agent's tools parameter.

ParameterDefaultDescription
nameagent.nameTool name shown to the model
descriptionagent.descriptionTool description
preserve_contextFalsePreserve conversation history between calls
mode"reject"Concurrency when the same tool is invoked in parallel: reject, queue, or spawn

Passing tools=[sub_agent] auto-wraps with mode="reject". For parallel calls to the same specialist, use sub_agent.as_tool(mode="spawn") or mode="queue".

See Agent as Tool — Concurrent invocation.


Multi-agent hooks

python
from elsai.hooks import (
    BeforeNodeCallEvent,
    AfterNodeCallEvent,
    MultiAgentInitializedEvent,
    BeforeMultiAgentInvocationEvent,
    AfterMultiAgentInvocationEvent,
)

BeforeNodeCallEvent

Fires before each node starts executing.

AttributeTypeDescription
sourceMultiAgentBaseThe graph or swarm orchestrator
node_idstrID of the node about to execute
invocation_statedict | NoneInvocation context
cancel_nodebool | strWritable — cancel this node when set

AfterNodeCallEvent

Fires after each node finishes.

AttributeTypeDescription
sourceMultiAgentBaseThe graph or swarm orchestrator
node_idstrID of the node that just completed
invocation_statedict | NoneInvocation context

A2A

Agent-to-Agent protocol support (A2A Protocol v1.0, a2a-sdk>=1.0.0). Requires elsai-agents[a2a]>=0.3.0.

  • Agent-to-Agent (A2A) — usage, session isolation, migration from v0.3
  • A2A APIA2AServer, A2AAgent, A2ASessionStore, stream normalizer, and handler options

Copyright © 2026 elsai foundry.