Interrupt on a specific tool when using Langgraph ReAct Agent #929
Replies: 3 comments 5 replies
-
For that, I'd make my own graph, have two tool nodes - one where I'd have an interrupt and the other I wouldn't. We demonstrate this in the customer support tutorial |
Beta Was this translation helpful? Give feedback.
-
Use case: I wanted to interrupt the LangGraph ReAct agent when a specific tool is about to be called (summary_file), so a human can review and optionally edit the parameters before the call executes. I used the built-in post_model_hook like this: def use_post_hook(state, config: RunnableConfig):
last_msg = state["messages"][-1]
if isinstance(last_msg, ToolMessage):
with open("post_messages.md", "w", encoding="utf-8") as f:
f.write(f"\n{last_msg.model_dump_json()}\n")
if not isinstance(last_msg, AIMessage):
return # do nothing
print("Post hooked: check action.")
new_msg = process_ai_message(last_msg)
if not new_msg: # has no new message, do nothing!
return
tool_calls = new_msg.additional_kwargs.get("tool_calls", None)
print("tool_calls", tool_calls)
if tool_calls and tool_calls[0]["function"]["name"] in ["summary_file"]:
print("---human_feedback---")
feedback = interrupt({
"name": tool_calls[0]["function"]["name"],
"type": "ask",
"content": f"{tool_calls[0]["function"]["arguments"]}"
})
print("feedback", feedback)
# handle feedback
...
return {
"messages": [RemoveMessage(id=last_msg.id), new_msg],
} And then passed it to the agent: geoda_agent = create_react_agent(
...,
post_model_hook=use_post_hook,
...,
) |
Beta Was this translation helpful? Give feedback.
-
If you want to introduce Human-in-the-Loop approval before calling certain tools (e.g. risky actions like place_order), you can use a post_model_hook with interrupt.
##How You can call it
##How you can resume it
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have been looking into Human In the Loop for Langgraph React Agent here: https://langchain-ai.github.io/langgraph/how-tos/create-react-agent-hitl/
In the tutorial, the graph is interrupted at every tool call.
However, I don't want it to stop at every single tool call but instead stop at a specific tool such as
AskHuman
like in this tutorial: https://langchain-ai.github.io/langgraph/how-tos/human_in_the_loop/wait-user-input/I cannot use the chain with tools as for my usecase it is required to have an agent. So, how can I have my langgraph agent only stop on a specific tool?
Thank you in advance.
Beta Was this translation helpful? Give feedback.
All reactions