Configuration options for the middleware
A middleware instance that can be used with createAgent
import { contextEditingMiddleware } from "langchain";
import { createAgent } from "langchain";
const agent = createAgent({
model: "anthropic:claude-sonnet-4-5",
tools: [searchTool, calculatorTool],
middleware: [
contextEditingMiddleware(),
],
});
The default configuration:
Customize the clearing behavior with ClearToolUsesEdit:
import { contextEditingMiddleware, ClearToolUsesEdit } from "langchain";
// Single condition: trigger if tokens >= 50000 AND messages >= 20
const agent1 = createAgent({
model: "anthropic:claude-sonnet-4-5",
tools: [searchTool, calculatorTool],
middleware: [
contextEditingMiddleware({
edits: [
new ClearToolUsesEdit({
trigger: { tokens: 50000, messages: 20 },
keep: { messages: 5 },
excludeTools: ["search"],
clearToolInputs: true,
}),
],
tokenCountMethod: "approx",
}),
],
});
// Multiple conditions: trigger if (tokens >= 50000 AND messages >= 20) OR (tokens >= 30000 AND messages >= 50)
const agent2 = createAgent({
model: "anthropic:claude-sonnet-4-5",
tools: [searchTool, calculatorTool],
middleware: [
contextEditingMiddleware({
edits: [
new ClearToolUsesEdit({
trigger: [
{ tokens: 50000, messages: 20 },
{ tokens: 30000, messages: 50 },
],
keep: { messages: 5 },
}),
],
}),
],
});
// Fractional trigger with model profile
const agent3 = createAgent({
model: chatModel,
tools: [searchTool, calculatorTool],
middleware: [
contextEditingMiddleware({
edits: [
new ClearToolUsesEdit({
trigger: { fraction: 0.8 }, // Trigger at 80% of model's max tokens
keep: { fraction: 0.3 }, // Keep 30% of model's max tokens
model: chatModel,
}),
],
}),
],
});
Implement your own context editing strategy by creating a class that
implements the ContextEdit interface:
import { contextEditingMiddleware, type ContextEdit, type TokenCounter } from "langchain";
import type { BaseMessage } from "@langchain/core/messages";
class CustomEdit implements ContextEdit {
async apply(params: {
tokens: number;
messages: BaseMessage[];
countTokens: TokenCounter;
}): Promise<number> {
// Implement your custom editing logic here
// and apply it to the messages array, then
// return the new token count after edits
return countTokens(messages);
}
}
Middleware that automatically prunes tool results to manage context size.
This middleware applies a sequence of edits when the total input token count exceeds configured thresholds. By default, it uses the
ClearToolUsesEditstrategy which mirrors Anthropic'sclear_tool_uses_20250919behaviour by clearing older tool results once the conversation exceeds 100,000 tokens.Basic Usage
Use the middleware with default settings to automatically manage context: