API Reference¶
MessageReducer¶
| Param | Default | Description |
|---|---|---|
min_messages |
0 |
Messages to retain after pruning (message-count mode) |
max_messages |
None |
Threshold to trigger pruning (None = never prune in count mode) |
config |
None |
A ReducerConfig. When provided, it overrides min_messages/max_messages |
If config is omitted, a ReducerConfig is built from min_messages/max_messages.
Methods¶
reduce(existing=None, new=None, namespace=None) -> ReducerResult¶
Concatenates existing + new, then prunes if over threshold (count or token mode). namespace is an opaque value forwarded unchanged to every on_prune hook; the reducer never inspects it.
result = reducer.reduce(existing=history, new=[new_message])
result = reducer.reduce(existing=history, namespace=("memories", user_id)) # with on_prune hooks
as_langgraph_reducer() -> Callable¶
Returns a function with signature (existing, new) -> list for use with LangGraph's Annotated[list, fn] pattern. Returns only the surviving messages.
ReducerConfig¶
ReducerConfig(
min_messages=10,
max_messages=20,
max_tokens=None,
target_tokens=None,
token_counter=None,
preserve_first=True,
cascade_tool_messages=True,
summarize_fn=None,
inject_summary=False,
summary_message_factory=None,
on_prune=[],
namespace_key="memory_namespace",
dedupe_on_prune=True,
dedupe_window=10_000,
)
| Field | Default | Description |
|---|---|---|
min_messages |
10 |
Messages to retain after pruning (message-count mode) |
max_messages |
20 |
Threshold to trigger pruning (message-count mode) |
max_tokens |
None |
Token threshold to trigger pruning. When set, enables token mode (takes precedence over message-count mode) |
target_tokens |
None |
Prune down to at or below this token count. Defaults to max_tokens |
token_counter |
None |
Callable[[message], int]. When omitted: tiktoken if installed, else char heuristic |
preserve_first |
True |
Never prune index 0 (system message) |
cascade_tool_messages |
True |
Also prune ToolMessages linked to a pruned AIMessage |
summarize_fn |
None |
Callable[[list], str] called with pruned messages |
inject_summary |
False |
Insert the summary back into surviving in place of the pruned block (see Summarization) |
summary_message_factory |
None |
Callable[[str, int], list] building the injected summary block |
on_prune |
[] |
List of RememberFn — (pruned, namespace) -> Any — called after pruning (see Long-Term Memory Hooks) |
namespace_key |
"memory_namespace" |
Key that framework integrations read from per-call config to find the namespace to forward |
dedupe_on_prune |
True |
Deliver each message id to hooks at most once per reducer instance |
dedupe_window |
10000 |
Bounded memory of delivered ids |
RememberFn¶
The type of an on_prune hook. namespace is whatever the caller of reduce() forwarded, or None.
Background¶
Wraps a RememberFn so it runs on a bounded worker pool instead of inside the caller's reduce(). Copies the pruned list, drops (and counts in .dropped) when the backlog is full, swallows hook exceptions, drains at exit. close(wait=True) stops accepting work; usable as a context manager. See Long-Term Memory Hooks.
ReducerResult¶
Returned by reduce().
| Field | Type | Description |
|---|---|---|
surviving |
list |
Messages that remain after pruning |
pruned |
list |
Messages that were removed |
summary |
str \| None |
Summary from summarize_fn, if configured and pruning occurred |
resolve_token_counter¶
Returns the effective per-message token counter using the three-layer resolution: user callable → tiktoken → character heuristic. Exposed for advanced use (e.g. pre-computing a budget). Most users never call this directly — reduce() uses it internally in token mode.
Pruning rules (both modes)¶
- Only
ai/agent/assistantandhuman/usermessages are pruning candidates. system,tool, andfunctionmessages are never pruned directly.- Index 0 is preserved when
preserve_first=True. - When an
aimessage is pruned,toolmessages linked viatool_call_idare pruned too (cascade_tool_messages=True). - Whole messages only — content is never truncated.