langgraph-swarm¶
See the project description for more details.
swarm
¶
| FUNCTION | DESCRIPTION |
|---|---|
create_swarm |
Create a multi-agent swarm. |
add_active_agent_router |
Add a router to the currently active agent to the |
SwarmState
¶
Bases: MessagesState
State schema for the multi-agent swarm.
create_swarm
¶
create_swarm(
agents: list[Pregel],
*,
default_active_agent: str,
state_schema: StateSchemaType = SwarmState,
context_schema: type[Any] | None = None,
**deprecated_kwargs: Unpack[DeprecatedKwargs],
) -> StateGraph
Create a multi-agent swarm.
| PARAMETER | DESCRIPTION |
|---|---|
agents
|
List of agents to add to the swarm An agent can be a LangGraph |
default_active_agent
|
Name of the agent to route to by default (if no agents are currently active).
TYPE:
|
state_schema
|
State schema to use for the multi-agent graph.
TYPE:
|
context_schema
|
Specifies the schema for the context object that will be passed to the workflow. |
| RETURNS | DESCRIPTION |
|---|---|
StateGraph
|
A multi-agent swarm |
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,
)
add_active_agent_router
¶
add_active_agent_router(
builder: StateGraph, *, route_to: list[str], default_active_agent: str
) -> StateGraph
Add a router to the currently active agent to the StateGraph.
| PARAMETER | DESCRIPTION |
|---|---|
builder
|
The graph builder (
TYPE:
|
route_to
|
A list of agent (node) names to route to. |
default_active_agent
|
Name of the agent to route to by default (if no agents are currently active).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
StateGraph
|
|
Example
from langchain.checkpoint.memory import InMemorySaver
from langchain.agents import create_agent
from langgraph.graph import StateGraph
from langgraph_swarm import SwarmState, create_handoff_tool, add_active_agent_router
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 = (
StateGraph(SwarmState)
.add_node(alice, destinations=("Bob",))
.add_node(bob, destinations=("Alice",))
)
# this is the router that enables us to keep track of the last active agent
workflow = add_active_agent_router(
builder=workflow,
route_to=["Alice", "Bob"],
default_active_agent="Alice",
)
# compile the workflow
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,
)
handoff
¶
| FUNCTION | DESCRIPTION |
|---|---|
create_handoff_tool |
Create a tool that can handoff control to the requested agent. |
create_handoff_tool
¶
create_handoff_tool(
*, agent_name: str, name: str | None = None, description: str | None = None
) -> BaseTool
Create a tool that can handoff control to the requested agent.
| PARAMETER | DESCRIPTION |
|---|---|
agent_name
|
The name of the agent to handoff control to, i.e. the name of the agent node in the multi-agent graph. Agent names should be simple, clear and unique, preferably in
TYPE:
|
name
|
Optional name of the tool to use for the handoff. If not provided, the tool name will be
TYPE:
|
description
|
Optional description for the handoff tool. If not provided, the tool description will be
TYPE:
|