class ClearToolUsesEditStrategy for clearing tool outputs when token limits are exceeded.
This strategy mirrors Anthropic's clear_tool_uses_20250919 behavior by
replacing older tool results with a placeholder text when the conversation
grows too large. It preserves the most recent tool results and can exclude
specific tools from being cleared.
Apply an edit to the message list, returning the new token count.
This method should:
tokens parametermessages array in-place (if needed)import { ClearToolUsesEdit } from "langchain";
const edit = new ClearToolUsesEdit({
trigger: { tokens: 100000 }, // Start clearing at 100K tokens
keep: { messages: 3 }, // Keep 3 most recent tool results
excludeTools: ["important"], // Never clear "important" tool
clearToolInputs: false, // Keep tool call arguments
placeholder: "[cleared]", // Replacement text
});
// Multiple trigger conditions
const edit2 = new ClearToolUsesEdit({
trigger: [
{ tokens: 100000, messages: 50 },
{ tokens: 50000, messages: 100 }
],
keep: { messages: 3 },
});
// Fractional trigger with model profile
const edit3 = 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
});