Skip to content
Compresr docs

LangGraph

Compress tool outputs, chat history, state-graph payloads, checkpoints, stores, and multi-agent handoffs with first-party Compresr components.

LangGraph 1.0+ shares the create_agent middleware mechanism with LangChain, so the three agent-level Compresr middlewares are re-exported from compresr.integrations.langgraph (Python) and @compresr/sdk/integrations/langgraph (TypeScript). On top, the package adds four LangGraph-native components: a state-graph node (make_compresr_node), a checkpoint serializer (CompresrCheckpointSerializer), a store wrapper (CompresrStore), and a multi-agent handoff tool (compresr_handoff_tool). Both SDKs ship the full surface with the same defaults — snake_case classes in Python, camelCase factory functions for middleware plus new-able classes in TypeScript.

1. Install

bash

2. Agent-level middleware (re-exported)

CompresrToolMiddleware, CompresrSummarizationMiddleware, and CompresrPromptMiddleware work unchanged inside LangGraph's create_agent; every keyword argument is identical to the LangChain reference. Only the import path differs:

python

3. Compress a state field inside a custom StateGraph

For graphs you build with StateGraph directly (not the pre-built ReAct shape), use make_compresr_node. It returns a callable node that reads state[context_key], compresses it, and writes the shortened text back into the same field.

python

The node returns {context_key: new_text} when it actually changed the field and {} otherwise, so it composes cleanly with LangGraph's diff-merging state updates. Python also exports compresr_node as an alias; TypeScript exports compresrNode.

Options

PythonTypeScriptRequired?Notes
context_keycontextKeyYesName of the state field to compress.
query_keyqueryKeyRecommendedField whose value is used as the query; the compression model is query-aware.
queryqueryn/aStatic query overriding extractor and query_key.
query_extractorqueryExtractorn/a(state) -> Optional[str], custom extraction.
target_compression_ratiotargetCompressionRation/aDefault 0.5.
min_tokensminTokensn/aDefault 200. Skip if field shorter than this.
coarsecoarsen/aParagraph vs token level; server default.
compression_modelcompressionModeln/aDefault "latte_v1". Pass "latte_v2" for the faster model.
on_erroronErrorn/a"passthrough" (default) or "raise".
api_key / base_url / clientapiKey / baseUrl / clientn/aStandard auth knobs.

4. Compress checkpoints: CompresrCheckpointSerializer

Long agent state can balloon at-rest storage: Postgres rewrites the whole TOAST row on every checkpoint, Redis pays for every byte. CompresrCheckpointSerializer is a JsonPlusSerializer subclass that walks the state on dumps_typed, finds long strings, and rewrites them as {"__compresr__": true, "v": "<compressed>"} sentinels before encoding. Default min_tokens is 500 (vs 200 for middleware); at-rest cost only matters above a certain payload size.

Lossy by design

This serializer is one-way: there is no decompression on loads_typed. Anything you compress at write time is what subsequent reads see. Use the fields allowlist in production to compress only the keys that carry bulk data (retrieved notes, scratchpads), not control state.

python

5. Compress at-rest store entries: CompresrStore

CompresrStore wraps any BaseStore (in-memory, Postgres, Redis, ...) and compresses long string fields on the write side — same lossy contract and same min_tokens=500 default as the checkpoint serializer. It exposes the full BaseStore surface (put, get, delete, search, list_namespaces, batch, plus async siblings); only writes are mutated, everything else delegates straight to the inner store.

python

6. Compress payloads passed between agents: compresr_handoff_tool

In a supervisor / sub-agent architecture, the supervisor often passes a large context blob (retrieved docs, prior conversation) to a sub-agent via a handoff tool. compresr_handoff_tool returns a LangChain BaseTool that compresses task_description and context before emitting Command(goto=agent_name, graph=Command.PARENT).

python

The tool name is auto-generated as f"transfer_to_{agent_name}". Both task_description and context are compressed with synthesised queries ("task for {agent_name}" and the task description itself), so the compression model always has a meaningful query even though it's query-specific.

  • LangChain: full reference for the three middlewares + CompresrExtractor.
  • Models: latte_v2 parameter semantics.