Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Learn/LangChain Mastery/Part 6 · 5. Retrieval & RAG Patterns
Lesson 44 of 68

Contextual Compression

Trimming the fat. Why send 1000 tokens when 50 will do?

Derpx0610 Jan · 1 min read
LangChain Mastery
The Retriever InterfaceSimilarity vs MMRMulti-Query RetrievalRAG-FusionQuery DecompositionParent Document RetrievalContextual CompressionPost-Retrieval Re-RankingConversational RAG & History
Contents
1. The Retrieval Paradox2. The Compressor3. TechniquesA. LLMChainExtractorB. EmbeddingsFilter4. Summary
0% read · 1 min leftBack to top ↑
Course outline & contents0%
LangChain Mastery
The Retriever InterfaceSimilarity vs MMRMulti-Query RetrievalRAG-FusionQuery DecompositionParent Document RetrievalContextual CompressionPost-Retrieval Re-RankingConversational RAG & History
Contents
1. The Retrieval Paradox2. The Compressor3. TechniquesA. LLMChainExtractorB. EmbeddingsFilter4. Summary

1. The Retrieval Paradox

You retrieve 10 documents because you want High Recall. But now your context window is full of irrelevant noise. If the answer is in the 7th document, the LLM might miss it ("Lost in the Middle").

2. The Compressor

A Contextual Compressor sits between the Retriever and the LLM. It takes the 10 retrieved docs and asks: "Given the user query, what parts of these docs are actually useful?"

It acts like a highlighter pen.

3. Techniques

A. LLMChainExtractor

Uses a small, fast LLM to rewrite the chunk.

  • Original: "The CEO, John Smith, announced earnings today. He also mentioned he likes golf. Revenue is up 5%."
  • Query: "What is the revenue?"
  • Compressed: "Revenue is up 5%."

B. EmbeddingsFilter

Embeds the query and the sentences within the chunk. Only keeps sentences with high similarity. Cheaper than LLM extraction.

4. Summary

Retrieval gets the document. Compression extracts the answer. This creates a higher quality "Context Density" for the final generation model.

Key Intuition: "Don't pay for tokens you don't need."

Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonParent Document RetrievalNext lesson →Post-Retrieval Re-Ranking