Skip to main content
Middleware provides a way to more tightly control what happens inside the agent. The core agent loop involves calling a model, letting it choose tools to execute, and then finishing when it calls no more tools:
Core agent loop diagram
Middleware exposes hooks before and after each of those steps:
Middleware flow diagram

What can middleware do?

Monitor

Track agent behavior with logging, analytics, and debugging

Modify

Transform prompts, tool selection, and output formatting

Control

Add retries, fallbacks, and early termination logic

Enforce

Apply rate limits, guardrails, and PII detection
Add middleware by passing them to @[create_agent]:
Refer to the docs below for a list of parameters and configuration details for each type of middleware.

Built-in middleware

LangChain provides prebuilt middleware for common use cases:

Summarization

Automatically summarize conversation history when approaching token limits.
Perfect for:
  • Long-running conversations that exceed context windows
  • Multi-turn dialogues with extensive history
  • Applications where preserving full conversation context matters
string | BaseChatModel
required
Model for generating summaries. Can be a model identifier string (e.g., 'openai:gpt-4o-mini') or a BaseChatModel instance.
number
Token threshold for triggering summarization
number
default:"20"
Recent messages to preserve
function
Custom token counting function. Defaults to character-based counting.
string
Custom prompt template. Uses built-in template if not specified.
string
default:"## Previous conversation summary:"
Prefix for summary messages

Human-in-the-loop

Pause agent execution for human approval, editing, or rejection of tool calls before they execute.
Perfect for:
  • High-stakes operations requiring human approval (database writes, financial transactions)
  • Compliance workflows where human oversight is mandatory
  • Long running conversations where human feedback is used to guide the agent
object
required
Mapping of tool names to approval configs
Tool approval config options:
boolean
default:"false"
Whether approval is allowed
boolean
default:"false"
Whether editing is allowed
boolean
default:"false"
Whether responding/rejection is allowed
Important: Human-in-the-loop middleware requires a checkpointer to maintain state across interruptions.See the human-in-the-loop documentation for complete examples and integration patterns.

Anthropic prompt caching

Reduce costs by caching repetitive prompt prefixes with Anthropic models.
Perfect for:
  • Applications with long, repeated system prompts
  • Agents that reuse the same context across invocations
  • Reducing API costs for high-volume deployments
Learn more about Anthropic Prompt Caching strategies and limitations.
string
default:"5m"
Time to live for cached content. Valid values: '5m' or '1h'

Model call limit

Limit the number of model calls to prevent infinite loops or excessive costs.
Perfect for:
  • Preventing runaway agents from making too many API calls
  • Enforcing cost controls on production deployments
  • Testing agent behavior within specific call budgets
number
Maximum model calls across all runs in a thread. Defaults to no limit.
number
Maximum model calls per single invocation. Defaults to no limit.
string
default:"end"
Behavior when limit is reached. Options: 'end' (graceful termination) or 'error' (throw exception)

Tool call limit

Control agent execution by limiting the number of tool calls, either globally across all tools or for specific tools.
Perfect for:
  • Preventing excessive calls to expensive external APIs
  • Limiting web searches or database queries
  • Enforcing rate limits on specific tool usage
  • Protecting against runaway agent loops
To limit tool calls globally across all tools or for specific tools, set toolName. For each limit, specify one or both of:
  • Thread limit (threadLimit) - Max calls across all runs in a conversation. Persists across invocations. Requires a checkpointer.
  • Run limit (runLimit) - Max calls per single invocation. Resets each turn.
Exit behaviors:
string
Name of specific tool to limit. If not provided, limits apply to all tools globally.
number
Maximum tool calls across all runs in a thread (conversation). Persists across multiple invocations with the same thread ID. Requires a checkpointer to maintain state. undefined means no thread limit.
number
Maximum tool calls per single invocation (one user message → response cycle). Resets with each new user message. undefined means no run limit.Note: At least one of threadLimit or runLimit must be specified.
string
default:"continue"
Behavior when limit is reached:
  • 'continue' (default) - Block exceeded tool calls with error messages, let other tools and the model continue. The model decides when to end based on the error messages.
  • 'error' - Throw a ToolCallLimitExceededError exception, stopping execution immediately
  • 'end' - Stop execution immediately with a ToolMessage and AI message for the exceeded tool call. Only works when limiting a single tool; throws error if other tools have pending calls.

Model fallback

Automatically fallback to alternative models when the primary model fails.
Perfect for:
  • Building resilient agents that handle model outages
  • Cost optimization by falling back to cheaper models
  • Provider redundancy across OpenAI, Anthropic, etc.
The middleware accepts a variable number of string arguments representing fallback models in order:
string[]
required
One or more fallback model strings to try in order when the primary model fails

PII detection

Detect and handle Personally Identifiable Information in conversations.
Perfect for:
  • Healthcare and financial applications with compliance requirements
  • Customer service agents that need to sanitize logs
  • Any application handling sensitive user data
string
required
Type of PII to detect. Can be a built-in type (email, credit_card, ip, mac_address, url) or a custom type name.
string
default:"redact"
How to handle detected PII. Options:
  • 'block' - Throw error when detected
  • 'redact' - Replace with [REDACTED_TYPE]
  • 'mask' - Partially mask (e.g., ****-****-****-1234)
  • 'hash' - Replace with deterministic hash
RegExp
Custom detector regex pattern. If not provided, uses built-in detector for the PII type.
boolean
default:"true"
Check user messages before model call
boolean
default:"false"
Check AI messages after model call
boolean
default:"false"
Check tool result messages after execution

To-do list

Equip agents with task planning and tracking capabilities for complex multi-step tasks.
Perfect for:
  • Complex multi-step tasks requiring coordination across multiple tools
  • Long-running operations where progress visibility is important
Just as humans are more effective when they write down and track tasks, agents benefit from structured task management to break down complex problems, adapt plans as new information emerges, and provide transparency into their workflow. You may have noticed patterns like this in Claude Code, which writes out a to-do list before tackling complex, multi-part tasks.
This middleware automatically provides agents with a write_todos tool and system prompts to guide effective task planning.
No configuration options available (uses defaults).

LLM tool selector

Use an LLM to intelligently select relevant tools before calling the main model.
Perfect for:
  • Agents with many tools (10+) where most aren’t relevant per query
  • Reducing token usage by filtering irrelevant tools
  • Improving model focus and accuracy
string | BaseChatModel
Model for tool selection. Can be a model identifier string (e.g., 'openai:gpt-4o-mini') or a BaseChatModel instance. Defaults to the agent’s main model.
number
Maximum number of tools to select. Defaults to no limit.
string[]
Array of tool names to always include in the selection

Context editing

Manage conversation context by trimming, summarizing, or clearing tool uses.
Perfect for:
  • Long conversations that need periodic context cleanup
  • Removing failed tool attempts from context
  • Custom context management strategies
ContextEdit[]
default:"[new ClearToolUsesEdit()]"
Array of ContextEdit strategies to apply
@[ClearToolUsesEdit] options:
number
default:"1000"
Token count that triggers the edit

Custom middleware

Build custom middleware by implementing hooks that run at specific points in the agent execution flow.

Class-based middleware

Two hook styles

Node-style hooks

Run sequentially at specific execution points. Use for logging, validation, and state updates.

Wrap-style hooks

Intercept execution with full control over handler calls. Use for retries, caching, and transformation.

Node-style hooks

Run at specific points in the execution flow:
  • beforeAgent - Before agent starts (once per invocation)
  • beforeModel - Before each model call
  • afterModel - After each model response
  • afterAgent - After agent completes (up to once per invocation)
Example: Logging middleware
Example: Conversation length limit

Wrap-style hooks

Intercept execution and control when the handler is called:
  • wrapModelCall - Around each model call
  • wrapToolCall - Around each tool call
You decide if the handler is called zero times (short-circuit), once (normal flow), or multiple times (retry logic). Example: Model retry middleware
Example: Dynamic model selection
Example: Tool call monitoring

Custom state schema

Middleware can extend the agent’s state with custom properties. Define a custom state type and set it as the state_schema:

Context extension

Context properties are configuration values passed through the runnable config. Unlike state, context is read-only and typically used for configuration that doesn’t change during execution. Middleware can define context requirements that must be satisfied through the agent’s configuration:

Execution order

When using multiple middleware, understanding execution order is important:
Before hooks run in order:
  1. middleware1.before_agent()
  2. middleware2.before_agent()
  3. middleware3.before_agent()
Agent loop starts
  1. middleware1.before_model()
  2. middleware2.before_model()
  3. middleware3.before_model()
Wrap hooks nest like function calls:
  1. middleware1.wrap_model_call()middleware2.wrap_model_call()middleware3.wrap_model_call() → model
After hooks run in reverse order:
  1. middleware3.after_model()
  2. middleware2.after_model()
  3. middleware1.after_model()
Agent loop ends
  1. middleware3.after_agent()
  2. middleware2.after_agent()
  3. middleware1.after_agent()
Key rules:
  • before_* hooks: First to last
  • after_* hooks: Last to first (reverse)
  • wrap_* hooks: Nested (first middleware wraps all others)

Agent jumps

To exit early from middleware, return a dictionary with jump_to:
Available jump targets:
  • 'end': Jump to the end of the agent execution
  • 'tools': Jump to the tools node
  • 'model': Jump to the model node (or the first before_model hook)
Important: When jumping from before_model or after_model, jumping to 'model' will cause all before_model middleware to run again. To enable jumping, decorate your hook with @hook_config(can_jump_to=[...]):

Best practices

  1. Keep middleware focused - each should do one thing well
  2. Handle errors gracefully - don’t let middleware errors crash the agent
  3. Use appropriate hook types:
    • Node-style for sequential logic (logging, validation)
    • Wrap-style for control flow (retry, fallback, caching)
  4. Clearly document any custom state properties
  5. Unit test middleware independently before integrating
  6. Consider execution order - place critical middleware first in the list
  7. Use built-in middleware when possible, don’t reinvent the wheel :)

Examples

Dynamically selecting tools

Select relevant tools at runtime to improve performance and accuracy.
Benefits:
  • Shorter prompts - Reduce complexity by exposing only relevant tools
  • Better accuracy - Models choose correctly from fewer options
  • Permission control - Dynamically filter tools based on user access

Additional resources


Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.