Skip to main content

langchain-vecr-compress

Drop-in LangChain integration for vecr-compress — the only LLM context compressor with a deterministic retention contract. Before your chat history reaches the model, vecr-compress pins every order ID, URL, date, email, and code reference using an auditable regex whitelist, then packs the remaining budget with the most question-relevant sentences. Structured data never disappears silently; tool calls round-trip intact. This partner package is a thin shim so you can install it with the standard LangChain pattern and get started immediately, with all logic staying in the vecr-compress core.

Install

pip install langchain-vecr-compress

This installs vecr-compress and langchain-core automatically.

30-second example

from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
from langchain_vecr_compress import VecrContextCompressor

compressor = VecrContextCompressor(budget_tokens=120)

history = [
    SystemMessage(content="You are a refund analyst."),
    HumanMessage(content="Hi! I have a question about order ORD-99172."),
    AIMessage(
        content="Let me look that up.",
        tool_calls=[{"id": "c1", "name": "lookup_order", "args": {"order_id": "ORD-99172"}}],
    ),
    HumanMessage(content="The charge was $1,499.00 on 2026-03-15. Please advise."),
    HumanMessage(content="Also, totally just saying hi — hope you're having a great day!"),
]

compressed = compressor.compress_messages(history)

for msg in compressed:
    print(type(msg).__name__, "->", msg.content[:80])

The compressor will drop the filler greeting while preserving ORD-99172, $1,499.00, 2026-03-15, and the AIMessage with its tool_calls list fully intact.

tool_calls round-trip guarantee

AIMessage objects carrying tool_calls are converted to Anthropic-style tool_use content blocks internally. The compressor's skip-mask treats any message containing a tool_use block as must-keep and passes it through verbatim. On the way out, the blocks are converted back to AIMessage(content=..., tool_calls=[...]). This round-trip was audited and tested in vecr-compress v0.1.1.

ai_msg = AIMessage(
    content="searching now",
    tool_calls=[{"id": "t1", "name": "web_search", "args": {"q": "refund policy"}}],
)
[out] = compressor.compress_messages([ai_msg])
assert out.tool_calls[0]["name"] == "web_search"  # always true

Advanced: access compression telemetry

result = compressor.compress_with_report(history)
print(f"{result.original_tokens} -> {result.compressed_tokens} tokens ({result.ratio:.1%})")
print(f"Pinned facts: {len(result.retained_matches)}")
for seg in result.dropped_segments:
    print("dropped:", seg["text"][:60])

Extending the retention contract

import re
from vecr_compress import RetentionRule, DEFAULT_RULES

custom_rules = DEFAULT_RULES.with_extra([
    RetentionRule(name="ticket", pattern=re.compile(r"\bTICKET-\d{4,8}\b")),
])
compressor = VecrContextCompressor(budget_tokens=2000, retention_rules=custom_rules)

License

Apache 2.0 — see LICENSE.

Metadata

Release files for langchain-vecr-compress 0.1.0

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

Source distribution (sdist)

Source distribution for langchain-vecr-compress 0.1.0
File Size Uploaded
langchain_vecr_compress-0.1.0.tar.gz 7.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for langchain-vecr-compress 0.1.0
File Interpreter ABI Platform
langchain_vecr_compress-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.1 kB

Release files / langchain_vecr_compress-0.1.0.tar.gz

Download URL langchain_vecr_compress-0.1.0.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
01afe4ae6d9bad1a3bdbad84dd55bb83a4b898077564ffbc16fba02942629abb
BLAKE2b-256 checksum
How to use checksums
bfbc5ebdb5afac2663ef7c421b3369235b48a6532251008695f6204a8710d872
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / langchain_vecr_compress-0.1.0-py3-none-any.whl

Download URL langchain_vecr_compress-0.1.0-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0f8ce4bd18240e464c99420f058725303e90a3a8fcca9be693d6004a914ec02e
BLAKE2b-256 checksum
How to use checksums
4c951210d8d9c38df9b8a98a45e959441f33d5e17c4498e861fec3570818b884
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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