惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
月光博客
月光博客
人人都是产品经理
人人都是产品经理
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence
Vercel News
Vercel News
MyScale Blog
MyScale Blog
爱范儿
爱范儿
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
H
Help Net Security
Last Week in AI
Last Week in AI
阮一峰的网络日志
阮一峰的网络日志
酷 壳 – CoolShell
酷 壳 – CoolShell
L
LangChain Blog
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
宝玉的分享
宝玉的分享
博客园 - 聂微东
云风的 BLOG
云风的 BLOG
J
Java Code Geeks
博客园 - 叶小钗
D
Docker

Stories by HARSHA J S on Medium

Mastering LangChain 1.2: Part 13 — Agentic RAG: Why Your AI Needs to Decide When to Google Mastering LangChain 1.2: Part 12 — The Elephant’s Memory: Persistent AI Agents that Never Forget Mastering LangChain 1.2: Part 11 — Custom Middleware Hooks: Orchestrating the AI Lifecycle Mastering LangChain 1.2: Part 9 — Structured Outputs: Turning Messy Chat into Clean Data Mastering LangChain 1.2: Mastering LangChain 1.2: Mastering LangChain 1.2: Part 6 — Dynamic Tool Security: The “Triple-Lock” Guardrail Mastering LangChain 1.2: Part 5 — Dynamic Model Routing: Escalating to Senior AI in Emergencies Mastering LangChain 1.2: Part 4 — Scaling with Middleware and Smart Summarization
Mastering LangChain 1.2: Part 10 — Human-in-the-Loop: Add...
HARSHA J S · 2026-03-27 · via Stories by HARSHA J S on Medium

HARSHA J S

We have built agents that can think, use tools, scale with middleware, and protect themselves with dynamic security. But in the real world, some actions are simply too dangerous to leave entirely to an AI.

Deploying to production, deleting a database, or sending an invoice to a client — these are high-stakes operations. You don’t want an “autonomous” agent doing these at 3 AM without a human double-checking the parameters.

Today, we’re implementing Human-in-the-Loop (HITL) using LangChain 1.2’s HumanInTheLoopMiddleware. This allows our agent to "pause" right before a dangerous action and wait for a human to say: "Go ahead."

1. Defining “Safe” vs. “Dangerous” Tools

Not every tool needs approval. Checking the cluster health is a “Safe” operation — it’s read-only and harmless. Deploying to production, however, is a “Dangerous” operation.

We signal this to the middleware by categorizing our tools:

@tool
def check_cluster_health():
"""Safe read-only tool. No approval needed."""
return "Cluster Status: HEALTHY"

@tool
def deploy_to_production(version: str):
"""🚨 DANGEROUS TOOL 🚨 Requires Human Approval."""
return f"🚀 SUCCESSFULLY DEPLOYED version {version}!"

2. Configuring the Safety Guard

The HumanInTheLoopMiddleware acts as a traffic controller. We tell it exactly which tools should trigger an "Interrupt."

from langchain.agents.middleware import HumanInTheLoopMiddleware

guard = HumanInTheLoopMiddleware(
interrupt_on={
"deploy_to_production": {
# The human can 'approve' or 'reject'
"allowed_decisions": ["approve", "reject"],
},
# Safe tools should act normally (don't pause)
"check_cluster_health": False,
}
)

3. The “Paused” State and Checkpointers

For an agent to wait for a human, it needs a way to “save its progress.” It can’t just stay running in a loop forever. We use an InMemorySaver (or a database in production) to checkpoint the agent’s state.

from langgraph.checkpoint.memory import InMemorySaver

checkpointer = InMemorySaver()

agent = create_agent(
model=model,
tools=[check_cluster_health, deploy_to_production],
middleware=[guard],
checkpointer=checkpointer, # Save the "wait" state here
)

4. The Intervention Workflow

Let’s see what happens when the user asks for a deployment:

  1. AI Decides: “I need to call
  2. deploy_to_production with version='v2.0'."
  3. Middleware Intervenes: It sees the tool name, checks the interrupt_on config, and pauses execution before the tool is even called.
  4. App Waits: The agent.invoke() call finishes, but the history ends with a "Tool Call" that has no result yet.
  5. Human Resumes: The human reviews the request and clicks “Approve.”
  6. Agent Resumes: We call agent.invoke() again, sending a Command with the decision. The AI then finishes the task.
# Resuming the agent after human approval
final_result = agent.invoke(
Command(resume={"decisions": [{"type": "approve"}]}),
config=config # The unique ID for this conversation
)

Conclusion: Trust Through Control

Human-in-the-Loop isn’t about limiting your AI; it’s about building trust. When stakeholders know they have a “kill switch” or an approval button for critical actions, they are much more likely to adopt AI agents in their core business workflows.

With LangChain 1.2, you can build agents that are as autonomous as you want them to be, but as controlled as you need them to be.

💬 What do you think?
Drop your thoughts, questions, or suggestions in the comments below!

Check out my YouTube channel for more exciting content! [YouTube Channel Link — Harsha Selvi]

Disclaimer: This text has been rephrased using AI tools, and some parts are derived from various sources to provide a comprehensive overview.

#AI #LangChain #MachineLearning #CyberSecurity #DevOps #SRE #Python #AIEngineering #Automation #SoftwareSafety