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-4o",
    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


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