GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Tell us in our annual survey
  • Docs by LangChain home page
  • Ask AI
  • GitHub
  • Try LangSmith
  • Overview
  • Managed Deep Agents
  • Deep Agents
  • LangChain
  • LangGraph
  • OpenWiki
  • Integrations
  • Learn
  • Reference

This view

  • Original page
  • New address

GeezerVison is showing docs.langchain.com — Middleware overview - Docs by LangChain

Middleware provides a way to more tightly control what happens inside the agent. Middleware is useful for the following:
  • Tracking agent behavior with logging, analytics, and debugging.
  • Transforming prompts, tool selection, and output formatting.
  • Adding retries, fallbacks, and early termination logic.
  • Applying rate limits, guardrails, and PII detection.
Add middleware by passing them to create_agent:
from langchain.agents import create_agent
from langchain.agents.middleware import SummarizationMiddleware, HumanInTheLoopMiddleware

agent = create_agent(
    model="gpt-6.1-sol",
    tools=[...],
    middleware=[
        SummarizationMiddleware(...),
        HumanInTheLoopMiddleware(...)
    ],
)

The agent loop

The core agent loop involves calling a model, letting it choose tools to execute, and then finishing when it calls no more tools: Core agent loop diagram Middleware exposes hooks before and after each of those steps: Middleware flow diagram

Use middleware inside a LangGraph workflow

Middleware is not a separate runtime: hooks run inside the compiled LangGraph that create_agent returns. You can drop the whole agent (middleware and all) into a larger StateGraph as a node or subgraph, and every middleware hook continues to run. Reach for this pattern when the surrounding topology is more than a standard “loop until done”: classifying input before routing to one of several agents, fanning out work in parallel, or stitching agent calls together with deterministic steps. HumanInTheLoopMiddleware matches against each tool’s .name. @tool-decorated functions take their name from the function, so the key below is "send_email".
from langchain.agents import AgentState, create_agent
from langchain.agents.middleware import HumanInTheLoopMiddleware
from langgraph.graph import START, StateGraph

# Assumes read_email, send_email, classify_node, and route are defined elsewhere.
email_agent = create_agent(
    model="claude-sonnet-4-6",
    tools=[read_email, send_email],
    middleware=[HumanInTheLoopMiddleware(interrupt_on={"send_email": True})],
)

graph = (
    StateGraph(AgentState)
    .add_node("classify", classify_node)
    .add_node("email_agent", email_agent)
    .add_edge(START, "classify")
    .add_conditional_edges("classify", route)
    .compile()
)
The HITL interrupt, summarization, PII redaction, retries, and any custom hooks all travel with the agent node. See Use subgraphs for the full set of composition patterns, including subgraph checkpointer scoping (per-invocation versus per-thread).

Additional resources

Built-in middleware

Explore built-in middleware for common use cases.

Custom middleware

Build your own middleware with hooks and decorators.

Middleware API reference

Complete API reference for middleware.

Middleware integrations

Provider-specific middleware for Anthropic, AWS, OpenAI, and more.

Testing agents

Test your agents with LangSmith.

Connect these docs to your agent of choice via MCP for real-time answers.
Edit this page on GitHub or file an issue.

Was this page helpful?

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.