人類參與迴圈架構如何實踐「人類優先、機器其次」
Day 28: Human-in-the-Loop Architecture — Machine Second, Human First in Practice
這篇實踐指南提出人類參與迴圈架構,將低影響、可逆操作設為自動執行,並要求高影響或不可逆操作經人工審批。操作類別由程式碼和政策設定,而非交由模型自行判斷;高影響操作會令智能體暫停並保存狀態,向審批者提供指令、推理及預期結果。批准經驗證後工作流恢復,拒絕則把人工備註加入上下文並重新規劃;文中附有 Python 核心邏輯,並提到 LangGraph 內置 interrupt 和 resume。
Efficiency is the goal of automation, but in high-consequence settings, full autonomy is the wrong target. Actions that change production data, move money, reconfigure network hardware or contact customers should combine the machine's speed with verified human judgment. The principle is Human First, Machine Second.
In plain terms: A junior colleague can read, research and draft all day without asking. But before they wire money or delete a database, they get a sign-off from someone senior.
1. Define the decision boundary
Divide actions into two classes with automatic gates.
- Reversible, low-impact (fully autonomous): reading documentation, summarising logs, querying read-only metrics, drafting a remediation plan.
- Irreversible or high-impact (gated): dropping a table, changing a switch VLAN, deploying to production, approving spending.
Decide the class in code and policy, using an allow-list of action types. Do not ask the model whether its own action is risky, because a confused or manipulated model will answer wrongly.
Diagram: Low-impact reversible actions such as reading docs, summarising logs and drafting plans can run autonomously. Irreversible or high-impact actions such as dropping tables, changing network configuration, deploying to production or approving spend need human approval. The classification is set in code and policy, not by the model. See the animated version.
2. Engineer the approval gate
A human-in-the-loop workflow needs an asynchronous pause and resume inside your orchestration.
-
Interrupt. When an action is high-impact, the agent stops, saves its state to durable storage and enters a
WAITING_FOR_APPROVALstate. - Notify with context. An alert goes to Slack, Teams or a portal with the proposed command, the agent's reasoning and the expected outcome.
- Authorise. The human approves or rejects, and the decision is authenticated and signed so it cannot be forged.
- Resume or revise. On a verified approval the saved state is restored and the action runs. On a rejection the human's note is added to the agent's context so it drafts a safer alternative.
Diagram: When an action meets the high-impact criteria the agent halts and saves its state, a notification with the proposed command and reasoning goes to a human, the human approves or rejects with authenticated authorisation, and the workflow resumes from stored state or revises its plan. See the animated version.
Diagram: A good approval request shows the proposed command, the agent's reasoning and the expected outcome in one place, with approve and reject actions. The example is illustrative. See the animated version.
Diagram: If the human rejects a proposed action, their note is added to the agent's context so it drafts a safer alternative. This keeps people reviewing only the actions that matter while the machine does the legwork. See the animated version.
Here is the core logic in Python. It assumes a database helper db for saving state and a shared secret for signing approvals. In production you would use your identity provider and a proper signing scheme, and a framework such as LangGraph offers built-in interrupt and resume.
import hmac, hashlib, json
HIGH_IMPACT = {"drop_table", "change_vlan", "deploy_prod", "approve_spend"} # policy, not model opinion
SECRET = b"load-from-a-secrets-manager"
def execute_or_pause(run_id: str, action: dict, state: dict):
if action["type"] not in HIGH_IMPACT:
return perform(action) # reversible: just do it
db.save(run_id, {"status": "WAITING_FOR_APPROVAL", "action": action, "state": state})
notify_approvers(run_id, action, reasoning=state["reasoning"]) # command + why + expected outcome
return "paused"
def sign(run_id: str, decision: str, approver: str) -> str:
msg = f"{run_id}:{decision}:{approver}".encode()
return hmac.new(SECRET, msg, hashlib.sha256).hexdigest()
def resume(run_id: str, decision: str, approver: str, signature: str, note: str = ""):
if not hmac.compare_digest(signature, sign(run_id, decision, approver)):
raise PermissionError("Invalid approval signature") # never trust an unsigned decision
saved = db.load(run_id)
if decision == "approve":
return perform(saved["action"])
saved["state"]["human_feedback"] = note # rejection: teach the next attempt
return replan(saved["state"])
Balancing autonomous reasoning with human oversight keeps the organisation safe while still capturing most of the productivity gain, because people only review the small share of actions that carry real risk.
Coming Up Next
Day 29: Managing technical debt, drift and model governance in enterprise IT.
HumanInTheLoop #HITL #EnterpriseGovernance #TechLeadership #ProcessAutomation
Originally published at https://sureshpallapothu.in/blog/day-28-human-in-the-loop, where this post includes animated diagrams.
來源:DEV Community · dev.to