0Pricing
AI Agents with LangChain & Autonomous Workflows · Lesson

Retrievers & Contextual Compression

Turn a vector store into a tunable retriever, control how many documents come back, and use contextual compression to strip irrelevant text before it reaches the LLM.

Retrievers & Contextual Compression is a free AI Agents with LangChain & Autonomous Workflows lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents with LangChain & Autonomous Workflows learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

From Vector Store to Retriever

A vector store knows how to search, but agents talk to a retriever — a thin interface with one job: given a query, return relevant documents.

Any vector store exposes as_retriever() to produce one.

retriever = vectorstore.as_retriever()
docs = retriever.invoke('How do I reset my password?')

Controlling k

The k parameter sets how many documents to return. Too few misses context; too many wastes tokens and adds noise.

retriever = vectorstore.as_retriever(
    search_kwargs={'k': 4}
)

Similarity Score Thresholds

Instead of a fixed count, you can return only documents above a relevance score. This avoids forcing irrelevant chunks when nothing good matches.

retriever = vectorstore.as_retriever(
    search_type='similarity_score_threshold',
    search_kwargs={'score_threshold': 0.7}
)

Maximal Marginal Relevance

MMR balances relevance with diversity, avoiding near-duplicate chunks. It is great when documents repeat similar text.

retriever = vectorstore.as_retriever(
    search_type='mmr',
    search_kwargs={'k': 4, 'fetch_k': 20}
)

Metadata Filtering

Documents carry metadata (source, date, category). You can filter retrieval to a subset, e.g. only the current product version.

retriever = vectorstore.as_retriever(
    search_kwargs={'filter': {'version': 'v2'}}
)

The Noise Problem

Even relevant chunks often contain unrelated sentences. Sending that noise to the LLM dilutes the answer and burns tokens.

Contextual compression shrinks each retrieved document to only the parts that matter for the query.

ContextualCompressionRetriever

This wrapper sits in front of a base retriever and post-processes its results with a compressor.

from langchain.retrievers import ContextualCompressionRetriever
from langchain.retrievers.document_compressors import LLMChainExtractor

compressor = LLMChainExtractor.from_llm(llm)
compression_retriever = ContextualCompressionRetriever(
    base_compressor=compressor,
    base_retriever=retriever
)

How Extraction Works

LLMChainExtractor asks the LLM to pull only the sentences from each document that are relevant to the query, discarding the rest before it reaches the final prompt.

docs = compression_retriever.invoke(
    'What is the refund window?'
)

Cheaper Filters

LLM extraction costs tokens. EmbeddingsFilter is a faster, cheaper alternative that drops documents below a similarity threshold using embeddings only — no extra LLM call.

from langchain.retrievers.document_compressors import EmbeddingsFilter

compressor = EmbeddingsFilter(
    embeddings=embeddings,
    similarity_threshold=0.76
)

Chaining Compressors

Combine steps in a DocumentCompressorPipeline: first a cheap embeddings filter, then LLM extraction on what survives. This keeps quality high while controlling cost.

from langchain.retrievers.document_compressors import DocumentCompressorPipeline

pipeline = DocumentCompressorPipeline(
    transformers=[embeddings_filter, extractor]
)

Plugging Into RAG

Because a compression retriever has the same interface as any retriever, you swap it into your RAG chain without changing the rest of the pipeline. Cleaner context usually means better, cheaper answers.

Quick Check

Test your retriever knowledge.

Recap

You learned to tune and refine retrieval:

  • Convert a store with as_retriever() and tune k
  • Use score thresholds, MMR, and metadata filters
  • Contextual compression removes noisy text
  • EmbeddingsFilter is a cheap alternative to LLM extraction
  • Chain compressors for quality plus efficiency

Better retrieval is often the biggest lever for RAG quality.

Frequently asked questions

Is the “Retrievers & Contextual Compression” lesson free?

Yes — the full text of “Retrievers & Contextual Compression” is free to read here on the web, and the AI Agents with LangChain & Autonomous Workflows course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents with LangChain & Autonomous Workflows course, upgrade to CoddyKit PRO.

What will I learn in “Retrievers & Contextual Compression”?

Turn a vector store into a tunable retriever, control how many documents come back, and use contextual compression to strip irrelevant text before it reaches the LLM. You practise AI Agents with LangChain & Autonomous Workflows with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start AI Agents with LangChain & Autonomous Workflows?

No prior experience is required. AI Agents with LangChain & Autonomous Workflows on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Retrievers & Contextual Compression” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this AI Agents with LangChain & Autonomous Workflows lesson?

Yes. Every AI Agents with LangChain & Autonomous Workflows lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Document Loaders Explained
  2. Text Splitters & Embeddings
  3. Vector Stores for Retrieval
  4. Retrievers & Contextual Compression
← Back to AI Agents with LangChain & Autonomous Workflows