Spokes.wiki Search About
Tech Article source ↗ source url updated Thu Jun 18 2026 00:00:00 GMT+0000 (Coordinated Universal Time)

LangChain middleware docs

The official LangChain middleware reference — the first-party definition of agent-middleware as LangChain frames it. Middleware is “a way to more tightly control what happens inside the agent”: tracking behaviour, transforming prompts and outputs, adding retries/fallbacks, and applying rate limits or guardrails. It runs inside the compiled LangGraph that create_agent returns, and is passed as a list, so middleware stack in order.

The named hook points

The custom-middleware page names the exact hooks an author implements:

  • before_agent — once per invocation, before the agent starts.
  • before_model — before each model call.
  • after_model — after each model response.
  • wrap_model_call — around each model call (nested-function style).
  • wrap_tool_call — around each tool call.
  • after_agent — once per invocation, after the agent completes.

Ordering is defined: before_* hooks run first-to-last, after_* hooks run last-to-first (reverse), and wrap_* hooks nest, with the first middleware wrapping all the others. Hooks come in decorator form (@before_model) or as methods on an AgentMiddleware subclass.

Built-in middleware

The docs ship middleware by concern: LLM tool selector, tool retry, model fallback, model-call limit, PII detection, SummarizationMiddleware, and HumanInTheLoopMiddleware — the named pieces that map onto threads this wiki tracks separately (agent-orchestration, durable-agents, guardrails).

Tier T1 — vendor’s own documentation. Cited by agent-middleware.