Human-in-the-Loop for Agentic Applications: A Detailed Summary
Key Concepts:
- Human-in-the-Loop (HITL): Strategic intervention points in agentic processes requiring human approval or feedback.
- Agentic Applications: AI systems capable of autonomous action and decision-making.
- Structured Output: Utilizing LLMs to generate outputs in a predefined format for routing application flow.
- Tool Calling: Enabling LLMs to invoke specific functions (tools) based on their reasoning.
- State Serialization: Persisting the context of an agentic process (messages, actions, parameters) for deferred execution.
- SSC Streaming: Server-Sent Events – a technique for real-time communication, useful for interactive HITL scenarios.
- Async Process: Handling HITL in backend workflows via notifications and separate execution endpoints.
- State Store: A database or mechanism for storing the current state of an agentic process.
I. Introduction: The Need for Human-in-the-Loop
Current AI advancements necessitate a Human-in-the-Loop (HITL) approach for many agentic applications. While LLMs can safely handle tasks like balance checks and deposits, more sensitive operations, particularly high-value transactions (e.g., money transfers), require human oversight. HITL allows for faster deployment (handling 50% of cases automatically while humans address the remaining 50% – the “edge cases”) and increased reliability by identifying and managing complex scenarios. Implementing HITL effectively, however, is challenging, extending beyond backend code to encompass the entire application architecture (frontend, API, agent, database).
II. Two Backend Approaches to Halting Processes
The core of HITL implementation lies in strategically halting the process to request human input. Two primary methods exist:
A. LLM as a Router (Structured Output):
This approach leverages the LLM to classify actions and route the workflow accordingly. An “action model” (using libraries like Pydantic) defines possible actions (e.g., check balance, transfer, deposit). The LLM outputs an “action plan” – a list of actions to execute. A “model validator” enforces confirmation requirements based on specific criteria (e.g., transfer amount exceeding $100).
- Example: A banking agent receives a request to transfer $500 to Alice. The LLM generates an action plan including a transfer action. The model validator recognizes the amount exceeds the threshold and sets
requires_confirmationtotrue. The process halts, awaiting human approval. - Technical Detail: Pydantic is used to define data structures and validate LLM outputs, ensuring adherence to the defined action model.
- Quote: “...offloading some of the decision-m logic and taking that more towards our back end rather than specifically relying on the tool calls and the LLM for the LLM to decide that.” – highlighting the benefit of backend control.
B. Tool Calling:
This more “agentic” approach grants the LLM access to tools and allows it to decide when to use them. The process is halted when the LLM initiates a tool call requiring approval.
- Example: The LLM, prompted with a transfer request, decides to use the “transfer_money” tool. The system detects this tool call and halts execution, prompting for confirmation.
- Technical Detail: The code iterates through the LLM’s output, specifically looking for tool calls to trigger the approval process.
- Nuance: Halting the process differs slightly; with structured output, it’s based on the LLM’s output, while with tool calling, it’s based on the LLM specifically invoking a tool.
III. Production Patterns for Seamless Integration
Implementing HITL in a production environment requires coordinating multiple components. Two common patterns are:
A. Server-Sent Events (SSC) Streaming:
Ideal for real-time, interactive applications like chat assistants.
- Process Flow:
- User initiates a request (e.g., transfer $150).
- Frontend sends the request to the API.
- API passes the request to the agent.
- Agent determines approval is needed (amount > $100).
- Agent signals the API that a tool call is pending approval.
- API sends a notification to the frontend (requiring approval).
- Frontend displays an approval button to the user.
- User approves/denies.
- Frontend sends the approval status to the API (via a separate endpoint).
- API loads the previous agent state from a “state store” (database).
- Agent resumes execution with the approved action.
- Results are streamed back to the frontend.
- Key Feature: The connection is closed after the approval request is sent, preventing timeouts and ensuring state preservation.
- Technical Detail: A “state store” (database) is crucial for persisting the agent’s context (messages, actions, parameters) during the approval process.
B. Async Process:
Suitable for backend workflows where immediate user interaction isn’t required (e.g., invoice processing).
- Process Flow:
- A backend process (e.g., invoice received) triggers the workflow.
- A worker (event-driven architecture or API endpoint) processes the request.
- Agent determines approval is needed.
- Agent sends a notification (e.g., Slack message) to a designated user.
- User approves/denies via the notification interface.
- Approval status is sent to the API.
- API loads the agent state from the state store.
- Agent resumes execution.
- Completion notification is sent.
- Key Feature: Relies on a notification surface (email, Slack) to facilitate human interaction.
IV. Core Principles for Building HITL Systems
- Deferred Execution: When approval is required, save the state instead of executing the action.
- State Serialization: Persist all necessary agent context (messages, actions, parameters) for resuming the process.
- Stateless Resume: Load the entire state from storage upon resuming, ensuring resilience to timeouts and long delays.
V. Conclusion
Implementing Human-in-the-Loop is crucial for building reliable and scalable agentic applications. The video detailed two backend approaches (structured output and tool calling) for halting processes and two production patterns (SSC streaming and async processes) for integrating HITL into a complete application architecture. The key takeaway is the importance of state management – saving and restoring the agent’s context to enable seamless resumption after human intervention. The provided GitHub repository and linked playlist offer further resources for building and deploying production-ready AI agents.
AI summaries can miss context or contain errors. Check important details against the original video.





