Skip to main content

Framework-agnostic message reducer for AI agent state management. Works with LangGraph, CrewAI, and plain dicts.

Project description

agentstate-reducer

Framework-agnostic message reducer for AI agent state management. Works with LangGraph, CrewAI, and plain dicts.

What It Does

Automatically prunes message history when it exceeds a threshold, keeping conversations manageable:

  • Windowed pruning: Trigger at max_messages, retain min_messages
  • System message preservation: Index 0 (system prompt) is never pruned
  • ToolMessage cascade: When an AI message is pruned, linked ToolMessages are pruned too
  • Optional summarization: Get a callback with pruned messages to generate summaries

Install

pip install agentstate-reducer

Zero dependencies. Works with Python 3.10+.

Quick Start

from agentstate_reducer import MessageReducer

reducer = MessageReducer(min_messages=10, max_messages=20)

# Works with plain dicts
result = reducer.reduce(
    existing=[
        {"role": "system", "content": "You are helpful"},
        {"role": "human", "content": "Hello"},
        {"role": "ai", "content": "Hi there!"},
    ],
    new=[{"role": "human", "content": "New message"}],
)
print(result.surviving)  # Messages that remain
print(result.pruned)     # Messages that were removed

# Works with LangChain BaseMessage objects too
from langchain_core.messages import HumanMessage, AIMessage
result = reducer.reduce(
    existing=[HumanMessage(content="Hello")],
    new=[AIMessage(content="Hi!")],
)

LangGraph Integration

Use with PrunableStateFactory or directly as a state annotation:

from agentstate_reducer import MessageReducer
from typing_extensions import Annotated, TypedDict

reducer = MessageReducer(min_messages=10, max_messages=20)

class MyState(TypedDict):
    messages: Annotated[list, reducer.as_langgraph_reducer()]

CrewAI Integration

Use with a CosmosDB persistence backend:

from agentstate_reducer import MessageReducer

reducer = MessageReducer(min_messages=10, max_messages=20)
# Pass to your persistence class which calls reducer.reduce()
# on save_state() to prune before storing

Summarization

from agentstate_reducer import MessageReducer, ReducerConfig

def summarize(pruned_messages):
    # Call your LLM to summarize what was removed
    return f"Summary of {len(pruned_messages)} messages"

config = ReducerConfig(
    min_messages=10,
    max_messages=20,
    summarize_fn=summarize,
)
reducer = MessageReducer(config=config)
result = reducer.reduce(existing=messages)
print(result.summary)  # "Summary of 5 messages"

API

MessageReducer(min_messages, max_messages, *, config)

Param Default Description
min_messages 0 Messages to retain after pruning
max_messages None Threshold to trigger pruning (None = never prune)
config None ReducerConfig object (overrides other params)

reducer.reduce(existing, new) -> ReducerResult

Field Type Description
surviving list Messages that remain
pruned list Messages that were removed
summary str | None Summary from summarize_fn if configured

reducer.as_langgraph_reducer() -> Callable

Returns a function with signature (existing, new) -> list for use with LangGraph's Annotated[list, fn] pattern.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agentstate_reducer-0.1.0.tar.gz (92.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agentstate_reducer-0.1.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file agentstate_reducer-0.1.0.tar.gz.

File metadata

  • Download URL: agentstate_reducer-0.1.0.tar.gz
  • Upload date:
  • Size: 92.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for agentstate_reducer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9d483d1f92b574ff08c48998cb1cb34609b03fa5552a666c3a202bbf0ef12851
MD5 69fc4df62e1b5f5ddebf8cec97934c09
BLAKE2b-256 f006b65398af1550ca01f1012d8902acc8256e6eddfeca45e769fff792a275b4

See more details on using hashes here.

File details

Details for the file agentstate_reducer-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: agentstate_reducer-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for agentstate_reducer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2c567c2e06e9609a6dc41d555ea893257abaaf4030d4ef51b5a08b5dfffc89df
MD5 79f98ad2e7ca9eb78929f81cb4c9cc04
BLAKE2b-256 d42a004be04390608810a6e0496124075e003e0e498c8a7fe4014cecfa1224a4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page