Day 9/30: Human-in-the-loop Agents A developer building a support bot with LangGraph and MCP found that the bot confidently provided incorrect information in uncertain situations, leading to customer complaints. The solution was implementing a human-in-the-loop mechanism that pauses execution and requests human approval when confidence is low. The developer provides a code example using conditional edges and a human approval tool to improve reliability. I recently spent hours debugging a support bot built with LangGraph and MCP, only to realize that the issue wasn't with the code itself, but with the way it was handling uncertain situations. The bot was designed to automatically respond to customer inquiries, but in some cases, it would confidently provide incorrect information. The problem was that the bot didn't know when to ask for help. It would charge ahead, making decisions without considering the potential risks or consequences. This led to a slew of angry customers and a significant amount of time spent on damage control. The root of the issue was that the bot lacked a mechanism to pause its execution and ask for human permission before taking a potentially risky action. This is where the concept of human-in-the-loop agents comes in. By design, these agents are capable of recognizing when they're uncertain or lack sufficient information to make a decision, and can pause their execution to ask for human guidance. In the context of the support bot, this would mean that before providing a response to a customer inquiry, the bot would check if it's confident in its answer. If not, it would pause and ask a human operator to review the response and provide approval before sending it to the customer. This simple mechanism can significantly improve the reliability and trustworthiness of the bot. To implement this in LangGraph and MCP, you can use the add conditional edges method to create a conditional edge in the state graph that checks for uncertainty. If the condition is met, the edge can trigger a pause in the execution and send a request for human approval. Here's an example of how this could be implemented in Python: python import langgraph as lg from mcp import tools Create a new state graph graph = lg.StateGraph Add a node for the uncertain state uncertain node = graph.add node "uncertain" Add a conditional edge that checks for uncertainty graph.add conditional edges uncertain node, lambda state: state "confidence" < 0.8, "pause", "request human approval" , "proceed", "provide response" Define the request human approval function def request human approval state : Send a request for human approval approval = tools.request approval state "response" if approval: return "proceed" else: return "reject" Define the provide response function def provide response state : Send the response to the customer tools.send response state "response" Create an MCP tool to handle the human approval request tool = tools.Tool "human approval", request human approval, resources= "human operator" Add the tool to the graph graph.add tool tool In this example, the add conditional edges method is used to create a conditional edge that checks if the confidence level is below a certain threshold. If the condition is met, the edge triggers a pause in the execution and sends a request for human approval using the request human approval function. The request human approval function sends a request for approval to a human operator and returns a decision based on the response. One practical gotcha to watch out for when implementing human-in-the-loop agents is ensuring that the pause mechanism is properly synchronized with the human approval process. If the pause is not properly synchronized, the agent may continue executing before receiving the human approval, which can lead to inconsistent or incorrect results. To avoid this, make sure to use a robust synchronization mechanism, such as a callback or a promise, to ensure that the agent waits for the human approval before proceeding. As we continue to build more complex agentic AI systems, the need for human-in-the-loop mechanisms will become increasingly important. Tomorrow, we'll explore another critical aspect of building reliable agents, one that will help us tackle even more challenging scenarios and edge cases.