Series 5 - Bài 6: Human-in-the-Loop
Human-in-the-Loop không phải dấu hiệu agent kém. Đó là design pattern cho high-stakes decisions. Bài này xây dựng framework quyết định khi nào agent tự làm, khi nào phải hỏi.
Tập này đang được chuẩn bị, quay lại sau nhé.
Có một misconception phổ biến: agent tốt là agent có thể tự hoàn thành mọi task mà không cần can thiệp của người.
Thực tế ngược lại. Agent tốt biết khi nào không nên tự quyết định — và dừng lại đúng lúc để hỏi người đúng câu hỏi.
Human-in-the-Loop (HITL) không phải dấu hiệu weakness. Đó là design pattern cho high-stakes decisions trong hệ thống autonomous.
HITL là gì, không phải là gì
HITL KHÔNG phải là:
Agent fail và cần người fix
Agent không đủ intelligent để quyết định
Fallback khi automation không work
HITL là:
Deliberate design decision cho certain actions
Risk management cho high-stakes choices
Accountability mechanism cho consequential decisions
User agency trong autonomous workflow
Sự khác biệt quan trọng:
Agent fail → HITL là reactive (bad)
Agent design → HITL là proactive (good)
Decision Framework: Khi nào HITL
Impact of Wrong Decision
Low High
┌──────────────┬──────────────┐
Reversibility │ │ │
Reversible │ Auto (Tier1)│ Auto + Log │
│ │ │
├──────────────┼──────────────┤
Irreversible │ Confirm │ HITL Always │
│ │ │
└──────────────┴──────────────┘
Quadrant 1 (Reversible, Low impact):
read_file, list_directory, search
→ Auto, không cần confirm
Quadrant 2 (Reversible, High impact):
write_file (có thể revert với git)
→ Auto nhưng log đầy đủ, có thể review
Quadrant 3 (Irreversible, Low impact):
send_notification (không thể unsend)
→ Confirm một lần, remember preference
Quadrant 4 (Irreversible, High impact):
delete_file, deploy to production, modify DB
→ HITL luôn, không exception
Confidence-based HITL
Ngoài action type, confidence cũng trigger HITL:
Agent self-assessment:
"Tôi hiểu task rõ ràng" → proceed
"Task có ambiguity" → clarify trước
"Approach này có significant trade-offs" → present options
"Task scope lớn hơn estimate" → report và confirm
LLM có thể signal low confidence:
"I'm not entirely sure about the requirements..."
"This could be interpreted two ways..."
→ System detect phrases này
→ Trigger confirmation flow trước khi tiếp tục
HITL Interaction Patterns
Pattern 1: Pre-action Confirmation
Trước khi thực hiện action:
Agent muốn: delete_file("src/legacy-module.ts")
HITL flow:
Agent → User:
"Tôi sắp xóa src/legacy-module.ts (245 lines).
File này không được reference ở nơi nào khác.
Xác nhận xóa? [y/n]"
User: y → proceed
User: n → cancel, agent tìm alternative
Tốt cho: destructive actions, high-impact changes
Latency: adds 1 round trip (user response time)
Pattern 2: Pre-task Clarification
Trước khi bắt đầu task:
User: "Fix the performance issues"
Agent detect ambiguity → HITL:
"Tôi có thể tiếp cận 'performance issues' theo vài hướng:
Option A: Profile và identify bottlenecks (read-only, ~30 min)
Option B: Optimize database queries (moderate changes)
Option C: Full performance refactor (significant changes)
Hướng nào bạn muốn?"
User chọn → agent proceed với clear scope
Tốt cho: ambiguous tasks, large scope
Latency: before task start, không block execution
Pattern 3: Mid-task Checkpoint
Giữa task khi phát hiện unexpected:
Agent đang refactor payment module
Giữa chừng phát hiện: một function không có tests
Agent → User (checkpoint):
"Tôi đang refactor processPayment và phát hiện
validateAmount() không có test coverage.
Tiếp tục mà không có tests có thể introduce bugs.
Tôi nên:
A) Viết tests trước rồi mới refactor (thêm ~1 giờ)
B) Tiếp tục refactor, thêm TODO comment về tests
C) Dừng và báo cáo, bạn quyết định"
Tốt cho: unexpected findings, scope change
Latency: block mid-execution — cần design chu đáo
Pattern 4: Post-task Review
Sau khi hoàn thành, trước khi finalize:
Agent hoàn thành refactor
Agent → User:
"Tôi đã hoàn thành refactor processPayment.
Summary:
- Modified: src/payment/payment.service.ts
- Added: 3 test cases for edge cases
- Tests: 52/52 pass
Có gì cần adjust không trước khi tôi report done?"
Tốt cho: quality gate trước khi commit
Latency: after execution, không block workflow
Async HITL: Không block execution
Synchronous HITL (blocking):
Agent dừng → gửi question → đợi user respond
→ User phải respond ngay
→ Không scale cho long-running tasks
Asynchronous HITL (non-blocking):
Agent gặp decision point
→ Save state
→ Queue question cho user
→ Continue với other work (nếu có)
→ User responds khi convenient
→ Agent resume từ saved state
Use cases cho async:
Long-running background agents
Batch processing
Agents running in different timezone
HITL UX: Câu hỏi tốt vs xấu
Bad HITL question:
"Should I proceed with the changes?"
→ Không cho context
→ User không biết đã xảy ra gì
→ Binary yes/no không inform decision
Good HITL question:
"Tôi sắp xóa 3 files:
- src/legacy/old-payment.ts (deprecated, không dùng)
- src/legacy/old-validation.ts (deprecated, không dùng)
- src/legacy/types.ts (vẫn được import ở 2 places)
File thứ 3 vẫn có references. Tôi có thể:
A) Xóa tất cả 3 (sẽ gây compile error, cần fix)
B) Xóa 2 file đầu, keep file thứ 3
C) Migrate references trước rồi xóa tất cả"
Good HITL:
Cung cấp đủ context
Đưa ra options cụ thể
Explain consequences mỗi option
Cho user make informed decision
Trade-off: More HITL vs Less HITL
More HITL:
+ Safer, ít accidents hơn
+ User luôn in control
+ Audit trail rõ ràng về decisions
- Friction, agent phải đợi
- User bị interrupt nhiều
- Latency tăng
Less HITL:
+ Smooth, agent tự chạy
+ Low latency
- Accidents xảy ra mà không có human check
- User không biết agent đã làm gì
Balance:
HITL cho irreversible + high-impact actions
Auto cho reversible + low-impact actions
HITL không nên exceed 20% của tổng actions
(nếu cao hơn → permissions quá chặt hoặc tasks quá risky)
Kết luận
- HITL là design decision, không phải fallback khi agent fail.
- Decision matrix: irreversibility × impact = HITL requirement.
- Confidence-based HITL: agent tự detect ambiguity và trigger confirmation.
- Bốn patterns: pre-action, pre-task clarification, mid-task checkpoint, post-task review.
- Async HITL để không block long-running tasks.
- HITL UX quan trọng: cung cấp context, options, và consequences cho user.
- HITL > 20% actions là signal: permissions quá chặt hoặc workflow design sai.