Human-in-the-loop (HIL) interactions are crucial for agentic systems. A common pattern is to add some human in the loop step after certain tool calls. These tool calls often lead to either a function call or saving of some information. Examples include:
A tool call to execute SQL, which will then be run by the tool
A tool call to generate a summary, which will then be saved to the State of the graph
Note that using tool calls is common whether actually calling tools or not.
There are typically a few different interactions you may want to do here:
Approve the tool call and continue
Modify the tool call manually and then continue
Give natural language feedback, and then pass that back to the agent
We can implement these in LangGraph using the interrupt() function. interrupt allows us to stop graph execution to collect input from a user and continue execution with collected input:
defhuman_review_node(state)->Command[Literal["call_llm","run_tool"]]:# this is the value we'll be providing via Command(resume=<human_review>)human_review=interrupt({"question":"Is this correct?",# Surface tool calls for review"tool_call":tool_call})review_action,review_data=human_review# Approve the tool call and continueifreview_action=="continue":returnCommand(goto="run_tool")# Modify the tool call manually and then continueelifreview_action=="update":...updated_msg=get_updated_msg(review_data)returnCommand(goto="run_tool",update={"messages":[updated_message]})# Give natural language feedback, and then pass that back to the agentelifreview_action=="feedback":...feedback_msg=get_feedback_msg(review_data)returnCommand(goto="call_llm",update={"messages":[feedback_msg]})
We are not going to show the full code for the graph we are hosting, but you can see it here. Once this graph is hosted, we are ready to invoke it and wait for user input.
First, we need to setup our client so that we can communicate with our hosted graph:
fromlanggraph_sdkimportget_clientclient=get_client(url=<DEPLOYMENT_URL>)# Using the graph deployed with the name "agent"assistant_id="agent"thread=awaitclient.threads.create()
import{Client}from"@langchain/langgraph-sdk";constclient=newClient({apiUrl:<DEPLOYMENT_URL>});// Using the graph deployed with the name "agent"constassistantId="agent";constthread=awaitclient.threads.create();
First, let's run the agent with an input that requires tool calls with approval:
input={"messages":[{"role":"user","content":"what's the weather in sf?"}]}asyncforchunkinclient.runs.stream(thread["thread_id"],assistant_id,input=input,stream_mode="updates",):ifchunk.dataandchunk.event!="metadata":print(chunk.data)
constinput={"messages":[{"role":"user","content":"what's the weather in sf?"}]};conststreamResponse=client.runs.stream(thread["thread_id"],assistantId,{input:input,streamMode:"updates"});forawait(constchunkofstreamResponse){if(chunk.data&&chunk.event!=="metadata"){console.log(chunk.data);}}
curl--requestPOST\--url<DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/stream\--header'Content-Type: application/json'\--data"{ \"assistant_id\": \"agent\", \"input\": {\"messages\": [{\"role\": \"human\", \"content\": \"what's the weather in sf?\"}]}, \"stream_mode\": [ \"updates\" ] }"
To approve the tool call, we need to let human_review_node know what value to use for the human_review variable we defined inside the node. We can provide this value by invoking the graph with a Command(resume=<human_review>) input. Since we're approving the tool call, we'll provide resume value of {"action": "continue"} to navigate to run_tool node:
Let's now say we want to edit the tool call. E.g. change some of the parameters (or even the tool called!) but then execute that tool.
input={"messages":[{"role":"user","content":"what's the weather in sf?"}]}asyncforchunkinclient.runs.stream(thread["thread_id"],assistant_id,input=input,stream_mode="updates",):ifchunk.dataandchunk.event!="metadata":print(chunk.data)
constinput={"messages":[{"role":"user","content":"what's the weather in sf?"}]};conststreamResponse=client.runs.stream(thread["thread_id"],assistantId,{input:input,streamMode:"updates",});forawait(constchunkofstreamResponse){if(chunk.data&&chunk.event!=="metadata"){console.log(chunk.data);}}
curl--requestPOST\--url<DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/stream\--header'Content-Type: application/json'\--data"{ \"assistant_id\": \"agent\", \"input\": {\"messages\": [{\"role\": \"human\", \"content\": \"what's the weather in sf?\"}]}, \"stream_mode\": [ \"updates\" ] }"
To do this, we will use Command with a different resume value of {"action": "update", "data": <tool call args>}. This will do the following:
combine existing tool call with user-provided tool call arguments and update the existing AI message with the new tool call
navigate to run_tool node with the updated AI message and continue execution
Sometimes, you may not want to execute a tool call, but you also may not want to ask the user to manually modify the tool call. In that case it may be better to get natural language feedback from the user. You can then insert this feedback as a mock RESULT of the tool call.
There are multiple ways to do this:
You could add a new message to the state (representing the "result" of a tool call)
You could add TWO new messages to the state - one representing an "error" from the tool call, other HumanMessage representing the feedback
Both are similar in that they involve adding messages to the state. The main difference lies in the logic AFTER the human_review_node and how it handles different types of messages.
For this example we will just add a single tool call representing the feedback (see human_review_node implementation). Let's see this in action!
input={"messages":[{"role":"user","content":"what's the weather in sf?"}]}asyncforchunkinclient.runs.stream(thread["thread_id"],assistant_id,input=input,stream_mode="updates",):ifchunk.dataandchunk.event!="metadata":print(chunk.data)
constinput={"messages":[{"role":"user","content":"what's the weather in sf?"}]};conststreamResponse=client.runs.stream(thread["thread_id"],assistantId,{input:input,streamMode:"updates"});forawait(constchunkofstreamResponse){if(chunk.data&&chunk.event!=="metadata"){console.log(chunk.data);}}
curl--requestPOST\--url<DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/stream\--header'Content-Type: application/json'\--data"{ \"assistant_id\": \"agent\", \"input\": {\"messages\": [{\"role\": \"human\", \"content\": \"what's the weather in sf?\"}]}, \"stream_mode\": [ \"updates\" ] }"
To do this, we will use Command with a different resume value of {"action": "feedback", "data": <feedback string>}. This will do the following:
create a new tool message that combines existing tool call from LLM with the with user-provided feedback as content
navigate to call_llm node with the updated tool message and continue execution
fromlanggraph_sdk.schemaimportCommandasyncforchunkinclient.runs.stream(thread["thread_id"],assistant_id,command=Command(resume={"action":"feedback","data":"User requested changes: use <city, country> format for location"}),stream_mode="updates",):ifchunk.dataandchunk.event!="metadata":print(chunk.data)
conststreamResponse=client.runs.stream(thread["thread_id"],assistantId,{command:{resume:{"action":"feedback","data":"User requested changes: use <city, country> format for location"}},streamMode:"updates"});forawait(constchunkofstreamResponse){if(chunk.data&&chunk.event!=="metadata"){console.log(chunk.data);}}
curl--requestPOST\--url<DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/stream\--header'Content-Type: application/json'\--data"{ \"assistant_id\": \"agent\", \"command\": { \"resume\": { \"action\": \"feedback\", \"data\": \"User requested changes: use <city, country> format for location\" } }, \"stream_mode\": [ \"updates\" ] }"
We can see that we now get to another interrupt - because it went back to the model and got an entirely new prediction of what to call. Let's now approve this one and continue.