Create a multi-agent swarm.
create_swarm(
agents: list[Pregel],
*,
default_active_agent: str,
state_schema: StateSchemaType = | Name | Type | Description |
|---|---|---|
agents* | list[Pregel] | List of agents to add to the swarm An agent can be a LangGraph |
default_active_agent* | str | Name of the agent to route to by default (if no agents are currently active). |
state_schema | StateSchemaType | Default: SwarmStateState schema to use for the multi-agent graph. |
context_schema | type[Any] | None | Default: None |
Example:
from langgraph.checkpoint.memory import InMemorySaver
from langchain.agents import create_agent
from langgraph_swarm import create_handoff_tool, create_swarm
def add(a: int, b: int) -> int:
'''Add two numbers'''
return a + b
alice = create_agent(
"openai:gpt-4o",
tools=[
add,
create_handoff_tool(
agent_name="Bob",
description="Transfer to Bob",
),
],
system_prompt="You are Alice, an addition expert.",
name="Alice",
)
bob = create_agent(
"openai:gpt-4o",
tools=[
create_handoff_tool(
agent_name="Alice",
description="Transfer to Alice, she can help with math",
),
],
system_prompt="You are Bob, you speak like a pirate.",
name="Bob",
)
checkpointer = InMemorySaver()
workflow = create_swarm(
[alice, bob],
default_active_agent="Alice"
)
app = workflow.compile(checkpointer=checkpointer)
config = {"configurable": {"thread_id": "1"}}
turn_1 = app.invoke(
{"messages": [{"role": "user", "content": "i'd like to speak to Bob"}]},
config,
)
turn_2 = app.invoke(
{"messages": [{"role": "user", "content": "what's 5 + 7?"}]},
config,
)Specifies the schema for the context object that will be passed to the workflow.