Description
I’m building the supervisor multi-agent gragh by myself. And I’m trying to use command message in the tool to handoff the task to another node(SOP_executor)
I expect the process will directly go to SOP_executor and WOULD NOT execute the supervisor again
But SOP_executor and supervisor is running in parallel.
Why?
Example Code
from typing import Annotated, List
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage, ToolMessage, AIMessageChunk, BaseMessage
from langchain.tools import tool, ToolRuntime
from langgraph.graph import MessagesState, StateGraph, START, END
from langgraph.prebuilt import ToolNode
from langchain_openai import ChatOpenAI
from langgraph.types import Command
class AgentState(MessagesState):
pass
@tool
def handoff_job_to_SOP_executor(handoffback_msg: Annotated[str, "call SOP executor to execute the job"],
runtime: ToolRuntime)-> Command:
"""
call SOP executor to execute the job
"""
tool_message = ToolMessage(
content=f"success to handoff job to SOP executor",
tool_call_id=runtime.tool_call_id
)
return Command(
goto="SOP_executor",
update={**runtime.state, "messages": runtime.state["messages"] + [tool_message]},
# graph=Command.PARENT,
)
def get_qwen_model():
return ChatOpenAI(
model="qwen3-max",
temperature=0.5,
max_tokens=5000,
api_key = mykey,
base_url='https://dashscope.aliyuncs.com/compatible-mode/v1',
streaming=False,
)
tools = [handoff_job_to_SOP_executor]
def main_super(state: AgentState):
llm = get_qwen_model()
print("\n\n[[arrive supervisor]]\n\n")
llm_with_tools = llm.bind_tools(tools, parallel_tool_calls=True)
llm_msg = [SystemMessage(content="hand off any job to SOP executor using handoff_job_to_SOP_executor tool")] + state["messages"]
response = llm_with_tools.invoke(llm_msg)
state["messages"].append(response)
print("\n\ndo something\n\n")
print("\n\n[[bye supervisor]]\n\n")
return state
def SOP_executor(state: AgentState):
print("\n\n[[arrive SOP_executor]]\n\n")
print("\n\ndo something\n\n")
print("\n\n[[bye SOP_executor]]\n\n")
return state
def should_continue(state: AgentState):
messages = state["messages"]
last_message = messages[-1]
if hasattr(last_message, 'tool_calls') and last_message.tool_calls:
return "tools"
return END
workflow = StateGraph(AgentState)
workflow.add_node("supervisor", main_super)
workflow.add_node("tools", ToolNode(tools))
workflow.add_node("SOP_executor", SOP_executor, destinations=["supervisor"])
workflow.add_edge(START, "supervisor")
workflow.add_edge("SOP_executor", END)
workflow.add_conditional_edges(
"supervisor",
should_continue,
{
"tools": "tools",
END: END,
}
)
workflow.add_edge("tools", "supervisor")
supervisor = workflow.compile()
for _, chunk in supervisor.stream(
{
"messages": [
{
"role": "user",
"content": "find US and New York state GDP in 2024. what % of US GDP was New York state?",
}
]
},
subgraphs=True
):
for k, v in chunk.items():
print()
print(type(v['messages'][-1]))
if isinstance(v['messages'][-1], dict):
print(v['messages'][-1])
else:
v['messages'][-1].pretty_print()
print()
System output
[[arrive supervisor]]
do something
[[bye supervisor]]
<class 'langchain_core.messages.ai.AIMessage'>
================================== Ai Message ==================================
Tool Calls:
handoff_job_to_SOP_executor (call_86f09b6a65ab4ec69f55ea87)
Call ID: call_86f09b6a65ab4ec69f55ea87
Args:
handoffback_msg: find US and New York state GDP in 2024. what % of US GDP was New York state?
<class 'langchain_core.messages.tool.ToolMessage'>
================================= Tool Message =================================
Name: handoff_job_to_SOP_executor
success to handoff job to SOP executor
[[arrive supervisor]]
[[arrive SOP_executor]]
do something
[[bye SOP_executor]]
<class 'langchain_core.messages.tool.ToolMessage'>
================================= Tool Message =================================
Name: handoff_job_to_SOP_executor
success to handoff job to SOP executor
do something
[[bye supervisor]]
<class 'langchain_core.messages.ai.AIMessage'>
================================== Ai Message ==================================
The job has been successfully handed off to the SOP executor for processing. Please wait for the results regarding the US and New York state GDP in 2024, as well as the percentage of US GDP attributed to New York state.