Skip to main content

LangChain integration for vecr-compress: deterministic retention-guaranteed LLM context compression.

Project description

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)

Links

License

Apache 2.0 — see LICENSE.

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_vecr_compress-0.1.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

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

langchain_vecr_compress-0.1.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: langchain_vecr_compress-0.1.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for langchain_vecr_compress-0.1.0.tar.gz
Algorithm Hash digest
SHA256 01afe4ae6d9bad1a3bdbad84dd55bb83a4b898077564ffbc16fba02942629abb
MD5 0f00610975b10ff5250c0cb8dfad2db6
BLAKE2b-256 bfbc5ebdb5afac2663ef7c421b3369235b48a6532251008695f6204a8710d872

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for langchain_vecr_compress-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0f8ce4bd18240e464c99420f058725303e90a3a8fcca9be693d6004a914ec02e
MD5 b4753d9bdaae54c6f19d4299d18e4db1
BLAKE2b-256 4c951210d8d9c38df9b8a98a45e959441f33d5e17c4498e861fec3570818b884

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