Middleware that trims the model’s input to a bounded number of recent messages (view-only).
- class langgraph_agent_toolkit.agents.components.middlewares.trim_messages.TrimMessagesMiddleware(max_messages=None)[source][source]
Bases:
AgentMiddlewareBound the model’s input to the last
max_messagesmessages, non-destructively.Ports the custom
create_react_agent’spre_model_hooktrimming to nativecreate_agent: each model call sees at mostmax_messagesrecent messages (trimmed to start on a human turn and to keep the system prompt), while the full history stays in state. Use it to bound text growth in long conversations —ContextEditingMiddlewareonly bounds tool output.max_messagesdefaults tosettings.DEFAULT_MAX_MESSAGE_HISTORY_LENGTHand can be overridden per agent. It counts messages (not tokens). Best for conversational / text-heavy agents; for a single turn that makes very many tool calls, prefer a larger value (or compose withSanitizeHistoryMiddleware) so the current turn’s tool results are not cut.- Parameters:
max_messages (int | None)
- __init__(max_messages=None)[source][source]
- Parameters:
max_messages (int | None)
- Return type:
None
- wrap_model_call(request, handler)[source][source]
Intercept and control model execution via handler callback.
Async version is awrap_model_call
The handler callback executes the model request and returns a ModelResponse. Middleware can call the handler multiple times for retry logic, skip calling it to short-circuit, or modify the request/response. Multiple middleware compose with first in list as outermost layer.
- Parameters:
request (ModelRequest) – Model request to execute (includes state and runtime).
handler (Callable[[ModelRequest], ModelResponse]) –
Callback that executes the model request and returns ModelResponse.
Call this to execute the model.
Can be called multiple times for retry logic.
Can skip calling it to short-circuit.
- Returns:
The model call result.
- Return type:
ModelResponse
Examples
!!! example “Retry on error”
!!! example “Rewrite response”
!!! example “Error to fallback”
!!! example “Cache/short-circuit”
!!! example “Simple AIMessage return (converted automatically)”
- async awrap_model_call(request, handler)[source][source]
Intercept and control async model execution via handler callback.
The handler callback executes the model request and returns a ModelResponse.
Middleware can call the handler multiple times for retry logic, skip calling it to short-circuit, or modify the request/response. Multiple middleware compose with first in list as outermost layer.
- Parameters:
request (ModelRequest) – Model request to execute (includes state and runtime).
handler (Callable[[ModelRequest], Awaitable[ModelResponse]]) –
Async callback that executes the model request and returns ModelResponse.
Call this to execute the model.
Can be called multiple times for retry logic.
Can skip calling it to short-circuit.
- Returns:
The model call result.
- Return type:
ModelResponse
Examples
!!! example “Retry on error”
- async awrap_tool_call(request, handler)[source]
Intercept and control async tool execution via handler callback.
The handler callback executes the tool call and returns a ToolMessage or Command. Middleware can call the handler multiple times for retry logic, skip calling it to short-circuit, or modify the request/response. Multiple middleware compose with first in list as outermost layer.
- Parameters:
request (ToolCallRequest) –
Tool call request with call dict, BaseTool, state, and runtime.
Access state via request.state and runtime via request.runtime.
handler (Callable[[ToolCallRequest], Awaitable[ToolMessage | Command[Any]]]) –
Async callable to execute the tool and returns ToolMessage or Command.
Call this to execute the tool.
Can be called multiple times for retry logic.
Can skip calling it to short-circuit.
- Returns:
ToolMessage or Command (the final result).
- Return type:
ToolMessage | Command[Any]
The handler Callable can be invoked multiple times for retry logic.
Each call to handler is independent and stateless.
Examples
!!! example “Async retry on error”
```python async def awrap_tool_call(self, request, handler):
- for attempt in range(3):
- try:
result = await handler(request) if is_valid(result):
return result
- except Exception:
- if attempt == 2:
raise
return result
```python async def awrap_tool_call(self, request, handler):
- if cached := await get_cache_async(request):
return ToolMessage(content=cached, tool_call_id=request.tool_call[“id”])
result = await handler(request) await save_cache_async(request, result) return result
- property name: str
The name of the middleware instance.
Defaults to the class name, but can be overridden for custom naming.
- transformers = ()
Stream transformer factories registered by the middleware.
Each entry is a scope-aware factory invoked as factory(scope) so every invocation receives a fresh instance. Factories are merged with the transformers argument of [create_agent][langchain.agents.create_agent] at graph compile time, after the ToolCallTransformer and before any user-supplied entries.
- wrap_tool_call(request, handler)[source]
Intercept tool execution for retries, monitoring, or modification.
Async version is awrap_tool_call
Multiple middleware compose automatically (first defined = outermost).
Exceptions propagate unless handle_tool_errors is configured on ToolNode.
- Parameters:
request (ToolCallRequest) –
Tool call request with call dict, BaseTool, state, and runtime.
Access state via request.state and runtime via request.runtime.
handler (Callable[[ToolCallRequest], ToolMessage | Command[Any]]) – Callable to execute the tool (can be called multiple times).
- Returns:
ToolMessage or Command (the final result).
- Return type:
ToolMessage | Command[Any]
The handler Callable can be invoked multiple times for retry logic.
Each call to handler is independent and stateless.
Examples
!!! example “Modify request before execution”
!!! example “Retry on error (call handler multiple times)”
!!! example “Conditional retry based on response”
- tools
Additional tools registered by the middleware.