Skip to main content
Middleware provides a way to more tightly control what happens inside the agent. Middleware is useful for the following: 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-4.1",
    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

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.

Testing agents

Test your agents with LangSmith.

Connect these docs to Claude, VSCode, and more via MCP for real-time answers.