langchain · difficulty ◆◆
SummarizationMiddleware: AND-Capable Triggers
Summarize only when the conversation is genuinely long in both dimensions.
Token bloat and message bloat are different problems. AND triggers let you summarize only when both are actually out of hand.
fully AI-generatedThis text is fully AI-generated and was published without human editorial review.$ pip install -U langchain>=1.3.5- langchain 1.3.5
- Release
- #34576
- PR
- OR → AND
- Paradigm
AND-capable triggers
What it does
SummarizationMiddleware in langchain-core intercepts LLM conversations and automatically triggers summarization when thresholds are hit. The new AND-capable trigger syntax lets you express compound conditions: {"tokens": 4000, "messages": 10} means summarize only when token count and message count both exceed their thresholds simultaneously. Previously, triggers were effectively OR-based, so any single threshold crossing would fire.
Why it matters
In production assistants, token bloat and message bloat behave differently. A single tool call returning a large log snippet can spike tokens without adding conversational depth. Many short follow-ups increase message count without much context cost. AND triggers distinguish these patterns, preserving recent context while keeping long-running chats manageable.
Example
$ AND trigger over both thresholdsConversation summarized.The middleware replaces message history with a summary once BOTH the token (100) and message (3) thresholds are exceeded.
Common flags
- {"tokens": 4000, "messages": 10}
- AND semantics: both thresholds must fire
- {"tokens": 100}
- OR semantics: a single threshold fires (legacy behavior)
History
Origin
AND-capable trigger conditions were ported in langchain==1.3.5 (2026-06-10), PR #34576, with docs in docs#4377.
Design shift
Moving from implicit OR to explicit AND gave users control over what "long enough to summarize" actually means for their workload.
Under the hood
Asymmetric thresholds
Set trigger={"tokens": 500, "messages": 2} to fire only when a single large message pushes tokens over 500 and at least 2 messages exist, giving you fine-grained control over noisy tool-heavy sessions.
Fun facts
Pros & cons
pros
- + Prevents premature summarization
- + Distinguishes token spikes from depth
- + Configurable per workload
cons
- − More configuration to reason about
- − Middleware order still matters
Takeaways
- 1Use compound triggers to require both token and message thresholds.
- 2Single-key triggers preserve the legacy OR behavior.
- 3Inspect context with get_context_messages() to verify what is summarized.
Related commands
archive entry — written and published by a scheduled job, never editorially reviewed. Kept for reference: out of the site’s search index, and no ads run here. the reviewed library →
