Skip to content

Documentation navigation

Multi-Agent

Coordinate specialized agents using Swarm workflows.

#Multi-Agent Swarms

A swarm is a team of specialist agents that hand work to each other. You describe who does what, who depends on whom, and which member writes the final answer. The runtime coordinates the run.

#What a swarm is

A swarm works like a small project team. One person kicks things off, others contribute a specialty, and one person owns the finished deliverable. A Swarm models that:

  • Each member is an Agent with a focused role, such as researcher or writer.
  • The runtime selects members from the roster according to the request and declared dependencies. List order does not designate an entry agent.
  • Members hand work to each other through declared dependencies, so an analyst can wait for the researcher's output before starting.
  • One member (or one swarm-level tool) is designated to produce the final output the caller receives.

Why not one big agent? Specialists keep each agent's instructions and tools narrow, let independent work run in parallel, and make a multi-step workflow explicit instead of burying it in one long prompt.

A swarm is an Agent sibling. Both Agent and Swarm are scopes. They share tool registration, events and final-tool support, plus batching and scheduling. The Agent-only .structured_output(Model) builder is not available on Swarm.

#Building a swarm

Create the member agents first, then group them. The first member can supply the swarm's default client. That does not make it run first.

Python
from maivn import Agent, Swarm
from maivn.messages import HumanMessage

researcher = Agent(
    name='researcher',
    description='Finds and gathers information on a topic',
    system_prompt='You research topics thoroughly and cite what you find.',
    api_key='your-api-key',
)

analyst = Agent(
    name='analyst',
    description='Turns research into insights',
    system_prompt='You analyze findings and extract the key takeaways.',
    api_key='your-api-key',
)

writer = Agent(
    name='writer',
    description='Writes the final article',
    system_prompt='You write clear, well-structured content.',
    api_key='your-api-key',
    use_as_final_output=True,  # this member owns the final answer
)

swarm = Swarm(
    name='content_team',
    description='Researches a topic and writes an article about it',
    agents=[researcher, analyst, writer],
)

Every member needs credentials the same way a standalone Agent does: pass an api_key or a shared client to each agent.

#Managing members after construction

You can also build a swarm incrementally:

Python
swarm = Swarm(name='content_team', agents=[])

swarm.add_agent(researcher)
swarm.add_agent(analyst)
swarm.add_agent(writer)

Inspect or look up members at any time:

Python
for agent in swarm.list_agents():
    print(f'{agent.name}: {agent.description}')

# Look up a member from the public roster.
member = next((item for item in swarm.list_agents() if item.name == 'writer'), None)

The current SDK exposes add_agent and list_agents, but no get_agent or get_swarm method. Retain application references when you need the parent or a particular member. A deployed agent identity defaults to its name.

#Member registration with dependencies

Adding an agent to the list registers it as a member. To declare how members depend on each other, use the swarm.member decorator builder. It registers the agent and attaches dependency metadata to the generated agent-invocation tool, the same vocabulary you already use for tools, applied at the member level.

Python
from maivn import Agent, Swarm, depends_on_tool

swarm = Swarm(name='content_team')

# A swarm-level dependency tool
@swarm.toolify(description='Load editorial context')
def load_context() -> dict:
    return {'audience': 'technical leaders'}

# Register a member whose invocation depends on a swarm tool's output
@depends_on_tool(load_context, arg_name='context')
def build_researcher() -> Agent:
    return Agent(name='researcher', api_key='your-api-key')

researcher = swarm.member(build_researcher)

# Register a member that depends on another member's output
writer = swarm.member.depends_on_agent(
    researcher,
    arg_name='research_notes',
)(Agent(name='writer', api_key='your-api-key', use_as_final_output=True))

Keep the concrete Agent reference when later member dependencies need it.

The member builder is chainable and has two methods:

Builder methodWhat it does
swarm.member.depends_on_agentWait for another member and inject its output
swarm.member.depends_on_toolWait for a swarm-level or member tool and inject its result before the member runs
Python
from maivn import Agent, Swarm

reviewer = swarm.member.depends_on_agent(
    writer, arg_name='draft',
).depends_on_tool(
    load_context, arg_name='context',
)(Agent(name='reviewer', api_key='your-api-key'))

The other dependency decorators (depends_on_await_for, depends_on_reevaluate, depends_on_interrupt and depends_on_private_data) belong on tools, not on member builders. To order a member after a tool, or to collect user input for it, put that decorator on a swarm-level tool and have the member depend on the tool with depends_on_tool.

Private data in swarms. Swarm has no private_data field, and a swarm run does not send a member's private_data to the runtime. Use RedactedMessage.known_pii_values for values that arrive in the messages, or run private-data work in a standalone Agent. See Private Data.

#Designated final output

A swarm produces one final answer. You decide which member owns it.

#One member owns the answer

Set use_as_final_output=True on exactly one member. That agent's output becomes the user-facing response.

Python
writer = Agent(
    name='writer',
    api_key='your-api-key',
    use_as_final_output=True,
)

At most one member across the swarm may carry this flag. If two do, swarm.validate_tool_configuration() raises ValueError, and so does swarm.invoke(..., force_final_tool=True), before it sends anything.

#A swarm-scope final tool

Instead of designating an agent, you can define a single typed report at the swarm level with final_tool=True. The swarm coordinates its members and emits this structured object as the result:

Python
from pydantic import BaseModel

@swarm.toolify(final_tool=True)
class TeamReport(BaseModel):
    """Combined output from the whole team."""
    research_summary: str
    analysis_insights: list[str]
    final_article: str

#When you need both

If multiple members own final tools, designate one member with use_as_final_output=True. A swarm-level final tool cannot be combined with a member carrying that flag. Member-local final tools can still produce intermediate results when the swarm owns its final output.

The SDK checks these rules in two places. swarm.validate_tool_configuration() raises ValueError, but nothing calls it for you, so run it in a test or at startup. swarm.invoke(..., force_final_tool=True) also checks the final-output rules before it sends a request. Without force_final_tool, the SDK does not reject a bad configuration. The errors:

  • Two agents both marked use_as_final_output=True.
  • Multiple members own final tools without a designated final-output member.
  • A swarm-level final tool conflicts with a designated final-output member.
  • force_final_tool=True with no swarm-level final tool, no designated member and no member final tool.

When no single member is designated, the runtime builds the answer from the member results instead of returning one member's output.

#Structured output from a member

Register a member's report model with final_tool=True. Inside a swarm, the runtime forces a final tool only on the designated use_as_final_output member, and only when you call the swarm with force_final_tool=True. An Agent(force_final_tool=True) default applies when that agent runs on its own. The SDK does not send it for a swarm member.

Python
report_agent = Agent(
    name='report_agent',
    api_key='your-api-key',
    use_as_final_output=True,
)

@report_agent.toolify(final_tool=True)
class MemberReport(BaseModel):
    headline: str
    bullets: list[str]

#Running a swarm

A swarm has the same four entry points as an agent: invoke, stream, ainvoke and astream. They accept a string, a message or a sequence of messages.

Python
# Synchronous, returns an InvokeResponse
response = swarm.invoke(HumanMessage(content='Write about AI trends in 2026'))

print(response.response)   # final assistant text
print(response.result)     # structured / final-tool output, when present

To request a structured final result, pass force_final_tool=True. The swarm resolves it against the use_as_final_output member or the swarm-scope final tool:

Python
response = swarm.invoke(
    HumanMessage(content='Write about AI trends'),
    force_final_tool=True,
)
report = response.result

Continue a conversation across calls with a thread_id:

Python
response = swarm.invoke(
    HumanMessage(content='Now make it shorter'),
    thread_id='article-session-123',
)

#Keyword-only config

Swarm config kwargs are keyword-only. As with Agent.invoke(), every option after messages on Swarm.invoke() / stream() / ainvoke() / astream() must be passed by keyword. Call them as swarm.invoke(messages, force_final_tool=True), never positionally.

The keyword options are force_final_tool, thread_id, verbose, reasoning, routing_preference, memory_config, allow_private_in_system_tools, model, system_tools_config, max_cycles, stream_response, options and origin. A per-call allow_private_in_system_tools wins over the swarm's value, and max_cycles caps the run's model-loop cycles (see Capping model-loop cycles). A Swarm(...) constructor sets defaults for model, system_tools_config and max_cycles, and per-call values override them. Memory settings are per call: Swarm has no memory_config constructor argument.

#Streaming and async

stream() yields StreamEvent values as the swarm executes, including any status-message events emitted by the runtime.

Python
for event in swarm.stream(
    HumanMessage(content='Run the multi-agent workflow'),
):
    handle(event)

The async variants mirror the synchronous ones:

Python
response = await swarm.ainvoke(HumanMessage(content='Plan the launch'))

async for event in swarm.astream(HumanMessage(content='Run the workflow')):
    handle(event)

For filtered, higher-level event reporting, wrap any invocation with events() (inherited from the shared scope):

Python
response = swarm.events(
    include=['enrichment', 'agent', 'model'],
    on_event=lambda payload: post_to_ui(payload),
).invoke(HumanMessage(content='Run the multi-agent workflow'))

events() no longer prints the terminal trace by default (maivn 3.1.0); pass auto_verbose=True or verbose=True when you want it. In the plain terminal tier, verbose=True prefixes each member's streamed lines with the member's name, such as [researcher] ..., and prints the final answer once.

When task_list is in the swarm's SystemToolsConfig(allowed_tools=[...]), the swarm publishes its plan as task_list_updated events: one item per member it runs, titled with the member's name, whose status moves from pending to in_progress to completed. An on_event callback reads the items from event['payload']['items'], while a normalized AppEvent carries them on event.task_list.items. See Task-list events.

#How coordination works

The SDK sends the runtime your roster: each member's prompt and tools, plus its declared dependencies. A planning step then chooses the member actions, and the runtime runs each as its own child session. Members with no dependencies start first and run in parallel when they are independent. A member that depends on another waits for that member's output. The designated final-output member runs with the upstream results available and produces the answer the caller receives. You declare the data flow; the runtime sequences the work.

An agent named in depends_on_agent must be a member of the same swarm, and you invoke the swarm. The member that runs for a dependency receives the user's request along with any injected inputs. For one member waiting on another, prefer swarm.member.depends_on_agent(...) (see Member registration with dependencies). The tool-level @depends_on_agent below also works, and it runs the specialist once.

Python
from maivn import Agent, Swarm, depends_on_agent

# data_collector and trend_analyzer are independent -> they can run in parallel
# synthesizer depends on both -> it waits for them, then produces the final output
data_collector = Agent(name='data_collector', api_key='your-api-key')
trend_analyzer = Agent(name='trend_analyzer', api_key='your-api-key')
synthesizer = Agent(name='synthesizer', api_key='your-api-key', use_as_final_output=True)

@synthesizer.toolify()
@depends_on_agent(data_collector, arg_name='data')
@depends_on_agent(trend_analyzer, arg_name='trends')
def synthesize(data: dict, trends: dict) -> dict:
    return {'synthesis': 'Combined insights'}

swarm = Swarm(
    name='analysis_team',
    agents=[data_collector, trend_analyzer, synthesizer],
)

#Shared tools and config across members

Tools registered on the swarm belong to the parent invocation. Member agents carry their own registered tools. They do not automatically inherit parent tools. Register a utility on each member that needs to call it, or use a swarm-level tool as an explicit member dependency.

Python
agent1 = Agent(name='agent1', api_key='your-api-key')
agent2 = Agent(name='agent2', api_key='your-api-key')
swarm = Swarm(name='team', agents=[agent1, agent2])

@swarm.toolify(description='Utility for the parent invocation')
def shared_utility(data: dict) -> dict:
    return {'processed': True}

agent1.add_tool(shared_utility)
agent2.add_tool(shared_utility)

A Swarm accepts system_tools_config (allowlists and approval controls for system tools) as a default for every run, and you can override it per call. Pass memory_config on each call, since the constructor does not take one.

To cap a run's model-loop cycles, set max_cycles on the swarm or on a call. It does not create a supervised repair loop. Model required passes with explicit member or tool dependencies, and have the application start another invocation when a postcondition fails.

use_as_final_output=True identifies the member that produces the user-facing answer. A member can still appear more than once in runtime events, so key UI cards by assignment or invocation ID rather than agent name.

#Complete example

This example uses fixture research. Replace it with your own data source. Each member has a final tool so the dependency receives the expected dictionary.

Python
from pydantic import BaseModel
from maivn import Agent, Swarm, depends_on_agent
from maivn.messages import HumanMessage

researcher = Agent(
    name='researcher',
    description='Researches topics and finds relevant information',
    system_prompt='You are a research specialist. Gather information and cite sources.',
    api_key='your-api-key',
)

@researcher.toolify(description='Return sample research for the topic', final_tool=True)
def research_topic(topic: str) -> dict:
    return {
        'topic': topic,
        'findings': [f'Finding about {topic}'],
        'sources': ['source1.com', 'source2.com'],
    }

writer = Agent(
    name='writer',
    description='Writes articles based on research',
    system_prompt='You are a content writer. Turn research into an article.',
    api_key='your-api-key',
    use_as_final_output=True,
)

@writer.toolify(description='Write an article from research', final_tool=True)
@depends_on_agent(researcher, arg_name='research')
def write_article(research: dict, style: str = 'professional') -> dict:
    return {
        'title': f"Article about {research['topic']}",
        'body': f"Based on research: {research['findings']}",
        'style': style,
    }

content_team = Swarm(
    name='content_team',
    description='A team that researches and writes articles',
    agents=[researcher, writer],
)

response = content_team.invoke(
    HumanMessage(content='Write an article about quantum computing'),
    force_final_tool=True,
)

print(response.result)

#Best practices

  • Give each member one clear job. Overlapping responsibilities make coordination ambiguous, and narrow roles keep prompts and tools focused.
  • Keep dependency chains short. Prefer a shallow graph (A -> B -> C) over a long pipeline. Independent members run in parallel and finish sooner.
  • Designate the final output explicitly whenever more than one scope could produce it.
  • Use terminal tracing while developing. swarm.invoke(..., verbose=True) shows what each member is doing and makes coordination easy to follow.

#See also

  • Tools — how to define and register the tools your members use.
  • Dependencies — the shared dependency vocabulary (depends_on_agent, depends_on_tool, depends_on_await_for, depends_on_reevaluate, depends_on_interrupt) used by both tools and swarm.member.
  • Structured Output — typed results via final_tool + force_final_tool and the structured_output() fast path.

#Next steps

  • Swarm API — the full Swarm class reference.
  • Agent API — the Agent class your members are built from.