Skip to main content

langgraph-reducer

This package provides:

  • 🧠 PrunableStateFactory: Auto-prunes message history without extra effort, works as a drop-in replacement for MessageState.
  • 🧹 MessagePrunerNode: A LangGraph node that summarizes or deletes messages when they exceed a count.
  • ⚠️ Important difference:
    • PrunableState runs on every node execution — better for lightweight, fast pruning.
    • MessagePrunerNode runs only where you add it — better for summarization or heavier logic (e.g., calling LLMs).

🚀 Install

pip install langgraph-reducer

🔁 PrunableState — Auto Cleanup

Use this when you just want to keep the latest N messages and forget the rest. No node logic needed — pruning is automatic.

from langraph_reducer import PrunableStateFactory

# Parameters:
# - min_messages: number of most recent messages to retain
# - max_messages: pruning is triggered once this threshold is exceeded

PrunableMessagesState = PrunableStateFactory.create_prunable_state(
    min_messages=10,
    max_messages=15
)

Use the PrunableMessagesState as the state type in your LangGraph:

from langgraph.graph import StateGraph

graph = StateGraph(PrunableMessagesState)

That’s it — pruning will be handled automatically during graph execution.


🧠 MessagePrunerNode — Summary or Delete

Use this when you want to summarize or remove old messages but need control over where in your graph this happens (e.g., before END).

from langchain_openai import ChatOpenAI
from langgraph_reducer import MessagePrunerNode

llm = ChatOpenAI(model="gpt-4o")

# model_func: Optional. If provided, node summarizes older messages.
#             If not provided, node simply deletes older messages.

def model_func(messages):
    return llm.invoke(messages)

pruner_node = MessagePrunerNode(
    min_messages=4,       # Retain at least 4 most recent messages
    max_messages=6,       # Trigger pruning if messages exceed 6
    model_func=model_func # Optional. Use LLM to summarize old messages.
)

Then wire it into your graph like a normal LangGraph node:

workflow.add_node("summarize_conversation", pruner_node)
workflow.add_edge("summarize_conversation", END)

Use a conditional edge to trigger the pruner only when needed.


🧩 Extending Prunable State

Want to add custom fields to the state? Just subclass the factory-created state:

from typing import Annotated
from langgraph.graph.message import add_messages

# Base state with auto-pruning
BaseState = PrunableStateFactory.create_prunable_state(10, 15)

# Extend it with your own fields
class MyState(BaseState):
    summary: str = ""
    profile_id: str = ""

Use MyState in your graph instead of the base one.

⚠️ Important:
Do not override or redefine the messages field when subclassing a prunable state.
The messages field is automatically managed for pruning and must retain its behavior.

If you override it, pruning logic will break silently.


📓 Examples

  • notebooks/basic_pruner_node.ipynb: shows how to integrate a summarizing node.
  • scripts/prunable_state_lambda.py: shows full pipeline with PrunableMessagesState in AWS Lambda.

Metadata

Release files for langgraph-reducer 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langgraph-reducer 0.0.2
File Size Uploaded Provenance
langgraph_reducer-0.0.2.tar.gz 7.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for langgraph-reducer 0.0.2
File Interpreter ABI Platform
langgraph_reducer-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 11.8 kB

Release files / langgraph_reducer-0.0.2.tar.gz

Download URL langgraph_reducer-0.0.2.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
7d627006948afabb036832ce1ce6af6c517b5b2307c535c5e9aad12ea6a151d3
BLAKE2b-256 checksum
How to use checksums
58e80d5eabd01cdbb9542389a282a674b2d39098899a539119441532bbad3718
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.16

Release files / langgraph_reducer-0.0.2-py3-none-any.whl

Download URL langgraph_reducer-0.0.2-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a1160d793cacade378df9ed991bd71c524365430483e510d317b97427ff2f45
BLAKE2b-256 checksum
How to use checksums
f70143f8ad12762d8068e56f5109375b3d6eb7c7d720448df088e2ffc724a607
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.16

Security for langgraph-reducer 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page