Trung BìnhAI Engineering iconAI Engineering

Human-in-the-loop (HITL) trong AI agent: khi nào cần và cách thiết kế?

HITL = agent dừng lại hỏi/xin approval của human trước khi tiếp tục, thay vì chạy fully autonomous. Cần thiết để giảm rủi ro và giữ control.

Khi BẮT BUỘC có HITL:

1. Destructive action — delete, drop table, git push --force, send email tới khách hàng, charge credit card, cancel subscription, send message to production slack.
2. Irreversible action — merge PR, deploy production, transfer fund.
3. High-cost operation — task tốn > $X (rate limit quota, book flight, gửi API call tính phí cao).
4. Low confidence — agent không chắc (self-evaluate thấp, retrieval không ra relevant doc, câu hỏi ambiguous).
5. Ngoài scope — request vượt quyền hạn agent (medical/legal advice, content chính trị nhạy cảm).
6. Compliance / regulated — domain healthcare/finance/legal — yêu cầu audit trail + human sign-off.

Pattern thiết kế HITL:

1. Approval before action (phổ biến nhất)
- Agent propose action → frontend render confirmation UI → human click approve/reject/edit.
- Ví dụ Claude Code: "Tôi định chạy rm -rf node_modules, ok?".
- LangGraph có primitive interrupt() tự động pause graph, resume khi có input.

2. Edit-in-place
- Agent sinh draft → human edit trực tiếp → agent tiếp tục với version đã edit. Phù hợp writing, code gen.

3. Multi-choice
- Agent đưa 2-3 option → human chọn. Tốt khi agent không đủ info để quyết.

4. Async review
- Agent chạy hoàn toàn, log mọi action → human review sau (hậu kiểm). Phù hợp khi latency HITL không chịu được, rủi ro thấp.
- Ví dụ: moderation agent flag content → human reviewer duyệt backlog.

5. Escalation / Handoff
- Agent phát hiện không xử lý được → transfer toàn bộ conversation cho human agent (customer support).

6. Periodic checkpoint
- Với long-running task (research 1h, code migration): pause sau mỗi N step hoặc checkpoint logical → human verify progress trước khi tiếp.

Implementation tips:

  • State persistence — khi pause cần lưu full state (conversation, agent scratchpad, pending action) để resume. Dùng DB (Postgres, Redis) hoặc LangGraph checkpointer.
  • Timeout — nếu human không respond trong X phút → fallback (cancel, defer, notify).
  • Context for approver — hiển thị đủ info để quyết: what action, why agent chose it, expected outcome, risk. Đừng bắt human duyệt blind.
  • Batch approval — cho các action đồng loại (approve all file edits), giảm fatigue.
  • Progressive autonomy — khởi đầu strict HITL; sau khi trust qua metrics → relax dần (ví dụ auto-approve nếu confidence > 0.9).
  • Audit log — ghi lại mọi approval decision (who, when, what) cho compliance.

Framework: LangGraph có built-in interrupt_before, interrupt_after, human node; CrewAIhuman_input=True trên task; OpenAI Assistantsrequired_action event.

Xem toàn bộ AI Engineering cùng filter theo level & chủ đề con.

Mở danh sách AI Engineering