Hello! In our last lesson, we built a more resilient agent by implementing automatic retries with exponential backoff. This strategy is excellent for handling transient failures where an external service is temporarily unavailable.
However, some situations are too critical for a machine to handle alone. What happens when all retries fail? Or what if the agent is about to perform a high-stakes, irreversible action like transferring a large sum of money or deleting a database? For these scenarios, we need a different kind of fallback: human oversight.
Today, we will learn how to design and implement a human-in-the-loop (HITL) fallback mechanism for critical agent errors. This is a crucial pattern for building safe, reliable, and trustworthy agents that can be deployed in production environments. We'll explore the core architectural principles and see how modern Agent Development Kits (ADKs) help you implement them.
1. What is Human-in-the-Loop (HITL)?
At its core, HITL is a design pattern where an autonomous system intentionally pauses its execution at a critical decision point and waits for human input before proceeding. It's about combining the speed and efficiency of automation with the judgment and oversight of a human.

This pattern is essential for:
- Safety: Preventing agents from taking destructive or costly actions without approval.
- Reliability: Handling situations where the agent is uncertain or has encountered an error it cannot resolve automatically.
- Trust: Giving users confidence that they have ultimate control over the agent's behavior.
- Compliance: Meeting regulatory requirements in fields like finance or healthcare, where human sign-off is mandatory.
How to Build Human-in-the-Loop for AI Agents (Practical Guide)
To start, let's watch a brief introduction that explains what HITL is and why it's so important for agentic applications, using a practical banking agent example.
Watch the first 1 minute and 37 seconds of this video by Dave Ebbelaar. Focus on the distinction he makes between safe actions (checking a balance) and sensitive actions (transferring money) that warrant human approval.
2. The Architectural Pattern for Production HITL
Implementing HITL in a real application is more complex than simply using Python's input() function, which would block your backend server. As a software engineer, you'll recognize the need for a more robust, scalable architecture. A production-grade HITL system relies on three key principles:
- Deferred Execution (Interrupt): When a sensitive action is identified, its execution is paused. The agent's graph of operations is interrupted.
- State Serialization: The agent's complete state at the moment of interruption is saved to a persistent store (like a database or a checkpointer). This includes the conversation history, the pending action and its parameters, and any other context needed to resume later.
- Stateless Resume: The agent's process can be shut down entirely while waiting for human input. When the human responds (perhaps minutes or hours later), a new process is started, it loads the saved state from the persistent store, incorporates the human's decision, and continues the execution. This makes the system resilient and scalable.
The following video provides an excellent walkthrough of two production patterns for implementing this architecture: one for real-time chat applications and another for asynchronous backend workflows.
How to Build Human-in-the-Loop for AI Agents (Practical Guide)
This next segment from the same video details the full-stack implementation of HITL. This will be highly relevant to your work as a software engineer, as it connects the agent logic to the frontend, API, and database.
Please watch from 12:19 to 23:49. Pay close attention to: The 'SSC streaming' pattern for real-time chat, noting how the state is saved before the connection is closed and resumed via a separate API call. The 'Async process' pattern for backend tasks, which uses a notification layer (like Slack or email). The final summary of the three key concepts: deferred execution, state serialization, and stateless resume.
Test your understanding!
An agent that manages a user's cloud infrastructure needs to delete a virtual machine. This is a destructive action. Using the three core principles we just discussed (deferred execution, state serialization, stateless resume), outline the sequence of events that should occur to implement a safe, HITL approval process.
Show answer
- Deferred Execution: The user asks the agent to "clean up unused resources." The agent reasons that this involves deleting a specific virtual machine. It identifies the
delete_vmtool as sensitive and interrupts its execution before calling the tool. - State Serialization: The agent system saves its current state to a database. This state includes the chat history, the identified action (
delete_vm), and the target VM's ID. It then sends a response to the user's interface: "I am about to delete VM 'test-server-123'. Please confirm." - Notification & UI: The user interface displays the confirmation message with "Approve" and "Deny" buttons. The backend process can now terminate, as the state is safely stored.
- Stateless Resume: The user clicks "Approve." The frontend makes a new API call to an endpoint like
/resume-taskwith the task ID and the approval. The backend service starts up, loads the saved state from the database, sees the approval, and now proceeds to execute thedelete_vmtool. It then reports the success back to the user.
3. HITL in Agent Development Kits
Now let's see how different ADKs provide abstractions to implement this architecture. They handle much of the complexity of interrupts and state management for you.
A. LangGraph: Interrupts and Checkpointers
LangGraph is designed with this type of control flow in mind. Its approach is considered highly robust and is built around the concept of explicit interrupts and persistent checkpointers.
Adding Human-in-the-Loop to DeepAgents
The LangChain team has a video demonstrating how to add HITL to their agents. This is a very practical look at a code-first implementation.
Please watch from 00:28 to 09:11. This covers the core implementation details. Focus on: The interrupt_config used to mark specific tools for HITL. The critical role of the checkpointer for saving and resuming state. The three different interaction patterns you can offer the human: allow_accept, allow_edit, and allow_respond. How the agent's execution is resumed using a command object after the human provides input.
As the video demonstrates, LangGraph's approach is powerful because it gives you fine-grained control over the human's interaction:
- Accept: The human simply approves the action as-is.
- Edit: The human can modify the parameters of the tool call before it runs (e.g., changing the recipient of an email).
- Respond: The human can provide a mock response instead of running the tool, telling the agent how to proceed (e.g., "Error, the user said to sign the email differently").
This is a direct implementation of the architecture we discussed: the interrupt_config enables deferred execution, the checkpointer handles state serialization, and the resume command facilitates a stateless resume.
B. Google's ADK: Callbacks and Conditions
Google's ADK, being a more foundational framework, approaches this with a flexible system of callbacks and conditional interrupts. You define the conditions under which the agent should pause.
Making AI Agents Enterprise-Ready - Part 3 of 4
This blog post by Matteo Gazzurelli provides a concise explanation and a code snippet showing the HITL pattern in Google's ADK.
Read the section 'Pattern 25: Human-in-the-Loop (HITL)'. Pay attention to the LlmAgent constructor and its workflow_interrupts parameter. Note how it maps a condition to a handler function.
In Google's ADK, you configure workflow_interrupts with a list of dictionaries. Each dictionary specifies:
- A
"condition": A string that represents a state requiring human review (e.g.,"high_risk_decision"or"confidence < 0.7"). The agent's prompt must be engineered to output this condition string when appropriate. - A
"handler": An asynchronous Python function (e.g.,human_review) that gets called when the condition is met. This handler is responsible for notifying the human and waiting for their response.
This approach offers flexibility but places more responsibility on you to implement the handler logic (notifying the user, waiting for input) and to ensure the LLM reliably outputs the correct condition strings through careful prompt engineering.
The article "HITL in AI Agents: Framework Comparison Guide" provides a good summary of these different philosophies. It notes that while LangGraph has the most mature, built-in support for HITL, the simplicity of Google ADK's callback approach can make it easier to understand the underlying patterns.
Conclusion
You have now moved beyond simple error handling to one of the most important patterns for deploying agents that interact with the real world. By designing a human-in-the-loop fallback, you create a safety net that makes your agent more reliable, trustworthy, and ready for production.
Key Takeaways:
- HITL is a critical safety pattern for actions that are high-stakes, irreversible, or ambiguous.
- A production-ready HITL system requires an architecture based on deferred execution, state serialization, and stateless resume.
- Agent Development Kits abstract this complexity:
- LangGraph uses a robust system of explicit
interruptsconfigured per tool, backed by a persistentcheckpointer. It offers rich interaction patterns like accept, edit, and respond. - Google's ADK uses a flexible system of conditional
workflow_interrupts, where you map custom condition strings to handler functions that you implement.
- LangGraph uses a robust system of explicit
- Implementing a full HITL workflow involves orchestrating the agent backend with a database, an API, and a user-facing frontend.
In the past few lessons, we've focused on making our agents more reliable by handling failures automatically or with human help. But how do we know if the agent is actually good at its job? In our next lesson, we will shift our focus to evaluation and learn how to define and apply evaluation metrics for agent performance, such as task success rate and token cost.