Middleware
Middleware is LangChain 1.0's main extension point: hook into fixed stages of the agent loop so you don't rewrite the loop for every production concern.
Why middleware?
Common production needs:
- Summarization (context limits)
- Long-term memory injection
- Human-in-the-loop before sensitive tools
- Subagent delegation
- Dynamic prompts / model routing
- Step and tool limits
Hooks sit around model and tool nodes; multiple middleware pieces compose (onion model).
Hook types (conceptual)
Order: forward on the way in, reverse on the way out.
Example: step limit (sketch)
Exact base classes and signatures depend on your installed version—see official middleware docs.
Built-in & community middleware
Examples: human-in-the-loop, summarization, model fallback, PII filters, prompt caching. Check LangChain blog and GitHub for community lists.
Middleware vs LangGraph nodes
Use middleware for single-agent enhancements; LangGraph for orchestration patterns.
Subagents
Middleware can spawn subagents with isolated context and merge results—see Deep Agents / delegation middleware.
Debugging
Use LangSmith state diffs; add one middleware at a time; avoid heavy sync IO in hooks.