Middleware
Middleware hooks into an agent before, after, or around model and tool calls. Use it for cross-cutting behavior such as logging, limits, redaction, retries, fallbacks, and human approval.
Tiny example
Section titled “Tiny example”from langchain.agents import create_agentfrom langchain.agents.middleware import ToolCallLimitMiddleware
agent = create_agent( model="openai:gpt-5.4-mini", tools=[search_orders], middleware=[ ToolCallLimitMiddleware(tool_name="search_orders", run_limit=3), ],)Hook mental model
Section titled “Hook mental model”| Hook style | Use it for |
|---|---|
before_* |
Validate or add state before a step |
after_* |
Inspect a result or record state |
wrap_* |
Control whether the wrapped call runs zero, one, or several times |
Middleware hooks run inside the compiled LangGraph returned by create_agent(); middleware is not a second runtime.
Failure note
Section titled “Failure note”Order matters when middleware wraps other middleware. Retrying outside a handler that performs a side effect can repeat that side effect. Test the actual order and keep authorization in the tool or service that owns the data.