Skip to main content

ContextSage logo

ContextSage

PyPI Python versions CI Documentation License

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:

async def summarize_history(summary_model, messages):
    async with ContextSummarizer(summary_model, 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.

Integrations

The optional LangChain integration wraps the same core engine:

from contextsage.integrations.langchain import IntelligentSummarizationMiddleware

middleware = IntelligentSummarizationMiddleware(
    "openai:gpt-5-mini",
    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, custom parsing, policies, and recovery. 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 Agent Skills for coding agents that need to integrate or extend the package. The skills guide also lists independently maintained dependency and integration skills.

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.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 contextsage 1.0.2
File Size Uploaded Provenance Actions
contextsage-1.0.2.tar.gz 61.0 kB Without provenance Details for contextsage-1.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for contextsage 1.0.2
File Interpreter ABI Platform Actions
contextsage-1.0.2-py3-none-any.whl Python 3 none any Details for contextsage-1.0.2-py3-none-any.whl

Total release size: 132.6 kB

Release files / contextsage-1.0.2.tar.gz

Download URL contextsage-1.0.2.tar.gz
Size 61.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d4f39f0b898d9b2776e9c9974d4afe3694b82a35f4a6e2c9ff91f5b1681a0187
BLAKE2b-256 checksum
How to use checksums
2ccced8e91f352884fb23bcf490a9c3b0dc6e7f212a91ebfdd9b86d088bb58fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","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.2-py3-none-any.whl

Download URL contextsage-1.0.2-py3-none-any.whl
Size 71.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6f1c8bed9dd47e8a6ed0d509616819cc304fd365c209605d9efe64a42f4f3b5e
BLAKE2b-256 checksum
How to use checksums
09070c56636298c8c0a0d9caab348d9be0caae2936b11fe1f44492e3f9b3cd58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.15 {"installer":{"name":"uv","version":"0.12.15","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.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.

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