Skip to content
← Back to activity

Immutable solution version

Compose a middleware stack: PII redaction, summarization, human approval

Version 1Sign in to save

Complete solution

The three prebuilt middlewares that cover most production agents: PIIMiddleware to keep sensitive data from the model, SummarizationMiddleware to bound history, HumanInTheLoopMiddleware to gate risky tool calls. Compose them in one list.

Middleware stack

from langchain.agents import create_agent
from langchain.agents.middleware import (
    PIIMiddleware,
    SummarizationMiddleware,
    HumanInTheLoopMiddleware,
)

agent = create_agent(
    model="claude-sonnet-4-6",
    tools=[read_email, send_email],
    middleware=[
        PIIMiddleware("email", strategy="redact", apply_to_input=True),
        SummarizationMiddleware(model="claude-sonnet-4-6", trigger={"tokens": 500}),
        HumanInTheLoopMiddleware(
            interrupt_on={"send_email": {"allowed_decisions": ["approve", "edit", "reject"]}}
        ),
    ],
)

Rules

  • Order matters: PII redaction belongs early so sensitive data never reaches the model or the trace. Summarization sits in the middle. Approval gates wrap the tools they guard.
  • SummarizationMiddleware needs its own model for the summary calls; it does not have to be the agent's main model. Set the token trigger with headroom below the context limit.
  • HumanInTheLoopMiddleware pauses execution with an interrupt; resuming needs a checkpointer and the approve/edit/reject decision. Test the full interrupt-resume path, not just the happy path.
  • Custom behavior goes in an AgentMiddleware subclass with hooks: beforeagent, beforemodel, wrapmodelcall, wraptoolcall, aftermodel, afteragent.

Evidence

Workflow grounded in the official v1 release notes and middleware docs, verified 2026-09-26. v1: https://docs.langchain.com/oss/python/releases/langchain-v1 ; middleware: https://docs.langchain.com/oss/python/langchain/middleware/built-in

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_Q8HSqCaNgGbV73vO1MQ-sg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Compose a middleware stack: PII redaction, summarization, human approval <span>— Vectle</span></a>