> ## Documentation Index
> Fetch the complete documentation index at: https://docs.langchain.com/llms.txt
> Use this file to discover all available pages before exploring further.

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

Add middleware by passing them to `createAgent`:

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import {
  createAgent,
  summarizationMiddleware,
  humanInTheLoopMiddleware,
} from "langchain";

const agent = createAgent({
  model: "gpt-5.5",
  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](https://mintcdn.com/langchain-5e9cc07a/Tazq8zGc0yYUYrDl/oss/images/core_agent_loop.png?fit=max\&auto=format\&n=Tazq8zGc0yYUYrDl\&q=85\&s=ac72e48317a9ced68fd1be64e89ec063)

Middleware exposes hooks before and after each of those steps:

![Middleware flow diagram](https://mintcdn.com/langchain-5e9cc07a/RAP6mjwE5G00xYsA/oss/images/middleware_final.png?fit=max\&auto=format\&n=RAP6mjwE5G00xYsA\&q=85\&s=eb4404b137edec6f6f0c8ccb8323eaf1)

## Use middleware inside a LangGraph workflow

Middleware is not a separate runtime: hooks run inside the compiled [LangGraph](https://docs.langchain.com/oss/javascript/langgraph/overview.md) that [`create_agent`](https://reference.langchain.com/javascript/langchain/index/createAgent) returns. You can drop the whole agent (middleware and all) into a larger [StateGraph](https://reference.langchain.com/javascript/langchain-langgraph/index/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`.

The key matches the `name` you pass to `tool({...}, { name })`.

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { AgentState, createAgent, humanInTheLoopMiddleware } from "langchain";
import { StateGraph, START } from "@langchain/langgraph";

// Assumes readEmail, sendEmail, classifyNode, and route are defined elsewhere.
// readEmail / sendEmail are registered with name: "read_email" / "send_email".
const emailAgent = createAgent({
  model: "claude-sonnet-4-6",
  tools: [readEmail, sendEmail],
  middleware: [humanInTheLoopMiddleware({ interruptOn: { send_email: true } })],
});

const graph = new StateGraph(AgentState)
  .addNode("classify", classifyNode)
  .addNode("emailAgent", emailAgent)
  .addEdge(START, "classify")
  .addConditionalEdges("classify", route)
  .compile();
```

The HITL interrupt, summarization, PII redaction, retries, and any custom hooks all travel with the agent node. See [Use subgraphs](https://docs.langchain.com/oss/javascript/langgraph/use-subgraphs.md) for the full set of composition patterns, including subgraph checkpointer scoping (per-invocation versus per-thread).

## Additional resources

> **[Built-in middleware](https://docs.langchain.com/oss/javascript/langchain/middleware/built-in.md)**
>
> Explore built-in middleware for common use cases.

> **[Custom middleware](https://docs.langchain.com/oss/javascript/langchain/middleware/custom.md)**
>
> Build your own middleware with hooks and decorators.

> **[Middleware API reference](https://reference.langchain.com/python/langchain/middleware/)**
>
> Complete API reference for middleware.

> **[Middleware integrations](https://docs.langchain.com/oss/javascript/integrations/middleware.md)**
>
> Provider-specific middleware for Anthropic, AWS, OpenAI, and more.

> **[Testing agents](https://docs.langchain.com/oss/javascript/langchain/test.md)**
>
> Test your agents with LangSmith.

***

[Connect these docs](https://docs.langchain.com/use-these-docs.md) to your agent of choice via MCP for real-time answers.

[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).
