@@ -96,13 +96,59 @@ def create_swarm(
96
96
97
97
Args:
98
98
agents: List of agents to add to the swarm
99
+ An agent can be a LangGraph [CompiledStateGraph](https://langchain-ai.github.io/langgraph/reference/graphs/#langgraph.graph.state.CompiledStateGraph),
100
+ a functional API [workflow](https://langchain-ai.github.io/langgraph/reference/func/#langgraph.func.entrypoint),
101
+ or any other [Pregel](https://langchain-ai.github.io/langgraph/reference/pregel/#langgraph.pregel.Pregel) object.
99
102
default_active_agent: Name of the agent to route to by default (if no agents are currently active).
100
103
state_schema: State schema to use for the multi-agent graph.
101
104
config_schema: An optional schema for configuration.
102
105
Use this to expose configurable parameters via `swarm.config_specs`.
103
106
104
107
Returns:
105
108
A multi-agent swarm StateGraph.
109
+
110
+ Example:
111
+
112
+ ```python
113
+ from langgraph.checkpoint.memory import InMemorySaver
114
+ from langgraph.prebuilt import create_react_agent
115
+ from langgraph_swarm import create_handoff_tool, create_swarm
116
+
117
+ def add(a: int, b: int) -> int:
118
+ '''Add two numbers'''
119
+ return a + b
120
+
121
+ alice = create_react_agent(
122
+ "openai:gpt-4o",
123
+ [add, create_handoff_tool(agent_name="Bob")],
124
+ prompt="You are Alice, an addition expert.",
125
+ name="Alice",
126
+ )
127
+
128
+ bob = create_react_agent(
129
+ "openai:gpt-4o",
130
+ [create_handoff_tool(agent_name="Alice", description="Transfer to Alice, she can help with math")],
131
+ prompt="You are Bob, you speak like a pirate.",
132
+ name="Bob",
133
+ )
134
+
135
+ checkpointer = InMemorySaver()
136
+ workflow = create_swarm(
137
+ [alice, bob],
138
+ default_active_agent="Alice"
139
+ )
140
+ app = workflow.compile(checkpointer=checkpointer)
141
+
142
+ config = {"configurable": {"thread_id": "1"}}
143
+ turn_1 = app.invoke(
144
+ {"messages": [{"role": "user", "content": "i'd like to speak to Bob"}]},
145
+ config,
146
+ )
147
+ turn_2 = app.invoke(
148
+ {"messages": [{"role": "user", "content": "what's 5 + 7?"}]},
149
+ config,
150
+ )
151
+ ```
106
152
"""
107
153
active_agent_annotation = state_schema .__annotations__ .get ("active_agent" )
108
154
if active_agent_annotation is None :
0 commit comments