kmail.at
← learning

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.

2026-06-10 · 7 min read

$ pip install -U langchain>=1.3.5

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 thresholds
Conversation 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.

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

  1. 1Use compound triggers to require both token and message thresholds.
  2. 2Single-key triggers preserve the legacy OR behavior.
  3. 3Inspect context with get_context_messages() to verify what is summarized.

Related commands

← all learning