AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

ClearToolUsesEdit Does Not Persist Cleared State With Checkpointer, Causing Eviction Every Turn

ClearToolUsesEdit.apply mutates a deepcopy of messages inside ContextEditingMiddleware.wrap_model_call, so the cleared flag and placeholder content never reach the persistent checkpointer. Once the raw message list exceeds the trigger, the edit runs on every turn for the rest of the session.

highConfidence 75%LangchainAffected V1.3.1

Origin Analysis

The middleware applies ClearToolUsesEdit to a deep copy of request.messages and discards the copy after the model call. The cleared ToolMessages and their response_metadata.context_editing.cleared flags are not returned as state updates, so the checkpointer retains the original full-content messages. On the next turn, the raw state still contains all uncleared ToolMessages, the trigger remains true, and the same messages are re-cleared indefinitely.
Install langchain==1.3.1 and langchain-core==1.4.0. Create a persistent state list mimicking checkpointed messages. Instantiate ClearToolUsesEdit with trigger=10000, keep=5. Each turn append one AIMessage with tool call and one ToolMessage with 4000 characters. Run simulate_middleware_call which deepcopies state and calls edit.apply. Observe that edited messages are cleared but original state remains full; after turn 1, trigger always fires and cleared messages never accumulate flags in checkpoint state.

Fixing Code Block

Edge Case Audit

This fix relies on the agent runtime invoking after_model after the model call. If after_model is not called in a custom runtime or older LangChain version, the fix has no effect. RemoveMessage uses message IDs; ensure ToolMessage IDs are unique and stable across checkpoints. Concurrent turns or multi-threaded use may cause race conditions if the checkpointer is not transaction-safe. Rolling back to the default ContextEditingMiddleware will restore the original bug but does not lose data besides already-cleared tool contents (which can be re-fetched). Test with your specific checkpointer and agent topology before production.

Ecosystem Topology