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 createAgent:
import {
  createAgent,
  summarizationMiddleware,
  humanInTheLoopMiddleware,
} from "langchain";

const agent = createAgent({
  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.