<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shubham Vora on Medium]]></title>
        <description><![CDATA[Stories by Shubham Vora on Medium]]></description>
        <link>https://medium.com/@shubhamvora?source=rss-f7172afcc93b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*fH53gf9ijq8Z9KzP</url>
            <title>Stories by Shubham Vora on Medium</title>
            <link>https://medium.com/@shubhamvora?source=rss-f7172afcc93b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 05:38:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shubhamvora/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Don’t Let Your AI Agents Run Wild: Building a Human-in-the-Loop System with LangGraph]]></title>
            <link>https://shubhamvora.medium.com/dont-let-your-ai-agents-run-wild-building-a-human-in-the-loop-system-with-langgraph-0189bf0c8e20?source=rss-f7172afcc93b------2</link>
            <guid isPermaLink="false">https://medium.com/p/0189bf0c8e20</guid>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[human-in-the-loop]]></category>
            <category><![CDATA[langgraph]]></category>
            <category><![CDATA[multiagent-system]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Shubham Vora]]></dc:creator>
            <pubDate>Fri, 10 Oct 2025 16:09:34 GMT</pubDate>
            <atom:updated>2025-10-10T16:09:34.475Z</atom:updated>
            <content:encoded><![CDATA[<p>A practical guide to building a robust “pause button” for your AI agents using Python, LangGraph, and FastAPI.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g9hJfZCbuPZH5vAi3prwoQ.png" /></figure><p>AI agents are getting smarter and more autonomous. They can research, write, analyze data, and even make decisions. But with great power comes great responsibility — and risk. What happens when an agent is about to perform a critical, irreversible action, like deleting a production database or sending a mass email to all your customers? You need a pause button. You need a safety net.</p><p>This is where a Human-in-the-Loop (HITL) system becomes essential. It provides the crucial oversight needed to guide autonomous agents, ensuring that high-stakes decisions are validated by a human before execution.</p><p>In this article, we’ll walk through how to build a custom HITL mechanism for a multi-agent system. While LangGraph now offers a built-in HITL feature, building our own is a fantastic way to understand the core concepts and gain the flexibility to tailor it to any project. We’ll use Python, LangGraph, and FastAPI to create a robust, real-time approval workflow.</p><h4>The Blueprint: Our HITL Architecture</h4><p>At its heart, our system needs to do one thing reliably: pause an agent, ask a human for permission, and then resume based on the answer. To achieve this without freezing the entire application, we’ll build a system with three main parts:</p><p>1. <strong>An Asynchronous Approval Engine</strong>: The core logic that allows an agent to pause and wait for human input without blocking other operations.</p><p>2. <strong>A Real-time Communication Layer</strong>: A WebSocket bridge powered by FastAPI to instantly send approval requests to a user interface and receive responses.</p><p>3. <strong>An Agent Orchestration Framework:</strong> LangGraph to define how our agents work together and to integrate our custom HITL “pause” points into the workflow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/833/1*n-3kIlN_1ePmFomgfBc0Xw.png" /></figure><h4>The Heart of the System: The Asynchronous Approval Engine</h4><p>The biggest challenge is pausing an agent without halting the entire application. We can’t just use time.sleep(). The solution lies in Python’s asyncio. We’ll use an asyncio.Event — a simple but powerful tool that acts like a signal flag. An agent can pause its execution and await this event, freeing up the system to handle other tasks. It only resumes when another part of our code (the part that gets the human’s response) sets the event.</p><pre># A simplified look at the approval function<br>async def request_human_approval(agent_name, task_details):<br>    # 1. Create a unique ID and a signal flag for this request<br>    approval_id = str(uuid.uuid4())<br>    approval_event = asyncio.Event()<br>    # 2. Send the request to the UI via WebSocket<br>    await websocket.send_json({<br>        &quot;type&quot;: &quot;approval_request&quot;,<br>        &quot;id&quot;: approval_id,<br>        &quot;agent&quot;: agent_name,<br>        &quot;task&quot;: task_details<br>    })<br>    # 3. Pause and wait for the signal (with a timeout!)<br>    try:<br>        await asyncio.wait_for(approval_event.wait(), timeout=300)<br>        # The event was set! Get the human&#39;s response.<br>        return get_stored_response(approval_id)<br>    except asyncio.TimeoutError:<br>        # The human didn&#39;t respond in time.<br>        return {&quot;approved&quot;: False, &quot;reason&quot;: &quot;Request timed out.&quot;}</pre><p>This asynchronous pattern is the key to a responsive HITL system.</p><h4>The Full HITL Flow: A Step-by-Step Sequence</h4><p>So, how does a request travel from the agent to the human and back? This sequence diagram illustrates the entire journey.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9DzUc6JX90sqcKOChvVvdw.png" /></figure><h4>Plugging HITL into LangGraph</h4><p>With our approval mechanism ready, we can now create HITL-enabled nodes in our LangGraph workflow. Let’s say we have a web_researcher agent. Instead of letting it run its tools directly, we’ll wrap its action in our approval function.</p><p>This means our agent node’s logic changes from “do the task” to “ask for permission, then maybe do the task.”</p><pre># Simplified HITL-enabled agent node<br>async def web_researcher_node_hitl(state):<br>    task = state[&quot;task&quot;]<br>    <br>    # Instead of just running the tool, we ask for approval first<br>    approval = await request_human_approval(&quot;Web Researcher&quot;, task)<br>    if approval.get(&quot;approved&quot;):<br>        # Human said yes! Run the tool.<br>        tool_result = web_search_tool(task)<br>    else:<br>        # Human said no.<br>        tool_result = f&quot;Search was not approved. Reason: {approval.get(&#39;reason&#39;)}&quot;<br>    # Update the state and continue the graph<br>    state[&quot;agent_results&quot;].append(tool_result)<br>    return state</pre><p>We then simply swap the standard node with our new HITL-enabled node when defining the graph. The rest of the LangGraph workflow remains the same, completely unaware of the human intervention that just happened.</p><pre>from langgraph.graph import StateGraph, START, END<br><br># Create the state graph<br>workflow = StateGraph(AgentState)<br><br># Add nodes - replacing standard node with HITL-enabled node<br>workflow.add_node(&quot;supervisor&quot;, supervisor_node)<br>workflow.add_node(&quot;web_researcher&quot;, self.web_researcher_node_hitl)  # HITL-enabled!<br>...</pre><h4>Key Takeaways</h4><p>Building a custom HITL system gives you immense control and provides a safety layer for your AI applications. The key principles are:</p><p>- <strong>Embrace Asynchronicity</strong>: Use tools like asyncio.Event to create non-blocking waits, keeping your application responsive.</p><p>- <strong>Use WebSockets for Real-Time</strong>: HTTP requests are too slow for this. WebSockets provide the instant, two-way communication needed for a smooth user experience.</p><p>- <strong>Decouple Logic</strong>: The agent doesn’t need to know how it gets approval. It just needs to ask and wait. This makes your code clean and modular.</p><p>- <strong>Log Everything</strong>: For auditing and debugging, make sure you persistently store every approval request and its outcome.</p><h4>Conclusion</h4><p>By implementing a “pause button” for your agents, you can confidently deploy them in complex, high-stakes environments, blending the power of autonomous AI with the wisdom of human oversight.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0189bf0c8e20" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>