> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-5e9cc07a-preview-update-1770920925-e15dbde.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Overview

> Control and customize agent execution at every step

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](/oss/javascript/langchain/middleware/built-in#llm-tool-selector), and output formatting.
* Adding [retries](/oss/javascript/langchain/middleware/built-in#tool-retry), [fallbacks](/oss/javascript/langchain/middleware/built-in#model-fallback), and early termination logic.
* Applying [rate limits](/oss/javascript/langchain/middleware/built-in#model-call-limit), guardrails, and [PII detection](/oss/javascript/langchain/middleware/built-in#pii-detection).

Add middleware by passing them to `createAgent`:

```typescript theme={null}
import {
  createAgent,
  summarizationMiddleware,
  humanInTheLoopMiddleware,
} from "langchain";

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

<img src="https://mintcdn.com/langchain-5e9cc07a-preview-update-1770920925-e15dbde/ksGO-J7Uu33BnUg4/oss/images/core_agent_loop.png?fit=max&auto=format&n=ksGO-J7Uu33BnUg4&q=85&s=6a1f021e199b10e309f273be4ba3b313" alt="Core agent loop diagram" style={{height: "200px", width: "auto", justifyContent: "center"}} className="rounded-lg block mx-auto" width="300" height="268" data-path="oss/images/core_agent_loop.png" />

Middleware exposes hooks before and after each of those steps:

<img src="https://mintcdn.com/langchain-5e9cc07a-preview-update-1770920925-e15dbde/REjDJZ2KMMLsAzwv/oss/images/middleware_final.png?fit=max&auto=format&n=REjDJZ2KMMLsAzwv&q=85&s=48d1bb177c25acdf8584bfaf80feda69" alt="Middleware flow diagram" style={{height: "300px", width: "auto", justifyContent: "center"}} className="rounded-lg mx-auto" width="500" height="560" data-path="oss/images/middleware_final.png" />

## Additional resources

<CardGroup cols={2}>
  <Card title="Built-in middleware" icon="box" href="/oss/javascript/langchain/middleware/built-in">
    Explore built-in middleware for common use cases.
  </Card>

  <Card title="Custom middleware" icon="code" href="/oss/javascript/langchain/middleware/custom">
    Build your own middleware with hooks and decorators.
  </Card>

  <Card title="Middleware API reference" icon="book" href="https://reference.langchain.com/python/langchain/middleware/">
    Complete API reference for middleware.
  </Card>

  <Card title="Testing agents" icon="scale-unbalanced" href="/oss/javascript/langchain/test">
    Test your agents with LangSmith.
  </Card>
</CardGroup>

***

<Callout icon="pen-to-square" iconType="regular">
  [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/langchain/middleware/overview.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
</Callout>

<Tip icon="terminal" iconType="regular">
  [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
</Tip>
