@@ -68,6 +68,59 @@ def add_active_agent_router(
68
68
69
69
Returns:
70
70
StateGraph with the router added.
71
+
72
+ Example:
73
+
74
+ ```python
75
+ from langgraph.checkpoint.memory import InMemorySaver
76
+ from langgraph.prebuilt import create_react_agent
77
+ from langgraph.graph import StateGraph
78
+ from langgraph_swarm import SwarmState, create_handoff_tool, add_active_agent_router
79
+
80
+ def add(a: int, b: int) -> int:
81
+ '''Add two numbers'''
82
+ return a + b
83
+
84
+ alice = create_react_agent(
85
+ "openai:gpt-4o",
86
+ [add, create_handoff_tool(agent_name="Bob")],
87
+ prompt="You are Alice, an addition expert.",
88
+ name="Alice",
89
+ )
90
+
91
+ bob = create_react_agent(
92
+ "openai:gpt-4o",
93
+ [create_handoff_tool(agent_name="Alice", description="Transfer to Alice, she can help with math")],
94
+ prompt="You are Bob, you speak like a pirate.",
95
+ name="Bob",
96
+ )
97
+
98
+ checkpointer = InMemorySaver()
99
+ workflow = (
100
+ StateGraph(SwarmState)
101
+ .add_node(alice, destinations=("Bob",))
102
+ .add_node(bob, destinations=("Alice",))
103
+ )
104
+ # this is the router that enables us to keep track of the last active agent
105
+ workflow = add_active_agent_router(
106
+ builder=workflow,
107
+ route_to=["Alice", "Bob"],
108
+ default_active_agent="Alice",
109
+ )
110
+
111
+ # compile the workflow
112
+ app = workflow.compile(checkpointer=checkpointer)
113
+
114
+ config = {"configurable": {"thread_id": "1"}}
115
+ turn_1 = app.invoke(
116
+ {"messages": [{"role": "user", "content": "i'd like to speak to Bob"}]},
117
+ config,
118
+ )
119
+ turn_2 = app.invoke(
120
+ {"messages": [{"role": "user", "content": "what's 5 + 7?"}]},
121
+ config,
122
+ )
123
+ ```
71
124
"""
72
125
channels = builder .schemas [builder .schema ]
73
126
if "active_agent" not in channels :
0 commit comments