ContextSage
Information-aware context summarization for AI agents. ContextSage compresses long conversation histories while preserving important facts, tool relationships and provenance. Its asynchronous, framework-agnostic core uses a neutral message model and can be adapted to any agent framework.
Install
pip install contextsage
Install optional integrations only when you use them:
pip install "contextsage[langchain]" # LangChain and LangGraph
pip install "contextsage[otel]" # OpenTelemetry SDK and OTLP exporter
pip install "contextsage[all]" # both optional integrations
The core requires Python 3.12 or newer and does not install an agent framework.
Framework-neutral quickstart
Implement parsefabric.LanguageModel with your summary provider, convert your
framework's history to contextsage.Message, then call the async engine before
the next model turn:
from contextsage import ContextSummarizer, ModelBudget
async def summarize_history(summary_model, messages):
async with ContextSummarizer(
summary_model,
target_budget=ModelBudget(128_000),
summary_budget=ModelBudget(64_000),
trigger=("tokens", 100_000),
) as engine:
result = await engine.summarize(messages, thread_id="conversation-42")
if result is not None:
messages = list(result.messages)
The engine parses text and structured content, compacts repetitive material,
preserves facts such as identifiers and corrections, validates the result,
and records lineage. If the history does not meet the trigger, summarize
returns None. See the runnable quickstart and the
core concepts.
The budgets above are illustrative: configure the actual agent and summary
model limits, provider overhead, and model-specific token counters. If
compaction is not enough, ContextSage can reduce oversized context using
sequential, hierarchical, or explicitly grouped passes. It raises
ReductionError when the configured constraints cannot be met safely.
Summarization cannot guarantee semantic equivalence; protect exact text and
choose a trusted provider when needed. See
oversized-context reduction.
Integrations
The optional LangChain integration wraps the same core engine:
from contextsage import ModelBudget
from contextsage.integrations.langchain import IntelligentSummarizationMiddleware
def make_middleware(summary_model):
return IntelligentSummarizationMiddleware(
summary_model,
target_budget=ModelBudget(128_000),
summary_budget=ModelBudget(64_000),
trigger=("tokens", 100_000),
keep=("messages", 20),
)
Or implement FrameworkAdapter for your framework's message type. The
adapter maps between native messages and the neutral Message model; no
framework dependency is needed by the core. See the LangChain integration
and custom framework example.
Downstream customization
ContextSage exposes the extension points of its dependencies rather than restricting them to built-in choices:
| Package | Customizable components |
|---|---|
| parsefabric | LanguageModel, tokenizers, line patterns, parsers and registries, fenced-code routes, code detection, JSON codec, lifecycle sink, and execution backend. |
| agxai | ProvenanceStore, observability, attribution, explanation and policy providers, capture policy, runtime configuration, registry, plugins, and custom state capture. |
| OpenTelemetry | Use the built-in event sink and SDK/exporters, or supply an EventSink for your telemetry platform. |
| LangChain (optional) | Supply a chat model, token counter, and LangChainAdapter to the integration. |
Use ParsingOptions to configure parsefabric and
create_agxai_runtime(...) to configure agxai. The
downstream customization example
demonstrates custom components from both packages.
Examples and documentation
The examples include framework adapters, LangChain, PostgreSQL provenance, OpenTelemetry, Prometheus, native reduction, custom parsing, policies, and safe failure. Scripted examples run without API keys and their output is verified in CI. The documentation site includes installation, configuration, integration guides, operations guidance and the API reference.
ContextSage also publishes an Agent Skill for coding agents that need to integrate or extend the package.
Development
uv sync --locked --all-extras --all-groups
uv run pytest
uv run ruff check .
uv run pyrefly check
See contributing and testing. ContextSage is licensed under the Apache License 2.0.
Metadata
Release files for contextsage 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| contextsage-1.0.1.tar.gz | 75.3 kB | Without provenance | Details for contextsage-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| contextsage-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for contextsage-1.0.1-py3-none-any.whl |
Total release size: 161.7 kB
Release files / contextsage-1.0.1.tar.gz
| Download URL | contextsage-1.0.1.tar.gz |
|---|---|
| Size | 75.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e3b60a05d512255e96744ff33c37889a742d1896d139269db6ecf3b7e7ae6691
|
|
BLAKE2b-256 checksum How to use checksums |
09c592a1608ec1a9dbf5d814572e714ea393027b862b1fda7849d47f2c4f419f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","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}
|
Release files / contextsage-1.0.1-py3-none-any.whl
| Download URL | contextsage-1.0.1-py3-none-any.whl |
|---|---|
| Size | 86.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ef459416ffe2727b500e02adc8a04d86689c9fec9b26343cf3809fd1362ddf2b
|
|
BLAKE2b-256 checksum How to use checksums |
4d6a73b3f7bdd7720aab02fc3fa4a9508a667640a4f59fa37a747384fda06a66
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","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}
|
Security for contextsage 1.0.1
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.