LangChain/Memory & Middleware
Intermediate12 min

Prebuilt Middleware Catalog

LangChain and Deep Agents ship 15+ production-ready middleware for reliability, cost control, security, and agentic capabilities. Use them individually or stacked to cover cross-cutting concerns without touching your agent's core logic.

Quick Reference

  • ModelFallbackMiddleware('gpt-4.1-mini', 'claude-sonnet-4-6') — auto-fallback on provider failure
  • PIIMiddleware('email', strategy='redact', apply_to_input=True) — sanitize user data
  • ToolRetryMiddleware(max_retries=3, retry_on=(ConnectionError, TimeoutError)) — resilient tools
  • LLMToolSelectorMiddleware(max_tools=3) — pre-filter tools for large tool sets
  • TodoListMiddleware() — add write_todos tool + planning system prompt

All Prebuilt Middleware

MiddlewarePackageWhat It Does
SummarizationMiddlewarelangchainCompress old messages when approaching token limits
HumanInTheLoopMiddlewarelangchainPause for human approval before specific tool calls
ModelCallLimitMiddlewarelangchainCap model calls per run or thread to control costs
ToolCallLimitMiddlewarelangchainCap tool calls globally or per-tool
ModelFallbackMiddlewarelangchainAuto-fallback to backup models on provider failure
ModelRetryMiddlewarelangchainRetry failed model calls with exponential backoff
ToolRetryMiddlewarelangchainRetry failed tool calls with exponential backoff
PIIMiddlewarelangchainDetect and redact/mask/block PII in messages
LLMToolSelectorMiddlewarelangchainUse an LLM to pre-filter relevant tools
LLMToolEmulatorlangchainReplace real tool calls with LLM-generated responses (testing)
ContextEditingMiddlewarelangchainClear old tool outputs when token limits are reached
TodoListMiddlewarelangchainAdd task planning + write_todos tool
ShellToolMiddlewarelangchainExpose a persistent shell session to the agent
FilesystemFileSearchMiddlewarelangchainAdd Glob and Grep search tools over a directory
FilesystemMiddlewaredeepagentsls / read_file / write_file / edit_file for context engineering
SubAgentMiddlewaredeepagentsSpawn task subagents to isolate context