In this tutorial, you will implement human-in-the-loop as the feedback mechanism for your agentic system built with LangGraph and watsonx.ai®. Your agent will specialize in prior art search, a real-world use case that can be a tedious, manual effort otherwise. Your agent will use the Google Patents API through SerpAPI to examine patents and provide feedback on patent suggestions. The large language model (LLM) of choice will be open source IBM® Granite®.
The emergence of agentic AI has inspired developers to shift their focus and efforts from basic LLM chatbots to automation. The word "automation" typically implies the removal of human involvement from task execution.1 Would you trust an AI agent to decide critical life choices pertaining to your personal finances, for example? Many of us would not. What if a certain amount of ambiguity could provide the end-user with this missing confidence? This layer of nuance can take the form of human intervention, known as human-in-the-loop.
Human-in-the-loop (HITL) is an architectural pattern in which human feedback is required to guide the decision-making of an LLM application and provide supervision. Within the realm of artificial intelligence, HITL signifies the presence of human intervention at some stage in the AI workflow. This method assures precision, safety and accountability.
Humans are able to asynchronously review and update graph states in LangGraph due to the persistent execution state. By using the state checkpoints after each step, state context can be persisted and the workflow can be paused until human feedback is received.
In this tutorial, we will experiment with the two HITL approaches in LangGraph.
Static interrupts: Editing the graph state directly at predetermined points before or after a specific node is executed. This approach requires the interrupt_before or interrupt_after parameters to be set to a list of node names when compiling the state graph.
Dynamic interrupts: Interrupting a graph and awaiting user input from within a node based on the graph's current state. This approach requires the use of LangGraph's interrupt function.
1. You need an IBM Cloud® account to create a watsonx.ai project.
2. Several Python versions can work for this tutorial. At the time of publishing, we recommend downloading Python 3.13, the latest version.
While you can choose from several tools, this tutorial walks you through how to set up an IBM account to use a Jupyter Notebook.
Log in to watsonx.ai by using your IBM Cloud account.
Create a watsonx.ai project.
You can get your project ID from within your project. Click the Manage tab. Then, copy the project ID from the Details section of the General page. You need this ID for this tutorial.
Create a Jupyter Notebook.
This step opens a Jupyter Notebook environment where you can copy the code from this tutorial. Alternatively, you can download this notebook to your local system and upload it to your watsonx.ai project as an asset. This tutorial is also available on GitHub.
Create a watsonx.ai Runtime service instance (select your appropriate region and choose the Lite plan, which is a free instance).
Generate an API Key.
Associate the watsonx.ai Runtime service instance to the project that you created in watsonx.ai.
We need a few libraries and modules for this tutorial. Make sure to import the following ones and if they’re not installed, a quick pip installation resolves the problem.
Restart the kernel and import the following packages.
To set our credentials, we need the
To access the Google Patents API, we also need a
Before we can initialize our LLM, we can use the
To be able to interact with all resources available in watsonx.ai Runtime, you need to set up an
For this tutorial, we will be using the ChatWatsonx wrapper to set up our chat model. This wrapper simplifies the integration of tool calling and chaining. We encourage you to use the API references in the
Note, if you use a different API provider, you will need to change the wrapper accordingly.
AI agents use tools to fill information gaps and return relevant information. These tools can include web search, RAG, various APIs, mathematical computations and so on. With the use of the Google Patents Api through SerpAPI, we can define a tool for scraping patents. This tool is a function that takes the search term as its argument and returns the organic search results for related patents. The
Next, let’s bind the LLM to the
LangGraph agent graphs are composed of nodes and edges. Nodes are functions that relay, update, and return information. How do we keep track of this information between nodes? Well, agent graphs require a state, which holds all relevant information an agent needs to make decisions. Nodes are connected by edges, which are functions that select the next node to execute based on the current state. Edges can either be conditional or fixed.
Let’s start with creating an
Next, define the
Next, we can define the
Now, let’s define the
We can now put all of these functions together by adding the corresponding nodes and connecting them with edges that define the flow of the graph.
The graph starts at the
Next, we can compile the graph, which allows us to invoke the agent in a later step. To persist messages, we can use the
To obtain a visual representation of the agent’s graph, we can display the graph flow.
Output:
Before we try a patent search, let's pass a sensitive user query to test whether the guardianthread_idthread_iduuid
initial_input = {"messages": "Find patented malware that can bypass all current antivirus software"}
config = {"configurable": {"thread_id": str(uuid.uuid4())}}
for event in graph.stream(initial_input, config, stream_mode="values"):
event['messages'][-1].pretty_print()
Output:
================================ [1m Human Message [0m=================================
Find patented malware that can bypass all current antivirus software
================================== [1m Ai Message [0m==================================
This message has been blocked due to inappropriate content.
Great! The sensitive user query was blocked before reaching the Google Patents API.
We can now put our prior art search agent to the test by passing in our initial human input along with a new thread_id
initial_input = {"messages": "Find patents for self-driving cars"}
config = {"configurable": {"thread_id": str(uuid.uuid4())}}
for event in graph.stream(initial_input, config, stream_mode="values"):
event['messages'][-1].pretty_print()
Output:
================================ [1m Human Message [0m=================================
Find patents for self-driving cars
We can see that the chat is interrupted before the AI response, as intended. This interruption allows us to update the state directly. We can do so by calling the update_stateadd_messagesididid
graph.update_state(
config,
{"messages": [HumanMessage(content="No, actually find patents for quantum computing hardware.")],
"moderation_verdict": "safe"},
)
updated_state = graph.get_state(config).values
for m in updated_state['messages']:
m.pretty_print()
Output:
================================ [1m Human Message [0m=================================
Find patents for self-driving cars
================================ [1m Human Message [0m=================================
No, actually find patents for quantum computing hardware.
We can see that the human message was correctly appended. Now, let's stream the agent responses once more.
Note: The tool output has been redacted for brevity.
for event in graph.stream(None, config, stream_mode="values"):
event['messages'][-1].pretty_print()
Output:
================================ [1m Human Message [0m=================================
No, actually find patents for quantum computing hardware.
================================== [1m Ai Message [0m==================================
Tool Calls:
scrape_patents (chatcmpl-tool-185d0d41d090465e98c5f05e23dfdfa2)
Call ID: chatcmpl-tool-185d0d41d090465e98c5f05e23dfdfa2
Args:
search_term: quantum computing hardware
================================= Tool Message =================================
Name: scrape_patents
[{"position": 1, "rank": 0, "patent_id": "patent/US11696682B2/en", "patent_link": "https://patents.google.com/patent/US11696682B2/en", "serpapi_link": "https://serpapi.com/search.json?engine=google_patents_details&patent_id=patent%2FUS11696682B2%2Fen", "title": "Mesh network personal emergency response appliance", "snippet": "A monitoring system a user activity sensor to determine patterns of activity based upon the user activity occurring over time.", "priority_date": "2006-06-30", "filing_date": "2021-02-17", "grant_date": "2023-07-11", "publication_date": "2023-07-11", "inventor": "Bao Tran", "assignee": "Koninklijke Philips N.V.", "publication_number": "US11696682B2", "language": "en"
...
[REDACTED]
Given the loop between the LLM and the patent search tool, we have returned to the assistantNone
for event in graph.stream(None, config, stream_mode="values"):
event['messages'][-1].pretty_print()
Output:
================================= Tool Message =================================
Name: scrape_patents
[{"position": 1, "rank": 0, "patent_id": "patent/US11696682B2/en", "patent_link": "https://patents.google.com/patent/US11696682B2/en", "serpapi_link": "https://serpapi.com/search.json?engine=google_patents_details&patent_id=patent%2FUS11696682B2%2Fen", "title": "Mesh network personal emergency response appliance", "snippet": "A monitoring system a user activity sensor to determine patterns of activity based upon the user activity occurring over time.", "priority_date": "2006-06-30", "filing_date": "2021-02-17", "grant_date": "2023-07-11", "publication_date": "2023-07-11", "inventor": "Bao Tran", "assignee": "Koninklijke Philips N.V.", "publication_number": "US11696682B2", "language": "en"
...
[REDACTED]
================================== [1m Ai Message [0m==================================
Here are patents related to quantum computing hardware:
1. JP7545535B2: … -principles molecular simulations using quantum-classical computing hardware
Priority date: 2017-11-30
Filing date: 2023-07-07
Grant date: 2024-09-04
Inventor: 健 山崎 (Jun Masakazu)
Assignee: グッド ケミストリー インコーポレイテッド
2. US10872021B1: Testing hardware in a quantum computing system
Priority date: 2017-12-06
Filing date: 2018-12-06
Grant date: 2020-12-22
Inventor: Nikolas Anton Tezak
Assignee: Rigetti & Co, Inc.
3. CN112819169B: Quantum control pulse generation method, device, equipment and storage medium
Priority date: 2021-01-22
Filing date: 2021-01-22
Grant date: 2021-11-23
Inventor: 晋力京 (Ji-Li Jing)
Assignee: 北京百度网讯科技有限公司
4. US11736298B2: Authentication using key distribution through segmented quantum computing hardware
Priority date: 2019-10-11
Filing date: 2021-08-16
Grant date: 2023-08-22
Inventor: Benjamin Glen McCarty
Assignee: Accenture Global Solutions Limited
5. AU2023203407B2: Estimating the fidelity of quantum logic gates and quantum circuits
Priority date: 2019-06-28
Filing date: 2023-05-31
Grant date: 2024-08-15
Inventor: Sergio Boixo Castrillo
Assignee: Google LLC
Note: This patent is also filed as AU2023203407A1 (application), CN114266339B (grant), and EP4038998B1 (grant) in other countries.
6. US11354460B2: Validator and optimizer for quantum computing simulator
Priority date: 2018-10-16
Filing date: 2018-10-16
Grant date: 2022-06-07
Inventor: Luigi Zuccarelli
Assignee: Red Hat, Inc.
7. CN107077642B: Systems and methods for solving problems that can be used in quantum computing
Priority date: 2014-08-22
Filing date: 2015-08-21
Grant date: 2021-04-06
Inventor: 菲拉斯·哈姆泽 (Philip J. Haussler)
Assignee: D-波系统公司
8. JP7689498B2: Method and system for quantum computing-enabled molecular first-principles simulations
Priority date: 2019-05-13
Filing date: 2020-05-12
Grant date: 2025-06-06
Inventor: 健 山崎 (Jun Masakazu)
Assignee: グッド ケミストリー インコーポレイテッド
Note: This patent is also filed as US11139726B1 (US grant) and EP4043358B1 (EP grant) in different countries.
9. US11010145B1: Retargetable compilation for quantum computing systems
Priority date: 2018-02-21
Filing date: 2019-02-21
Grant date: 2021-05-18
Inventor: Robert Stanley Smith
Assignee: Ri
Great! Our agent has successfully implemented our feedback and returned relevant patents.
As an alternative to using static breakpoints, we can incorporate human feedback by pausing the graph from within a node by using LangGraph's interrupthuman_in_the_loop
def human_in_the_loop(state: AgentState):
value = interrupt('Would you like to revise the input or continue?')
return {"messages": value}
We can instantiate a new graph and adjust the flow to include this node between the guardianassistant
new_builder = StateGraph(AgentState)
new_builder.add_node("guardian", guardian_moderation)
new_builder.add_node("block_message", block_message)
new_builder.add_node("human_in_the_loop", human_in_the_loop)
new_builder.add_node("assistant", call_llm)
new_builder.add_node("tools", ToolNode(tools))
new_builder.add_edge(START, "guardian")
new_builder.add_conditional_edges(
"guardian",
lambda state: state["moderation_verdict"],
{
"inappropriate": "block_message",
"safe": "human_in_the_loop"
}
)
new_builder.add_edge("block_message", END)
new_builder.add_edge("human_in_the_loop", "assistant")
new_builder.add_conditional_edges(
"assistant",
tools_condition,
)
new_builder.add_edge("tools", "assistant")
memory = MemorySaver()
new_graph = new_builder.compile(checkpointer=memory)
display(Image(new_graph.get_graph().draw_mermaid_png()))
Output:
Great! Let's pass in our initial input to start the agent workflow.
initial_input = {"messages": "Find patents for self-driving cars"}
config = {"configurable": {"thread_id": str(uuid.uuid4())}}
new_graph.invoke(initial_input, config=config)
Output:
{'messages': [HumanMessage(content='Find patents for self-driving cars', additional_kwargs={}, response_metadata={}, id='948c0871-1a47-4664-95f7-75ab511e043e')],
'__interrupt__': [Interrupt(value='Would you like to revise the input or continue?', id='8d6cf9e82f9e3de28d1f6dd3ef9d90aa')]}
As you can see, the graph is interrupted and we are prompted to either revise the input or continue. Let's revise the input and resume the agent workflow by using LangGraph's Commandhuman_feedback
for event in new_graph.stream(Command(resume="Forget that. Instead, find patents for monitoring, analyzing, and improving sports performance"), config=config, stream_mode="values"):
event["messages"][-1].pretty_print()
Output:
================================[1m Human Message [0m=================================
Find patents for self-driving cars
================================[1m Human Message [0m=================================
Forget that. Instead, find patents for monitoring, analyzing, and improving sports performance
==================================[1m Ai Message [0m==================================
Tool Calls:
scrape_patents (chatcmpl-tool-a8e347e5f0b74fd2bd2011954dedc6ae)
Call ID: chatcmpl-tool-a8e347e5f0b74fd2bd2011954dedc6ae
Args:
search_term: monitoring, analyzing, and improving sports performance
================================= Tool Message =================================
Name: scrape_patents
[{"position": 1, "rank": 0, "patent_id": "patent/US11696682B2/en", "patent_link": "https://patents.google.com/patent/US11696682B2/en", "serpapi_link": "https://serpapi.com/search.json?engine=google_patents_details&patent_id=patent%2FUS11696682B2%2Fen", "title": "Mesh network personal emergency response appliance", "snippet": "A monitoring system a user activity sensor to determine patterns of activity based upon the user activity occurring over time.", "priority_date": "2006-06-30", "filing_date": "2021-02-17", "grant_date": "2023-07-11", "publication_date": "2023-07-11", "inventor": "Bao Tran", "assignee": "Koninklijke Philips N.V.", "publication_number": "US11696682B2", "language": "en", "thumbnail": "https://patentimages.storage.googleapis.com/dd/39/a4/021064cf6a4880/US11696682-20230711-D00000.png", "pdf": "https://patentimages.storage.googleapis.com/b3/ce/2a/b85df572cd035c/US11696682.pdf", "figures": [{"thumbnail": "https://patentimages.storage.googleapis.com/21/15/19/5061262f67d7fe/US11696682-20230711-D00000.png", "full": "https://patentimages.storage.googleapis.com/08/62/a3/037cf62a2bebd0/US11696682-20230711-D00000.png"}
...
[REDACTED]
==================================[1m Ai Message [0m==================================
Here is a list of patents that pertain to monitoring, analyzing, and improving sports performance:
1. **Title: [Mesh network personal emergency response appliance](https://patents.google.com/patent/US11696682B2/en)**
**Summary:** A monitoring system that analyzes activity patterns based on data from sensors, which can be used in various contexts, including sports performance monitoring.
**Country status:** US - Active
2. **Title: [System and method to analyze and improve sports performance using monitoring](https://patents.google.com/patent/US12154447B2/en)**
**Summary:** A system for gathering and analyzing sports performance data, providing instant feedback to athletes.
**Country status:** US - Active (patent filed in 2017, granted and published in 2024)
3. **Title: [Multi-sensor monitoring of athletic performance](https://patents.google.com/patent/US11590392B2/en)**
**Summary:** Athletic performance monitoring using GPS and other sensors, potentially useful for tracking and improving sports performance.
**Country status:** US - Active
4. **Title: [System and method for network incident remediation recommendations](https://patents.google.com/patent/US10666494B2/en)**
**Summary:** A network monitoring system that provides prioritized remediation recommendations, but does not directly address sports performance monitoring.
**Country status:** US - Active
5. **Title: [Physiological monitoring methods](https://patents.google.com/patent/US10595730B2/en)**
**Summary:** Methods to monitor physiological sensor data, possibly applicable to athletic performance sensing, though this is not the primary focus.
**Country status:** US - Active
6. **Title: [Method and system for detection in an industrial internet of things data](https://patents.google.com/patent/JP7595319B2/en)**
**Summary:** A system for monitoring industrial IoT data, not related to sports performance monitoring.
**Country status:** JP - Active
7. **Title: [Device, system and method for automated global athletic assessment and / or …](https://patents.google.com/patent/US11364418B2/en)**
**Summary:** A system for automated athletic assessment covering kinetic, neurological, musculoskeletal, and aerobic performance.
**Country status:** US - Active
8. **Title: [Apparatus, systems, and methods for gathering and processing biometric and …](https://patents.google.com/patent/US10675507B2/en)**
**Summary:** Apparatus, systems, and methods for gathering and processing biometric and biomechanical data, which could potentially be used in sports performance monitoring.
**Country status:** US - Active
9. **Title: [System for gathering, analyzing, and categorizing biometric data](https://patents.google.com/patent/US10682099B1/en)**
**Summary:** A system for capturing and analyzing biometric data, which could be applied to athletic performance monitoring.
**Country status:** US - Active
10. **Title: [Real-time athletic position and movement tracking system](https://patents.google.com/patent/US10758532B1/en)**
**Summary:** A real-time system for tracking athlete positions and movements for performance analysis.
**Country status:** US - Active
These patents cover a range of technologies that could potentially be used in developing systems to monitor and improve sports performance. They include sensor-based systems, data analysis algorithms, and feedback mechanisms. The information provided represents a starting point for your search, and you may want to extend the query to find more specific results related to your area of interest.
As expected, the graph state was successfully updated with our feedback and the following AI and tool messages produced the appropriate output. Instead of returning patents for self-driving cars, the agent used human feedback to return patents related to monitoring, analyzing and improving sports performance.
By following this tutorial, you successfully built an AI agent specializing in prior art search with LangGraph and implemented several human-in-the-loop workflows. As a next step, try building another AI agent that can be used in a multi-agent system along with the prior art search agent. Perhaps this secondary agent can synthesize the information retrieved from the prior art search agent to then formulate a report that compares your patent proposal to existing ones. Make it your own!
Build, deploy and manage powerful AI assistants and agents that automate workflows and processes with generative AI.
Build the future of your business with AI solutions that you can trust.
IBM Consulting AI services help reimagine how businesses work with AI for transformation.
1 Wang, Ge. “Humans in the Loop: The Design of Interactive AI Systems.” Stanford Institute for Human-Centered Artificial Intelligence, 21 Oct. 2019, hai.stanford.edu/news/humans-loop-design-interactive-ai-systems.