LangChain/Memory & Middleware
Intermediate14 min

Prebuilt Middleware Catalog

LangChain ships 14 production-ready middleware classes and Deep Agents adds 2 more. This article is organized around decisions: which ones your agent needs, how to order them, and what breaks when you get it wrong.

Quick Reference

  • ModelFallbackMiddleware('gpt-4.1-mini', 'claude-sonnet-4-6') — fallback chain on hard provider failure
  • PIIMiddleware('email', strategy='redact', apply_to_input=True, apply_to_tool_results=True) — redact PII from inputs AND tool responses
  • ToolRetryMiddleware(max_retries=3, tools=['api_tool'], retry_on=(ConnectionError, TimeoutError)) — retry specific flaky tools only
  • LLMToolSelectorMiddleware(model='gpt-4.1-mini', max_tools=3) — pre-filter tools when you have 10+
  • ModelCallLimitMiddleware(run_limit=10, thread_limit=50) — hard cap per run and per conversation
  • SummarizationMiddleware(model='gpt-4.1-mini') — compress old context when approaching token limits
  • SubAgentMiddleware(default_model='claude-sonnet-4-6', subagents=[...]) — delegate subtasks to isolated agents with their own context

Which Middleware Do I Actually Need?

The wrong answer is 'all of them.' Every middleware adds latency, a new failure mode, and debugging complexity. Start with zero. Add only what your agent demonstrably needs.

Your situationAdd this middleware
Agent calls more than one LLM providerModelFallbackMiddleware + ModelRetryMiddleware
Agent handles user-provided PII (email, credit card, SSN)PIIMiddleware with apply_to_tool_results=True
Agent exposes 10+ toolsLLMToolSelectorMiddleware
Agent runs long multi-turn conversationsSummarizationMiddleware (or ContextEditingMiddleware)
Agent performs complex multi-step planningTodoListMiddleware
Agent needs to run shell commandsShellToolMiddleware + DockerExecutionPolicy
Agent needs to search files in a directoryFilesystemFileSearchMiddleware
Agent delegates subtasks to specialized subagentsSubAgentMiddleware (Deep Agents)
Always add ModelCallLimitMiddleware + ToolCallLimitMiddleware

These two are cheap to add and prevent runaway spend. Every agent should have them, regardless of what else you stack.

Real project

A team building a code review agent added 11 middleware on day one — every prebuilt option that seemed relevant. When the agent started producing inconsistent output, debugging which middleware was transforming the context took two days. They stripped it back to 3 middleware and added each subsequent one only when a specific production problem required it.

Learn this in → Start minimal. Add middleware when you have evidence the behavior is needed, not when you have a feeling it might help.