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
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.
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.
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.
The three middlewares cover orthogonal token sources and compose in the same create_agent / createAgent call:
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:
| Python | TypeScript | Default | Notes |
|---|---|---|---|
api_key | apiKey | n/a | Required unless client is passed. |
client | client | n/a | Pre-built CompressionClient: bypasses api_key/base_url. |
base_url | baseUrl | https://api.compresr.ai | Override for self-hosted. |
compression_model | compressionModel | "latte_v1" | Query-aware, requires a query. Pass "latte_v2" to opt into the newer model. |
target_compression_ratio | targetCompressionRatio | 0.5 | 0 < r ≤ 1 removes that fraction; r > 1 is Nx target. |
coarse | coarse | server default | Paragraph-level vs token-level. |
query | query | n/a | Static query overriding everything else. |
query_extractor | queryExtractor | n/a | Custom resolution: (tool_call, messages) -> str for the tool middleware, (messages) -> str for the other two. |
on_error | onError | "passthrough" | "raise" to fail fast in tests. The type is exported as ErrorPolicy for typed configs. |
CompresrToolMiddleware only:
| Python | TypeScript | Default | Notes |
|---|---|---|---|
min_tokens | minTokens | 200 | Skip tool outputs shorter than this. |
allow_tools | allowTools | None | Whitelist of tool names. Pass allow_tools OR ignore_tools, not both. |
ignore_tools | ignoreTools | None | Blacklist. |
query_arg | queryArg | n/a | Pull the query directly from a named tool arg. |
CompresrSummarizationMiddleware only:
| Python | TypeScript | Default | Notes |
|---|---|---|---|
max_tokens_before_summary (alias trigger) | maxTokensBeforeSummary (alias trigger) | 4000 | Token threshold that triggers a summary. Aliases match LangChain's own SummarizationMiddleware naming. |
messages_to_keep (alias keep) | messagesToKeep (alias keep) | 20 | Recent messages preserved verbatim. |
token_counter | tokenCounter | SDK estimate_tokens | char/4 heuristic, upgrades to tiktoken cl100k_base when available. |
CompresrPromptMiddleware only:
| Python | TypeScript | Default | Notes |
|---|---|---|---|
max_tokens | maxTokens | n/a | Required. Hard ceiling for the outbound prompt. |
min_tokens | minTokens | 200 | Don't touch messages smaller than this. |
token_counter | tokenCounter | SDK estimate_tokens | Same 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.
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 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:
Related
- LangGraph: same middlewares applied inside
StateGraph, plus node-level helpers, lossy checkpoint serializer, lossy store wrapper, and multi-agent handoff. - Models:
latte_v2parameter semantics (target_compression_ratio,coarse, and friends). - RAG guide: the underlying retrieve → compress → answer pipeline.