Skip to content
Compresr docs

LangChain

First-party Compresr middleware, document compressor, and tool wrappers for LangChain 1.0+ agents and retrievers.

compresr.integrations.langchain (Python) and @compresr/sdk/integrations/langchain (TypeScript) cover the four places LangChain users typically burn tokens: tool outputs in an agent loop, long chat history, the final outbound prompt, and retrieved documents in a ContextualCompressionRetriever. Every entry point makes the same Compresr call as the SDK — same auth, same models, same parameter semantics.

Python is class-based, TypeScript is factory-based

In Python the three middlewares are classes (CompresrToolMiddleware(...)); in TypeScript they are factory functions (compresrToolMiddleware({...})). The document compressor CompresrExtractor is a class in both. Snake_case kwargs in Python; camelCase options in TypeScript.

1. Install

bash

LangChain 1.0+ is required for the create_agent / createAgent middleware mechanism; the document compressor (CompresrExtractor) works on any version that ships BaseDocumentCompressor. LangChain deps ship in the base install — pip install compresr[langchain] still resolves for back-compat but adds nothing (extras are no-ops since 2.6.0).

2. Compress tool outputs in an agent

CompresrToolMiddleware plugs into create_agent. Each time a tool returns, the middleware compresses the result against the user's question before it lands in agent state, so the LLM's next reasoning step sees a shorter ToolMessage. api_key= can be omitted when COMPRESR_API_KEY is set or you've run compresr-sdk login — see Authentication.

python

3. Replace old history with a compressed summary

CompresrSummarizationMiddleware is a KV-cache-friendly alternative to LangChain's SummarizationMiddleware. When the conversation crosses a token threshold, it compresses everything older than the last N messages into a single [Earlier conversation summary] ... HumanMessage and leaves recent messages untouched. No LLM round-trip: token-level compression. It detects its own prior summary via the [Earlier conversation summary] prefix and refuses to re-summarize an already-summarized prefix, so it's safe to run on every turn.

python

4. Cap the outbound prompt with CompresrPromptMiddleware

The last-mile budget cap. CompresrPromptMiddleware runs in wrap_model_call and walks the messages largest-first, compressing just enough to fit under max_tokens. It mutates the model request, not agent state; the next turn still sees the original messages.

python

The three middlewares cover orthogonal token sources and compose in the same create_agent / createAgent call:

python

5. Middleware options

All keyword-only (Python) / options-object fields (TypeScript). The compression knobs share their semantics with the agent client's compression policy. Shared by all three middlewares:

PythonTypeScriptDefaultNotes
api_keyapiKeyn/aRequired unless client is passed.
clientclientn/aPre-built CompressionClient: bypasses api_key/base_url.
base_urlbaseUrlhttps://api.compresr.aiOverride for self-hosted.
compression_modelcompressionModel"latte_v1"Query-aware, requires a query. Pass "latte_v2" to opt into the newer model.
target_compression_ratiotargetCompressionRatio0.50 < r ≤ 1 removes that fraction; r > 1 is Nx target.
coarsecoarseserver defaultParagraph-level vs token-level.
queryqueryn/aStatic query overriding everything else.
query_extractorqueryExtractorn/aCustom resolution: (tool_call, messages) -> str for the tool middleware, (messages) -> str for the other two.
on_erroronError"passthrough""raise" to fail fast in tests. The type is exported as ErrorPolicy for typed configs.

CompresrToolMiddleware only:

PythonTypeScriptDefaultNotes
min_tokensminTokens200Skip tool outputs shorter than this.
allow_toolsallowToolsNoneWhitelist of tool names. Pass allow_tools OR ignore_tools, not both.
ignore_toolsignoreToolsNoneBlacklist.
query_argqueryArgn/aPull the query directly from a named tool arg.

CompresrSummarizationMiddleware only:

PythonTypeScriptDefaultNotes
max_tokens_before_summary (alias trigger)maxTokensBeforeSummary (alias trigger)4000Token threshold that triggers a summary. Aliases match LangChain's own SummarizationMiddleware naming.
messages_to_keep (alias keep)messagesToKeep (alias keep)20Recent messages preserved verbatim.
token_countertokenCounterSDK estimate_tokenschar/4 heuristic, upgrades to tiktoken cl100k_base when available.

CompresrPromptMiddleware only:

PythonTypeScriptDefaultNotes
max_tokensmaxTokensn/aRequired. Hard ceiling for the outbound prompt.
min_tokensminTokens200Don't touch messages smaller than this.
token_countertokenCounterSDK estimate_tokensSame char/4 heuristic with tiktoken upgrade.

6. Compress retrieved documents

CompresrExtractor is a BaseDocumentCompressor: a drop-in replacement for LLMChainExtractor inside a ContextualCompressionRetriever. Batches all eligible documents into a single Compresr call (up to 100 per batch) — pairs well with high-recall retrieval (k=20+). See the RAG guide for the surrounding pipeline.

python

The extractor sets metadata["compresr"] = True on every document it touched and leaves documents below min_tokens unchanged (or filters them out if drop_below_min=True).

7. Wrap a single tool

If you only need compression on one tool, without an agent middleware, wrap it directly. wrap_tool_with_compression / wrapToolWithCompression returns a new StructuredTool preserving name, description, args_schema, return_direct, and error handlers.

python

Python raises TypeError if the input isn't a StructuredTool; wrap a raw function with @tool first. TypeScript is more permissive: wrapToolWithCompression accepts anything exposing name plus func, _call, or invoke.

For the case where you own the tool's source, there is also a decorator form with the same options, compress_tool_output / compressToolOutput, applied on top of @tool:

python
  • LangGraph: same middlewares applied inside StateGraph, plus node-level helpers, lossy checkpoint serializer, lossy store wrapper, and multi-agent handoff.
  • Models: latte_v2 parameter semantics (target_compression_ratio, coarse, and friends).
  • RAG guide: the underlying retrieve → compress → answer pipeline.