langchain.js
    Preparing search index...

    Function contextEditingMiddleware

    • 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 ClearToolUsesEdit strategy which mirrors Anthropic's clear_tool_uses_20250919 behaviour by clearing older tool results once the conversation exceeds 100,000 tokens.

      Use the middleware with default settings to automatically manage context:

      Parameters

      Returns AgentMiddleware<undefined, undefined, any>

      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:

      • Triggers when context exceeds 100,000 tokens
      • Keeps the 3 most recent tool results
      • Uses approximate token counting (fast)
      • Does not clear tool call arguments

      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);
      }
      }