How to Build Human-in-the-Loop for AI Agents (Practical Guide)

The video explains how to implement human-in-the-loop (HITL) systems for AI agents, allowing AI to handle routine tasks autonomously while requiring human approval for higher-risk actions. It covers practical backend strategies, including using large language models as routers or tool callers, and outlines scalable architectural patterns—such as state persistence and asynchronous workflows—for integrating HITL into production applications.

The video provides a practical guide to implementing human-in-the-loop (HITL) systems for AI agents, focusing on scenarios where human approval or feedback is necessary before an AI agent can proceed with certain actions. The presenter uses the example of a banking agent, where actions like checking a balance or making a deposit can be automated, but transferring large sums of money should require human approval. This approach allows organizations to deploy AI-powered applications more quickly and safely, as the AI can handle straightforward cases autonomously while humans intervene in edge cases or higher-risk operations.

Two main backend implementation strategies are discussed: using a large language model (LLM) as a router and using tool calling. In the router approach, the LLM classifies user requests and outputs an action plan, which is then checked in the backend for actions that require human confirmation. For example, if a transfer amount exceeds a certain threshold, the backend enforces a confirmation step, regardless of the LLM’s output. In the tool calling approach, the LLM is given access to specific tools (functions), and the backend monitors for tool calls that require approval, pausing execution until human input is received.

The video demonstrates these concepts with Python code examples, showing how to halt the process and wait for user input using simple logic and Python’s input function. While this is effective for prototyping and testing, the presenter notes that this approach is not suitable for production environments, as it does not scale or integrate well with real-world application architectures involving frontends, APIs, and databases.

To address production needs, the presenter outlines two common architectural patterns: Server-Sent Events (SSE) streaming for real-time chat applications, and asynchronous (async) processes for backend workflows. In the SSE pattern, the frontend communicates with the backend via API calls, and when human approval is needed, the system saves the current state to a database and notifies the user through the frontend. The user can then approve or deny the action, and the backend resumes the process by loading the saved state. In the async pattern, typically used for backend or event-driven workflows, notifications (such as emails or Slack messages) are sent to users for approval, and the process resumes once approval is received, again by retrieving the saved state from storage.

The key takeaways for building robust HITL systems are to enable deferred execution by saving state instead of executing actions immediately, to serialize and persist all necessary context and pending actions, and to ensure stateless resumption so that processes can be reliably continued even after delays or interruptions. The video concludes by emphasizing the importance of understanding the broader application architecture when implementing HITL, and encourages viewers to explore further resources on building production-ready AI agents.