Skip to main content

Reversible distil context compression for LangChain and LangGraph

Project description

langchain-distil

Reversible distil context compression for LangChain and LangGraph.

distil compresses an agent's accumulated messages before the model call: tool/function messages get a reversible digest, human/system messages get lossless compression, and the model's own (ai/assistant) messages are never rewritten. Originals persist in a local handle store and are recoverable via distil's distil_expand MCP tool or distil.mcp_server.load_restore(handle).

Decision-equivalence between compressed and full context is certified separately, offline, by distil bench / distil validate. This package performs the compression only.

Install

pip install langchain-distil                 # message-dict hooks
pip install "langchain-distil[runnable]"     # + as_runnable() (needs langchain-core)

Usage

LangGraph — drop distil in as a pre_model_hook:

from langgraph.prebuilt import create_react_agent
from langchain_distil import pre_model_hook

agent = create_react_agent(model, tools, pre_model_hook=pre_model_hook())

Any message list — compress before .invoke:

from langchain_distil import compress_messages

resp = model.invoke(compress_messages(messages))

As a Runnable in a chain:

from langchain_distil import as_runnable

chain = as_runnable() | model

verbatim=True selects Tier-0 lossless mode (no digest stubs).

License

MIT

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

langchain_distil-0.1.0.tar.gz (120.5 kB view details)

Uploaded Source

Built Distribution

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

langchain_distil-0.1.0-py3-none-any.whl (2.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for langchain_distil-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2de1d5fc432efcb41c4eed6544d1c675756ac1ac13fc3eb5322cefeaa64a1fa6
MD5 dc5dff7356417376ad255b7039990576
BLAKE2b-256 499b03225dade27cefcb11f8e42775995197f045d9e7ad6a240561b67f787771

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for langchain_distil-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0e0185c8905ec7e04d6d5b4337e24f5df5305420945526017c1d4c9f796492c2
MD5 23d33f35a694b8479fa34cce506cf585
BLAKE2b-256 806ad92ca46566a1d389a292c8eb5f84f1d2a2a9db6d696a27e7985300bfdd71

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