Integration guide

LangChain prompt compression integration

Add SuperCompress to any LangChain application with a custom callback handler. Every LLM call in your chain automatically gets compressed context.

By Arjun Shah - Creator of SuperCompress - Updated 2026-07-03

Installation

pip install supercompress langchain

Callback handler for LangChain

from supercompress import Compressor
from langchain.callbacks import BaseCallbackHandler

class CompressionHandler(BaseCallbackHandler):
    def __init__(self):
        self.comp = Compressor()

    def on_llm_start(self, serialized, prompts, **kwargs):
        result = self.comp.compress(prompts[0].text, "Continue.")
        prompts[0].text = result.compressed_text

Using with LangChain agents

from langchain.agents import create_react_agent
agent = create_react_agent(llm, tools, prompt)
agent.callbacks = [CompressionHandler()]

Frequently asked questions

Does this work with LangChain streaming?

Yes. Compress first, then stream normally.

Can I use it with LangGraph?

Yes. Add the CompressionHandler to any node that calls an LLM.

Try it yourself

Paste your long prompt into the playground, ask a question, and see what SuperCompress keeps and removes. Free, no signup needed.

Open the Playground See benchmarks