mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-10-20 15:01:44 +02:00
Alternative to https://github.com/langchain-ai/langgraph/pull/3124 Currently if a tool interrupts, the entire tool node executes again after resuming. So tools can get executed twice if parallel tool calls are generated. Here we allow ToolNode to accept tool calls, so we can use the `Send` API to distribute the tool calls to multiple instances of the tool node. ```python from langchain_anthropic import ChatAnthropic from langchain_core.tools import tool from langgraph.checkpoint.memory import MemorySaver from langgraph.prebuilt import create_react_agent from langgraph.types import Command, Send, interrupt @tool def human_assistance(query: str) -> str: """Request assistance from a human.""" human_response = interrupt({"query": query}) return human_response["data"] @tool def get_weather(location: str) -> str: """Use this tool to get the weather.""" return "It's sunny!" tools = [get_weather, human_assistance] llm = ChatAnthropic(model="claude-3-5-sonnet-20240620") agent = create_react_agent( llm, tools, checkpointer=MemorySaver(), tool_call_parallelism="parallel_tool_nodes", ) user_input = ( "Could you please (1) request assistance for building an AI agent " "from a human, and (2) search for the weather in Boston, MA? " "Generate two tool calls at once." ) config = {"configurable": {"thread_id": "1"}} for event in agent.stream( {"messages": [{"role": "user", "content": user_input}]}, config, stream_mode="values", ): event["messages"][-1].pretty_print() ``` ``` ... ``` ```python human_response = "You should check out LangGraph to build your agent." human_command = Command(resume={"data": human_response}) for event in agent.stream(human_command, config, stream_mode="values"): event["messages"][-1].pretty_print() ``` --------- Co-authored-by: Vadym Barda <vadym@langchain.dev>