Optimizing the LLM context window by filtering and summarizing retrieved chunks into high-density snippets.
Contextual Compression is an advanced retrieval refinement technique that solves the "Needle in a Haystack" problem. In a standard RAG system, if you retrieve 5 chunks of 1000 characters each, you are forcing the LLM to process 5000 characters of data, most of which might be irrelevant to the query. Contextual Compression uses a secondary LLM (the Compressor) to extract only the most relevant sentences from every chunk, creating a high-density "Super-Context."
Engineering Trade-offs
Feature
Technical Impact
Pros
Dramatically reduces input token costs; prevents the LLM from being distracted by irrelevant context; maximizes the value of small context windows.
Cons
Adds a pre-generation LLM call, increasing latency; risk of losing vital nuance if compression is too aggressive.
Architecture Overview
The Compression data flow refines "raw" document segments before inference:
[USER Query] │ ┌───────▼───────┐ │ Vector Search │ (Find Top 5 Large Chunks) └───────┬───────┘ │ ┌────────▼────────┐ │ Contextual │ (LLM: Extract query-relevant lines) │ Compressor │ └────────┬────────┘ │ ┌────────▼────────┐ │ Compressed Pool │ (High-density, low-token count) └────────┬────────┘ │ ┌────────▼────────┐ │ Final Generation│ (Focus only on the extracted signal) └─────────────────┘
Implementation Walkthrough
The following steps trace the process of refining a large document chunk into a 2-sentence relevant summary.
Candidate Chunk Retrieval
We retrieve a large 1000-character chunk containing the query topic. This chunk contains the answer but is surrounded by 70% irrelevant data.
const query = "Maximum node count for Phoenix v2?";const chunk = "Phoenix v2 is an enterprise consensus protocol. It has been tested with up to 500 nodes in Phase 1 and 2000 nodes in the current stable release. The protocol also includes an advanced BFT mechanism for node failure detection...";console.log(`[RETRIEVAL] Raw chunk size: ${chunk.length} characters.`);
Contextual Compression Extraction
We use a lightweight model (GPT-4o-mini) to isolate the exact sentence in the chunk that answers the query.
import { generateText } from 'ai';import { openai } from '@ai-sdk/openai';const { text: compressed } = await generateText({ model: openai('gpt-4o-mini'), prompt: `Extract only the sentences from this context that answer: "${query}". \n\n Context: ${chunk}`,});console.log(`[COMPRESS] Length reduction: ${((chunk.length - compressed.length) / chunk.length * 100).toFixed(0)}%`);
Complete Production Script
This script implements a baseline compression pipeline to illustrate token reduction and signal fidelity.
import { generateText } from 'ai';import { openai } from '@ai-sdk/openai';import 'dotenv/config';/** * Intelligent Contextual Compression Pipeline. */async function executeCompression() { const logger = { info: (msg) => console.log(`[INFO] ${new Date().toISOString()} | ${msg}`), timer: (label) => console.time(`[TIME] ${label}`), }; const query = "What is the consensus mechanism for Phoenix v2?"; const rawContext = ` Phoenix v2 is designed for modular decentralization. The core consensus mechanism utilizes Ed25519 signatures and BFT voting groups with a rotating proposer model. Additional features include layer-2 scaling and zero-knowledge proof support for privacy-preserving nodes. `; try { logger.info(`Source Context Length: ${rawContext.length} chars.`); // 1. Primary Compression logger.timer("compression"); const { text: compressed } = await generateText({ model: openai('gpt-4o-mini'), prompt: `Extract the specific technical detail answering: "${query}" from: ${rawContext}`, }); console.timeEnd("compression"); logger.info(`Compressed Detail: "${compressed.trim()}"`); console.log(`\n--- COMPRESSION SUCCESS: TOKEN EFFICIENCY MAXIMIZED ---\n`); } catch (error) { console.error(`[FATAL] Pipeline failed: ${error.message}`); }}executeCompression();
Summary of Impact
Metric
Raw Multi-Chunk Context
Compressed Multi-Chunk Context
Token Efficiency
Baseline (100%)
Optimized (~30-40% of baseline)
LLM Attention Stability
Moderate (Distracted by noise)
High (Signal-focused)
Execution Cost
Baseline
Baseline - ~15% (Savings on generation tokens)
When to Use This
When building RAG applications for models with small context windows (e.g., GPT-3.5 or Llama 3 8B).
To reduce input token costs for high-volume chat applications.
When your retrieved chunks are very long and contain significant conversational/legal filler.