How to Use LangChain Prompt Compression to Reduce Token Usage and Lower API Costs
How to Use LangChain Prompt Compression to Reduce Token Usage and Lower API Costs
Imagine you are talking to a super-smart computer brain, like ChatGPT. When you give it information and ask a question, it “reads” everything you sent. Each word and piece of information costs money and takes time for the computer brain to process. Sometimes, you send a lot of information, and much of it isn’t truly needed for the answer.
This is where LangChain prompt compression comes in handy. It’s like having a clever assistant who reads all your information first. This assistant then picks out only the most important parts before sending them to the super-smart computer brain. This helps you save money and get faster answers.
The Problem with Long Conversations and Data
When you build applications that use large language models (LLMs), you often need to provide a lot of background information. This information might come from documents, past conversations, or databases. All this text adds up, and each piece of text is broken down into “tokens.”
Think of tokens as the building blocks of language that the LLM understands. Every time you send text to an LLM, you pay for each token. If you send too many tokens, your costs can quickly become very high. Also, LLMs have a “context window,” which is a limit on how much text they can read at once.
If your prompt, including all the background information, goes beyond this context window, the LLM simply can’t process it all. This means it might miss crucial details, leading to less accurate or incomplete answers. LangChain prompt compression offers a smart solution to this common problem.
What is Prompt Compression?
Prompt compression is the art of making your input to an LLM shorter without losing important meaning. It’s about getting rid of the fluff and keeping only the essential details. This process helps your application run more efficiently and cost-effectively.
In LangChain, this concept is implemented through smart tools that can analyze your prompt and supporting documents. These tools then decide which parts are most relevant to your specific question. By focusing on context compression, we ensure the LLM receives a concise and powerful input.
This way, you use fewer tokens, which directly translates to token reduction and lower API costs. It also means the LLM can process your request faster because it has less text to read.
Introducing LangChain’s ContextualCompressionRetriever
LangChain provides a powerful tool called the ContextualCompressionRetriever. This special retriever doesn’t just fetch information; it also compresses it. It works by taking information from a regular retriever and then passing it through a “compressor” to shrink it down.
Imagine you have a big library of books, and you ask for books about “penguins.” A regular retriever would find all the books about penguins. The ContextualCompressionRetriever then takes those books, quickly scans them for your specific question (like “What do penguins eat?”), and pulls out only the sentences or paragraphs that answer that specific question.
This targeted context compression ensures that the LLM only sees the most pertinent details. It’s a crucial step in achieving significant token reduction and making your LLM applications more affordable and responsive.
How ContextualCompressionRetriever Works Under the Hood
The ContextualCompressionRetriever operates in two main steps:
- Base Retrieval: First, it uses a regular retriever (like one connected to a vector store) to fetch a larger set of documents that are broadly related to your query. Think of this as getting a handful of potentially relevant books from the library.
- Compression: Second, it takes these retrieved documents and passes them through a chosen “document compressor.” This compressor then analyzes each document and your original query to extract only the most relevant snippets. This is like highlighting only the key sentences in those books that directly answer your specific question.
This two-step process allows for a very effective prompt compression strategy. You get the breadth of retrieval from your base retriever and the precision of compression from your chosen compressor. You can even combine this with advanced techniques for splitting documents meaningfully, as discussed in LangChain Semantic Text Splitter: Chunk by Meaning.
Setting Up Your ContextualCompressionRetriever
To use the ContextualCompressionRetriever, you first need a base retriever. This is typically a retriever built on top of a vector store, where your documents are stored as numerical embeddings. Let’s set up a simple one using an in-memory vector store for demonstration.
First, you’ll need to install the necessary LangChain packages and a local embedding model. We’ll use OpenAIEmbeddings for simplicity, but you could use others.
1
2
# Install necessary packages
!pip install -qU langchain langchain-openai faiss-cpu
Now, let’s prepare some documents and create a simple vector store and retriever.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_text_splitters import CharacterTextSplitter
from langchain_core.documents import Document
# Set up your OpenAI API key (replace with your actual key or environment variable)
# import os
# os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"
# Some example documents
documents = [
"The quick brown fox jumps over the lazy dog.",
"Artificial intelligence is rapidly advancing, leading to new opportunities.",
"Machine learning is a subset of AI that focuses on algorithms learning from data.",
"The capital of France is Paris, a beautiful city known for its Eiffel Tower.",
"The sun rises in the east and sets in the west, marking the beginning and end of each day.",
"Penguins are flightless birds that primarily live in the Southern Hemisphere.",
"They are well-adapted for life in the water, with wings that act as flippers.",
"Most penguin species feed on krill, fish, squid, and other small marine life.",
"Global warming poses a significant threat to their natural habitats.",
"Australia is a large country known for its unique wildlife like kangaroos and koalas."
]
# Split documents (optional for small docs, but good practice)
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.create_documents(documents)
# Create an embeddings model
embeddings = OpenAIEmbeddings()
# Create a FAISS vector store from the documents
vectorstore = FAISS.from_documents(texts, embeddings)
# Create a base retriever from the vector store
base_retriever = vectorstore.as_retriever(search_kwargs={"k": 5}) # Retrieve top 5 relevant documents
This code sets up our basic system. The base_retriever is ready to fetch documents. If you’re building more complex RAG applications, you might be interested in how to Build RAG Applications with LangChain Vector Store in 2026.
Choosing a Document Compressor
Now, we need to choose a compressor that will work with our ContextualCompressionRetriever. LangChain offers several built-in compressors. One powerful option is to use an LLM itself to summarize or extract information, such as LLMChainExtractor or the more advanced LLMLinguaCompressor.
Using LLMChainExtractor for Compression
The LLMChainExtractor uses another LLM to decide which parts of the retrieved documents are most relevant to your query. It can be quite effective for context compression.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from langchain.retrievers import ContextualCompressionRetriever
from langchain.retrievers.document_compressors import LLMChainExtractor
from langchain_openai import ChatOpenAI
# Initialize the LLM for the compressor
llm_compressor = ChatOpenAI(temperature=0)
# Create the LLMChainExtractor
compressor = LLMChainExtractor.from_llm(llm_compressor)
# Create the ContextualCompressionRetriever
compression_retriever = ContextualCompressionRetriever(
base_compressor=compressor,
base_retriever=base_retriever
)
# Now, let's try a query
query = "What do penguins eat?"
compressed_docs = compression_retriever.invoke(query)
print(f"Original documents (if retrieved by base_retriever): {len(base_retriever.invoke(query))} chunks")
print(f"Compressed documents: {len(compressed_docs)} chunks")
for doc in compressed_docs:
print(f"- {doc.page_content}")
In this example, the LLMChainExtractor analyzes the documents found by base_retriever and extracts only the most relevant sentences. This provides significant token reduction before the information is sent to your main LLM.
Advanced Compression with LLMLingua
LLMLingua is a fascinating and highly effective technique for prompt compression. It’s a standalone library that can drastically reduce the token count of your prompts. LangChain provides an integration for LLMLingua through LLMLinguaCompressor. LLMLingua uses a smaller, fast LLM to identify and remove redundant or less important information, making it ideal for context compression.
To use LLMLinguaCompressor, you first need to install the llmlingua library:
1
!pip install -qU llmlingua
Now, let’s integrate LLMLinguaCompressor into our ContextualCompressionRetriever.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from langchain.retrievers.document_compressors import LLMLinguaCompressor
from langchain_openai import OpenAI
# Initialize LLM for LLMLingua. It can be a smaller, faster model.
# Note: LLMLinguaCompressor usually uses its own internal model,
# but sometimes requires an external LLM for specific sub-tasks or if not using their default.
# For simplicity, we'll use OpenAI for the example, but you could configure it differently.
# Set initial settings for LLMLinguaCompressor
# The target_token_number is an important parameter for LLMLinguaCompressor.
# It tries to compress the text down to this number of tokens.
# The `rate` parameter (e.g., 0.5) attempts to compress to 50% of the original length.
# You can also specify `model_name` for the internal compressor model if you have local models.
compressor_llmlingua = LLMLinguaCompressor(
model_name="openai/gpt-3.5-turbo", # You can specify a different model or use the default
device_map="cpu", # or "cuda" if you have a GPU
# Setting the target_token_number or rate is crucial for LLMLingua.
# We'll aim for a 50% reduction for demonstration.
rate=0.5,
# verbose=True # Uncomment for detailed output from LLMLingua
)
# Create the ContextualCompressionRetriever with LLMLinguaCompressor
compression_retriever_llmlingua = ContextualCompressionRetriever(
base_compressor=compressor_llmlingua,
base_retriever=base_retriever
)
# Now, let's try the same query
query = "What do penguins eat?"
compressed_docs_llmlingua = compression_retriever_llmlingua.invoke(query)
print(f"Original documents (if retrieved by base_retriever): {len(base_retriever.invoke(query))} chunks")
print(f"Compressed documents using LLMLingua: {len(compressed_docs_llmlingua)} chunks")
for doc in compressed_docs_llmlingua:
print(f"- {doc.page_content}")
You’ll notice that LLMLinguaCompressor tries to achieve a specific token reduction. The output content might look a bit different, potentially more summarized or even rephrased, as LLMLingua aims to remove redundancy intelligently. This makes it an excellent tool for prompt compression where you want maximum savings.
Practical Example: Building a RAG Application with LangChain Prompt Compression
Let’s put this into a more complete RAG (Retrieval-Augmented Generation) application. A RAG application first retrieves relevant documents and then uses an LLM to answer questions based on those documents. Incorporating LangChain prompt compression here is crucial for efficiency.
We will simulate a scenario where we have a larger set of documents and want to answer a question efficiently. We’ll use the ContextualCompressionRetriever with LLMLinguaCompressor.
Step 1: Prepare More Extensive Documents
For a realistic RAG example, let’s create a more substantial set of documents.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# More extensive documents for our RAG application
long_documents = [
"The history of artificial intelligence dates back to ancient philosophical attempts to understand human thought.",
"The term 'artificial intelligence' was coined by John McCarthy in 1956 at the Dartmouth Conference, marking the birth of AI as an academic field.",
"Early AI research focused on problem-solving and symbolic methods, leading to expert systems in the 1970s and 80s.",
"Machine learning, a branch of AI, gained prominence with the development of neural networks and increased computational power.",
"Deep learning, a subset of machine learning, involves neural networks with many layers and has driven recent breakthroughs in image recognition and natural language processing.",
"Natural Language Processing (NLP) allows computers to understand, interpret, and generate human language.",
"LangChain is a framework designed to simplify the creation of applications using large language models (LLMs).",
"It provides tools for chaining together different components, like models, retrievers, and memory, to build complex AI agents.",
"RAG (Retrieval-Augmented Generation) is a technique where an LLM retrieves information from a knowledge base before generating a response.",
"This helps LLMs provide more accurate and up-to-date answers by grounding them in specific data.",
"Vector stores like FAISS, Chroma, and Weaviate are essential for efficient document retrieval in RAG systems.",
"They store embeddings, which are numerical representations of text, allowing for quick similarity searches.",
"Prompt engineering is the practice of designing prompts for LLMs to achieve desired outcomes.",
"Effective prompt engineering can significantly improve the quality and relevance of LLM responses.",
"Prompt compression techniques help in reducing the length of input prompts, leading to cost savings and faster inference times.",
"LLMLingua is a notable tool used for efficient prompt compression, often integrated into LangChain workflows.",
"ContextualCompressionRetriever in LangChain enables dynamic compression of retrieved documents based on the query.",
"It uses a base retriever to fetch relevant chunks and then a compressor to distill them further.",
"The recent advancements in LLMs have made them capable of understanding and generating human-like text across various domains.",
"Google Gemini offers advanced function calling capabilities, allowing agents to interact with external tools.",
"LangGraph is an extension of LangChain, offering state machines for building more robust multi-step AI agents.",
"Hybrid search, combining keyword and semantic search, improves retrieval accuracy in RAG systems, as seen with tools like Weaviate."
]
# Split documents into smaller chunks
text_splitter_rag = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
docs_rag = text_splitter_rag.create_documents(long_documents)
# Create an embeddings model
embeddings_rag = OpenAIEmbeddings()
# Create a FAISS vector store
vectorstore_rag = FAISS.from_documents(docs_rag, embeddings_rag)
# Create a base retriever for the RAG application
base_retriever_rag = vectorstore_rag.as_retriever(search_kwargs={"k": 7}) # Retrieve top 7 documents
Step 2: Set up the Compressor and ContextualCompressionRetriever
We’ll reuse our LLMLinguaCompressor for efficient prompt compression.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from langchain.retrievers.document_compressors import LLMLinguaCompressor
from langchain.retrievers import ContextualCompressionRetriever
from langchain_openai import OpenAI
# Initialize LLM for LLMLinguaCompressor
# (Ensure llmlingua library is installed)
compressor_llmlingua_rag = LLMLinguaCompressor(
model_name="openai/gpt-3.5-turbo", # You can specify a different model or use the default
device_map="cpu", # or "cuda"
rate=0.4, # Aim for 40% of original length for aggressive compression
# verbose=True
)
# Create the ContextualCompressionRetriever with LLMLinguaCompressor
compression_retriever_rag = ContextualCompressionRetriever(
base_compressor=compressor_llmlingua_rag,
base_retriever=base_retriever_rag
)
Step 3: Build the RAG Chain
Now, let’s build a simple RAG chain. We’ll compare the token usage with and without LangChain prompt compression. We’ll need a prompt template and an LLM. For more about setting up your RAG components, see Build RAG Applications with LangChain Vector Store in 2026.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
from langchain_core.callbacks import get_openai_callback
# Initialize the main LLM for answering questions
llm_rag = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
# Define a prompt template
prompt = ChatPromptTemplate.from_messages([
("system", "Answer the user's question based on the provided context. If you don't know the answer, say you don't know."),
("user", "Context: {context}\n\nQuestion: {input}")
])
# Create a document combining chain
document_chain = create_stuff_documents_chain(llm_rag, prompt)
# --- RAG Chain WITHOUT compression ---
retrieval_chain_no_compression = create_retrieval_chain(base_retriever_rag, document_chain)
# --- RAG Chain WITH compression ---
retrieval_chain_with_compression = create_retrieval_chain(compression_retriever_rag, document_chain)
# Let's ask a question and observe token usage
question = "What is LangChain and how does RAG relate to it? Also, mention the history of AI."
print("--- Running RAG WITHOUT Prompt Compression ---")
with get_openai_callback() as cb:
response_no_compression = retrieval_chain_no_compression.invoke({"input": question})
print(f"\nResponse (No Compression): {response_no_compression['answer']}")
print(f"Token Usage (No Compression): {cb}")
print("\n--- Running RAG WITH LangChain Prompt Compression (LLMLingua) ---")
with get_openai_callback() as cb:
response_with_compression = retrieval_chain_with_compression.invoke({"input": question})
print(f"\nResponse (With Compression): {response_with_compression['answer']}")
print(f"Token Usage (With Compression): {cb}")
You should observe a noticeable difference in the “Token Usage” for the compressed version. The input tokens for the prompt compression chain will be significantly lower, directly translating to token reduction and lower API costs. This is the core benefit of using LangChain prompt compression.
Benefits of LangChain Prompt Compression
Using LangChain prompt compression offers several significant advantages for your LLM applications:
- Reduced API Costs: This is often the most immediate and tangible benefit. By sending fewer tokens to the LLM, you pay less per API call. For applications with high usage, this can lead to substantial savings over time.
- Faster Response Times: Smaller prompts mean the LLM has less text to process. This results in quicker inference times, providing a more responsive user experience. Your users won’t have to wait as long for answers.
- Overcome Context Window Limitations:
Context compressionallows you to fit more relevant information into the LLM’s limited context window. This is especially useful when dealing with very long documents or detailed historical conversations. You can provide deeper context without hitting limits. - Improved Relevance and Accuracy: By focusing on the most important parts of the context,
prompt compressioncan sometimes even improve the LLM’s ability to extract relevant information. With less noise, the LLM can concentrate on the core details needed for a precise answer. - Enhanced User Experience: A faster, more accurate, and more reliable application makes for a better user experience. Users get the information they need quickly and precisely, encouraging continued engagement.
These benefits make LangChain prompt compression an essential technique for anyone serious about building efficient and effective LLM-powered applications.
Advanced Tips and Best Practices
While LangChain prompt compression is powerful, here are some tips to get the most out of it:
- Experiment with Compressors: Don’t stick to just one compressor.
LLMLinguaCompressoris great, butLLMChainExtractoror even simpler ones might be better for specific use cases. Test different options to see what works best for your data and queries. - Tune
LLMLinguaParameters: If you’re usingLLMLinguaCompressor, play withrateandtarget_token_number. A higherrate(closer to 1.0) means less compression, while a lowerrate(e.g., 0.3) means more aggressive compression. Find the sweet spot where you maximizetoken reductionwithout losing critical information. - Pre-processing is Key: Even before compression, ensure your documents are well-chunked. Using intelligent chunking strategies, like the semantic text splitter, can provide a better starting point for your base retriever. You can learn more about this in LangChain Semantic Text Splitter: Chunk by Meaning.
- Monitor Performance and Cost: Always keep an eye on your token usage and API costs. Tools like
get_openai_callbackare invaluable for this. Regularly evaluate if yourprompt compressionis achieving the desired balance of cost savings and response quality. - Consider the Trade-offs: Aggressive
context compressionmight sometimes remove nuances that an LLM would find helpful. If you notice a drop in answer quality, try a less aggressive compression setting. It’s a balance between saving tokens and maintaining quality. - Combine with Other Retrieval Techniques:
ContextualCompressionRetrievercan be layered on top of advanced retrieval methods like hybrid search. For example, using it with aWeaviatevector store and hybrid search can provide extremely robust and efficient RAG. Check out LangChain Weaviate Hybrid Search for Scalable RAG for more details. - Integrate with Agents: For complex, multi-step AI agents,
prompt compressioncan be vital for managing the ever-growing context. When building agents with tools or state graphs, keeping the prompt concise helps the agent stay focused and efficient. This can be especially important in advanced setups like those discussed in LangChain Google Gemini Function Calling Agent with Custom Tools or when using LangGraph StateGraph for Multi-step AI Agent.
By following these best practices, you can effectively leverage LangChain prompt compression to build highly optimized and cost-efficient LLM applications.
When to Use LangChain Prompt Compression
LangChain prompt compression is not always strictly necessary, but it becomes incredibly valuable in specific scenarios:
- When dealing with large volumes of text: If your RAG system frequently retrieves many long documents,
context compressionis a must. - When using expensive LLMs: If you’re using premium models like GPT-4, even small
token reductioncan lead to significant cost savings. - When context window limits are a concern: If your prompts often bump against the maximum token limit of your chosen LLM, compression can prevent truncation and loss of information.
- When response speed is critical: For real-time applications where users expect quick answers, minimizing tokens sent to the LLM helps speed up processing.
- When building complex agents: Agents often accumulate a lot of conversational history or observations.
Prompt compressionhelps keep the agent’s internal thought process efficient.
If you are building simple applications with very short, concise prompts and minimal retrieval, the overhead of compression might not be worth it. However, for most production-ready or data-intensive LLM applications, LangChain prompt compression is a game-changer.
Understanding the Trade-off: Speed vs. Quality
It is important to remember that prompt compression is not magic. It works by making decisions about what information is most important. This means there’s a slight risk that highly aggressive compression could sometimes remove a subtle detail that might have been marginally relevant.
However, for most practical applications, the benefits of token reduction far outweigh this small risk. Especially with intelligent compressors like LLMLingua, the goal is to remove redundancy and low-relevance information, not vital facts. Always test your compressed system to ensure it meets your quality standards.
The key is to find the right balance. You want to achieve significant context compression without compromising the accuracy or completeness of the LLM’s responses. This is an iterative process of testing and fine-tuning.
Conclusion
In the world of large language models, efficiency is key. LangChain prompt compression provides a powerful suite of tools to help you manage the cost and performance of your applications. By intelligently reducing the amount of text sent to LLMs, you achieve significant token reduction, lower API costs, and faster response times.
Whether you’re using ContextualCompressionRetriever with LLMChainExtractor or the advanced capabilities of LLMLinguaCompressor, mastering prompt compression is an essential skill. Start experimenting with these techniques today to unlock the full potential of your LangChain-powered applications and build more cost-effective and responsive AI experiences.
Leave a comment