This page contains reference documentation for Middleware. See the docs for conceptual guides, tutorials, and examples on using Middleware.
LangChain provides prebuilt middleware for common agent use cases:
| CLASS | DESCRIPTION |
|---|---|
SummarizationMiddleware |
Automatically summarize conversation history when approaching token limits |
HumanInTheLoopMiddleware |
Pause execution for human approval of tool calls |
ModelCallLimitMiddleware |
Limit the number of model calls to prevent excessive costs |
ToolCallLimitMiddleware |
Control tool execution by limiting call counts |
ModelFallbackMiddleware |
Automatically fallback to alternative models when primary fails |
PIIMiddleware |
Detect and handle Personally Identifiable Information |
TodoListMiddleware |
Equip agents with task planning and tracking capabilities |
LLMToolSelectorMiddleware |
Use an LLM to select relevant tools before calling main model |
ToolRetryMiddleware |
Automatically retry failed tool calls with exponential backoff |
LLMToolEmulator |
Emulate tool execution using LLM for testing purposes |
ContextEditingMiddleware |
Manage conversation context by trimming or clearing tool uses |
ShellToolMiddleware |
Expose a persistent shell session to agents for command execution |
FilesystemFileSearchMiddleware |
Provide Glob and Grep search tools over filesystem files |
AgentMiddleware |
Base middleware class for creating custom middleware |
Create custom middleware using these decorators:
| DECORATOR | DESCRIPTION |
|---|---|
@before_agent |
Execute logic before agent execution starts |
@before_model |
Execute logic before each model call |
@after_model |
Execute logic after each model receives a response |
@after_agent |
Execute logic after agent execution completes |
@wrap_model_call |
Wrap and intercept model calls |
@wrap_tool_call |
Wrap and intercept tool calls |
@dynamic_prompt |
Generate dynamic system prompts based on request context |
@hook_config |
Configure hook behavior (e.g., conditional routing) |
Core types for building middleware:
| TYPE | DESCRIPTION |
|---|---|
AgentState |
State container for agent execution |
ModelRequest |
Request details passed to model calls |
ModelResponse |
Response details from model calls |
ClearToolUsesEdit |
Utility for clearing tool usage history from context |
InterruptOnConfig |
Configuration for human-in-the-loop interruptions |
SummarizationMiddleware types:
| TYPE | DESCRIPTION |
|---|---|
ContextSize |
Union type |
ContextFraction |
Summarize at fraction of total context |
ContextTokens |
Summarize at token threshold |
ContextMessages |
Summarize at message threshold |
Summarizes conversation history when token limits are approached.
Human in the loop middleware.
Tracks model call counts and enforces limits.
Track tool call counts and enforces limits during agent execution.
Automatic fallback to alternative models on errors.
Detect and handle Personally Identifiable Information (PII) in conversations.
Middleware that provides todo list management capabilities to agents.
Uses an LLM to select relevant tools before calling the main model.
Middleware that automatically retries failed tool calls with configurable backoff.
Emulates specified tools using an LLM instead of executing them.
Automatically prune tool results to manage context size.
Middleware that registers a persistent shell tool for agents.
Provides Glob and Grep search over filesystem files.
Base middleware class for an agent.
State schema for the agent.
Model request information for the agent.
Response from model execution including messages and optional structured output.
Configuration for clearing tool outputs when token limits are exceeded.
Configuration for an action requiring human in the loop.
Decorator used to dynamically create a middleware with the before_agent hook.
Decorator used to dynamically create a middleware with the before_model hook.
Decorator used to dynamically create a middleware with the after_model hook.
Decorator used to dynamically create a middleware with the after_agent hook.
Create middleware with wrap_model_call hook from a function.
Create middleware with wrap_tool_call hook from a function.
Decorator used to dynamically generate system prompts for the model.
Decorator to configure hook behavior in middleware methods.