44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341 | class ContextManagerTool(Node):
"""
A tool that generates a conversation summary.
When called by the agent, this tool:
1. Generates a summary of the conversation using its own LLM
2. Returns the summary as tool result
3. Agent then decides how to apply the summary.
The tool doesn't modify the agent's state - it just generates and returns the summary.
Attributes:
group (Literal[NodeGroup.TOOLS]): The group this node belongs to.
name (str): The name of the tool.
description (str): Tool description with usage warning.
error_handling (ErrorHandling): Configuration for error handling.
llm (Node): LLM instance for generating summaries.
"""
group: Literal[NodeGroup.TOOLS] = NodeGroup.TOOLS
name: str = "Context Manager Tool"
description: str = (
"Generates a conversation summary to help manage context.\n\n"
"WARNING: This tool will trigger context compression. Before calling it,\n"
"save any necessary information because previous messages will be removed.\n"
)
error_handling: ErrorHandling = Field(default_factory=lambda: ErrorHandling(timeout_seconds=3600))
token_budget_ratio: float = Field(default=0.75, gt=0, lt=1)
llm: Node = Field(..., description="LLM instance for generating summaries")
model_config = ConfigDict(arbitrary_types_allowed=True)
input_schema: ClassVar[type[ContextManagerInputSchema]] = ContextManagerInputSchema
def init_components(self, connection_manager: ConnectionManager | None = None) -> None:
"""Initialize components for the tool."""
connection_manager = connection_manager or ConnectionManager()
super().init_components(connection_manager)
# Initialize the LLM if it is a postponed component
if self.llm.is_postponed_component_init:
self.llm.init_components(connection_manager)
def reset_run_state(self):
"""Reset the intermediate steps (run_depends) of the node."""
self._run_depends = []
@property
def to_dict_exclude_params(self) -> dict:
"""
Property to define which parameters should be excluded when converting the class instance to a dictionary.
Returns:
dict: A dictionary defining the parameters to exclude.
"""
return super().to_dict_exclude_params | {"llm": True}
def to_dict(self, **kwargs) -> dict:
"""Converts the instance to a dictionary.
Returns:
dict: A dictionary representation of the instance.
"""
data = super().to_dict(**kwargs)
data["llm"] = self.llm.to_dict(**kwargs)
return data
def _get_token_budget(self) -> int:
"""Return the max input tokens available for conversation messages per chunk.
Subtracts estimated prompt overhead (summarization/merge instructions)
from the ratio-based budget so that chunk + prompt always fits within
the LLM context window.
"""
raw_budget = int(self.llm.get_token_limit() * self.token_budget_ratio)
prompt_overhead = self._count_message_tokens(
[
Message(content=HISTORY_SUMMARIZATION_PROMPT_REPLACE, role=MessageRole.USER),
]
)
return max(raw_budget - prompt_overhead, 1)
def _count_message_tokens(self, messages: list[Message | VisionMessage]) -> int:
"""Count tokens for a list of messages using the summarization LLM's tokenizer."""
return token_counter(
model=self.llm.model,
messages=[m.model_dump(exclude={"metadata"}) for m in messages],
)
_TRUNCATION_MARKER = "\n\n[... truncated due to context limit ...]"
def _truncate_message(self, msg: Message | VisionMessage, max_tokens: int) -> Message | VisionMessage:
"""Truncate a single message so it fits within *max_tokens*.
Uses a ratio-based character estimate with token-count verification.
Falls back to halving the cut point up to 3 times if still over budget.
VisionMessages are returned as-is (image content can't be meaningfully truncated).
"""
if isinstance(msg, VisionMessage):
return msg
msg_tokens = self._count_message_tokens([msg])
if msg_tokens <= max_tokens:
return msg
content = msg.content
ratio = max_tokens / msg_tokens
cut_point = max(1, int(len(content) * ratio * 0.9))
for _ in range(3):
candidate = msg.model_copy(update={"content": content[:cut_point] + self._TRUNCATION_MARKER})
if self._count_message_tokens([candidate]) <= max_tokens:
break
cut_point = cut_point // 2
logger.warning(
f"Context Manager Tool: Truncated oversized message "
f"({msg_tokens} tokens > {max_tokens} token limit). Kept {cut_point}/{len(content)} chars."
)
return candidate
def _split_messages_into_chunks(
self,
messages: list[Message | VisionMessage],
budget: int,
) -> list[list[Message | VisionMessage]]:
"""Split messages into chunks that each fit within *budget* tokens.
Messages are kept in order. A single message that exceeds the budget
is truncated to fit.
"""
chunks: list[list[Message | VisionMessage]] = []
current_chunk: list[Message | VisionMessage] = []
current_tokens = 0
for msg in messages:
msg_tokens = self._count_message_tokens([msg])
if msg_tokens > budget:
if current_chunk:
chunks.append(current_chunk)
current_chunk = []
current_tokens = 0
truncated = self._truncate_message(msg, budget)
chunks.append([truncated])
continue
if current_chunk and current_tokens + msg_tokens > budget:
chunks.append(current_chunk)
current_chunk = [msg]
current_tokens = msg_tokens
else:
current_chunk.append(msg)
current_tokens += msg_tokens
if current_chunk:
chunks.append(current_chunk)
return chunks
def _call_llm_for_summary(
self,
messages: list[Message | VisionMessage],
config: RunnableConfig | None = None,
**kwargs,
) -> str:
"""Send *messages* to the LLM and return the generated text."""
llm_result = self.llm.run(
input_data={},
prompt=Prompt(messages=messages),
config=config,
**(kwargs | {"parent_run_id": kwargs.get("run_id"), "run_depends": []}),
)
self._run_depends = [NodeDependency(node=self.llm).to_dict(for_tracing=True)]
if llm_result.status != RunnableStatus.SUCCESS:
error_msg = llm_result.error.message if llm_result.error else "Unknown error"
raise ValueError(f"Context Manager Tool: LLM failed to generate summary: {error_msg}")
summary = llm_result.output.get("content", "")
if not summary:
raise ValueError("Context Manager Tool: LLM returned empty summary.")
return summary
def _summarize_replace_history(
self,
messages: list[Message | VisionMessage],
config: RunnableConfig | None = None,
**kwargs,
) -> str:
"""Generate a complete summary of the conversation (replace mode).
When the conversation history exceeds the LLM's context window, the
messages are split into chunks that fit, each chunk is summarised
independently, and the per-chunk summaries are merged into one final
summary. If the merged text is still too large, a second merge pass
is applied.
Args:
messages: List of messages to summarize.
config: Configuration for the run.
**kwargs: Additional parameters.
Returns:
str: The generated summary.
"""
budget = self._get_token_budget()
message_tokens = self._count_message_tokens(messages)
if message_tokens <= budget:
logger.info("Context Manager Tool: History fits in context, single-pass summary.")
summary_messages = messages + [
Message(content=HISTORY_SUMMARIZATION_PROMPT_REPLACE, role=MessageRole.USER, static=True),
]
summary = self._call_llm_for_summary(summary_messages, config, **kwargs)
logger.info(f"Context Manager Tool: Summary generated. Length: {len(summary)}")
return summary
chunks = self._split_messages_into_chunks(messages, budget)
logger.info(
f"Context Manager Tool: History ({message_tokens} tokens) exceeds budget "
f"({budget} tokens). Splitting into {len(chunks)} chunks."
)
chunk_summaries: list[str] = []
for idx, chunk in enumerate(chunks):
logger.info(f"Context Manager Tool: Summarizing chunk {idx + 1}/{len(chunks)}.")
chunk_messages = chunk + [
Message(content=HISTORY_SUMMARIZATION_PROMPT_REPLACE, role=MessageRole.USER, static=True),
]
chunk_summaries.append(self._call_llm_for_summary(chunk_messages, config, **kwargs))
combined = "\n\n---\n\n".join(chunk_summaries)
combined_messages = [
Message(content=combined, role=MessageRole.USER, static=True),
Message(content=MERGE_SUMMARIES_PROMPT, role=MessageRole.USER, static=True),
]
combined_tokens = self._count_message_tokens(combined_messages)
if combined_tokens > budget:
logger.info(
"Context Manager Tool: Merged summaries still exceed budget "
f"({combined_tokens} > {budget}). Running additional merge pass."
)
merge_chunks = self._split_messages_into_chunks(
[Message(content=s, role=MessageRole.USER, static=True) for s in chunk_summaries],
budget,
)
re_summaries: list[str] = []
for merge_chunk in merge_chunks:
merge_chunk.append(Message(content=MERGE_SUMMARIES_PROMPT, role=MessageRole.USER, static=True))
re_summaries.append(self._call_llm_for_summary(merge_chunk, config, **kwargs))
combined = "\n\n".join(re_summaries)
combined_messages = [
Message(content=combined, role=MessageRole.USER, static=True),
Message(content=MERGE_SUMMARIES_PROMPT, role=MessageRole.USER, static=True),
]
summary = self._call_llm_for_summary(combined_messages, config, **kwargs)
logger.info(f"Context Manager Tool: Chunked summary generated. Length: {len(summary)}")
return summary
def execute(
self, input_data: ContextManagerInputSchema, config: RunnableConfig | None = None, **kwargs
) -> dict[str, Any]:
"""
Generate conversation summary from provided messages.
Returns:
dict[str, Any]:
- content: Short acknowledgment for the agent observation.
- summary: The full generated summary used by ``_compact_history``.
"""
config = ensure_config(config)
self.reset_run_state()
self.run_on_node_execute_run(config.callbacks, **kwargs)
if not self.llm:
raise ValueError("Context Manager Tool: No LLM configured.")
if not input_data.messages:
raise ValueError("Context Manager Tool: No messages provided to summarize.")
logger.info(f"Context Manager Tool: Generating summary for {len(input_data.messages)} messages.")
summary_result = self._summarize_replace_history(
input_data.messages,
config,
**kwargs,
)
if input_data.notes:
summary_result = f"Notes: {input_data.notes}\n\n{summary_result}"
return {
"content": f"Conversation history was summarized successfully ({len(input_data.messages)} "
"messages compressed).",
"summary": summary_result,
}
|