How to Build Human‑In‑The‑Loop (HITL) Capabilities into ReactAgent
This article explains how to integrate a Human‑In‑The‑Loop (HITL) mechanism into ReactAgent, detailing the motivation, design of interaction, tool description, XML‑based UI rendering, Redis‑driven waiting loop, and the broader architectural parallels with design patterns and other agent frameworks.
Overview
The article describes the introduction and practical implementation of a Human‑In‑The‑Loop (HITL) mechanism within the ReactAgent framework, focusing on design choices, code structure, and the relationship between agent capabilities and software engineering patterns.
Why HITL Is Needed
Without a generic human‑machine collaboration layer, agents rely on limited strategies that cause semantic drift and loss of control:
Multi‑turn clarification: When the user’s intent is unclear, the system prompts the user to supplement the intent, then re‑infers the task using the updated context.
Tool description constraints: For tools with strict input requirements, the tool description can request missing parameters from the user, but this approach still leaves the agent in control and can lead to inaccurate context compression.
These two tactics are essentially pseudo‑human‑in‑the‑loop because the agent finishes its reasoning before the user can intervene, which may cause loss of information during context compression or repeated reinterpretation of system prompts.
Core HITL Requirements
Design an interaction that allows users to supplement tasks and influence execution flow.
Maintain a single continuous conversation by suspending the dialogue instead of closing it, using a wait‑and‑resume mechanism.
HITL Implementation Effects
Two simple cases illustrate the effect of human‑in‑the‑loop interaction:
Case 1 – Unclear parameters: The agent pauses the current dialogue, the user provides the missing parameter in real time, and the agent resumes execution within the same conversation.
Case 2 – Steering task direction: The user directly influences the task flow through the HITL tool, avoiding a separate follow‑up dialogue.
Implementation Details
Interaction Design
A custom XML protocol is used to embed HTML‑compatible tags that the front‑end can render directly, enabling rich UI components without additional client‑side processing. The following screenshots show the rendered UI:
The XML tags allow the agent to present a complex form that the user can fill in without leaving the conversation.
HITL Tool Definition
A dedicated tool is defined for situations where a parameter is missing or the user must intervene. The tool’s output is defined as the user’s supplemental content. When the LLM decides to call the tool, the system first returns an empty UI placeholder because the required parameters are not yet known. After the user submits the form, the tool returns the user input as its result, and the LLM continues processing.
Conversation Suspension and Reconnection
The HITL tool uses a Redis‑backed waiting loop. The tool repeatedly queries Redis for a key named AnswerId until the user’s input appears, then returns that value to the LLM. A timeout guard prevents endless waiting.
// Pseudocode for the waiting loop
while (!redis.exists(answerId)) {
Thread.sleep(200); // short pause before next check
// optional: break after a maximum number of attempts to avoid deadlock
}
String result = redis.get(answerId);
return result; // feed back to LLMDesign Pattern Parallels
The HITL approach mirrors several classic software‑engineering concepts:
Hooks / Interceptors: The tool intercepts the normal agent flow to request human input, similar to AOP advice.
Aspect‑oriented programming: The HITL logic is a cross‑cutting concern that can be woven into any agent task.
Factory pattern: Future extensions could generate specialized sub‑agents on‑demand based on task requirements.
Adapter / Anti‑corruption layer: Wrapping external tool calls behind a uniform interface shields the LLM from heterogeneous APIs.
Lazy loading / circular dependency handling: Tools are loaded only when needed, reducing prompt size and improving context management.
Multi‑level caching: Short‑term (Redis) and long‑term (database) caches manage memory and reduce repeated LLM calls.
Agent Evolution and Engineering Design Intersections
The article maps several design patterns to agent capabilities, showing how concepts such as hooks, interceptors, responsibility chains, lazy loading, and multi‑level caches can be directly applied to improve agent reliability and extensibility. It also compares ReactAgent’s loop‑plus‑responsibility‑chain architecture with graph‑based orchestration frameworks like Dify and LangGraph.
Conclusion
Implementing HITL in ReactAgent provides a concrete way to let humans steer LLM‑driven tasks, reduces semantic drift, and aligns agent engineering with well‑known software design patterns. The author suggests future work on dynamically generating specialized agents via factory patterns and further exploring how design patterns can inspire new agent behaviours.
Alibaba Cloud Developer
Alibaba's official tech channel, featuring all of its technology innovations.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
