Skip to content

Commit a2d02aa

Browse files
authored
updates for summarization triggers (#4377)
depends on langchain-ai/langchain#34576
1 parent ec0c519 commit a2d02aa

2 files changed

Lines changed: 45 additions & 10 deletions

File tree

pipeline/preprocessors/link_map.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class LinkMap(TypedDict):
104104
"StateFileSearchMiddleware": "langchain-anthropic/middleware/file_search/StateFileSearchMiddleware",
105105
"OpenAIModerationMiddleware": "langchain-openai/middleware/openai_moderation/OpenAIModerationMiddleware",
106106
"ContextSize": "langchain/agents/middleware/summarization/ContextSize",
107+
"TriggerClause": "langchain/agents/middleware/summarization/TriggerClause",
107108
# Messages
108109
"AIMessage": "langchain-core/messages/ai/AIMessage",
109110
"AIMessage.tool_calls": "langchain/messages/#langchain.messages.AIMessage.tool_calls",

src/oss/langchain/middleware/built-in.mdx

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,24 @@ const agent = createAgent({
125125
Model for generating summaries. Can be a model identifier string (e.g., `'openai:gpt-5.4-mini'`) or a `BaseChatModel` instance. See @[`init_chat_model`][init_chat_model(model)] for more information.
126126
</ParamField>
127127

128-
<ParamField body="trigger" type="ContextSize | list[ContextSize] | None">
128+
<ParamField body="trigger" type="ContextSize | TriggerClause | list[ContextSize | TriggerClause] | None">
129129
Condition(s) for triggering summarization. Can be:
130130

131-
- A single @[`ContextSize`] tuple (specified condition must be met)
132-
- A list of @[`ContextSize`] tuples (any condition must be met - OR logic)
131+
- A single @[`ContextSize`] tuple (the specified threshold must be met)
132+
- A single @[`TriggerClause`] dict (all specified thresholds must be met - AND logic)
133+
- A list mixing either form (any item must be met - OR logic)
133134

134-
Condition should be one of the following:
135+
Supported thresholds are:
135136

136137
- `fraction` (float): Fraction of model's context size (0-1)
137138
- `tokens` (int): Absolute token count
138139
- `messages` (int): Message count
139140

140-
At least one condition must be specified. If not provided, summarization will not trigger automatically.
141+
A @[`ContextSize`] tuple expresses exactly one threshold. A @[`TriggerClause`] dict can include one or more thresholds, e.g. `{"tokens": 4000, "messages": 10}`, and all thresholds in the dict must be met (AND).
141142

142-
See the API reference for @[`ContextSize`] for more information.
143+
Each @[`TriggerClause`] dict must specify at least one threshold. If `trigger` is not provided, summarization will not trigger automatically.
144+
145+
See the API reference for @[`ContextSize`] and @[`TriggerClause`] for more information.
143146
</ParamField>
144147

145148
<ParamField body="keep" type="ContextSize" default="('messages', 20)">
@@ -249,9 +252,10 @@ const agent = createAgent({
249252
The summarization middleware monitors message token counts and automatically summarizes older messages when thresholds are reached.
250253

251254
**Trigger conditions** control when summarization runs:
252-
- Single condition object (specified must be met)
253-
- Array of conditions (any condition must be met - OR logic)
254-
- Each condition can use `fraction` (of model's context size), `tokens` (absolute count), or `messages` (message count)
255+
- A single threshold triggers when that threshold is met
256+
- A trigger clause with multiple thresholds triggers only when all thresholds are met (AND logic)
257+
- A list of trigger conditions triggers when any item is met (OR logic)
258+
- Each threshold can use `fraction` (of model's context size), `tokens` (absolute count), or `messages` (message count)
255259

256260
**Keep condition** control how much context to preserve (specify exactly one):
257261
- `fraction` - Fraction of model's context size to keep
@@ -293,8 +297,38 @@ agent2 = create_agent(
293297
],
294298
)
295299

296-
# Using fractional limits
300+
# AND logic: trigger only when tokens >= 4000 AND messages >= 10
297301
agent3 = create_agent(
302+
model="gpt-5.4",
303+
tools=[your_weather_tool, your_calculator_tool],
304+
middleware=[
305+
SummarizationMiddleware(
306+
model="gpt-5.4-mini",
307+
trigger={"tokens": 4000, "messages": 10},
308+
keep=("messages", 20),
309+
),
310+
],
311+
)
312+
313+
# Combine AND and OR: trigger if (tokens >= 5000 AND messages >= 3)
314+
# OR (tokens >= 3000 AND messages >= 6)
315+
agent4 = create_agent(
316+
model="gpt-5.4",
317+
tools=[your_weather_tool, your_calculator_tool],
318+
middleware=[
319+
SummarizationMiddleware(
320+
model="gpt-5.4-mini",
321+
trigger=[
322+
{"tokens": 5000, "messages": 3},
323+
{"tokens": 3000, "messages": 6},
324+
],
325+
keep=("messages", 20),
326+
),
327+
],
328+
)
329+
330+
# Using fractional limits
331+
agent5 = create_agent(
298332
model="gpt-5.4",
299333
tools=[your_weather_tool, your_calculator_tool],
300334
middleware=[

0 commit comments

Comments
 (0)