Skip to main content
Guides & Strategies6 min readUpdated July 2026

Compression Strategies Guide

Choosing the optimal strategy for System Prompts, RAG Contexts, and Chat Logs.

Strategy 1: Query-Aware RAG Context Compression

When compressing retrieved vector documents, prioritize content highly relevant to the user query:
rag_strategy.py
from llmslim import compress_documents

retrieved_docs = ["Chunk 1 text...", "Chunk 2 text...", "Chunk 3 text..."]
user_query = "What is the Q3 net margin for enterprise hardware?"

# Filter & compress RAG contexts by query relevance score
compressed_chunks = compress_documents(
    retrieved_docs,
    query=user_query,
    target_ratio=0.3
)

final_prompt = "\n---\n".join([c.compressed_text for c in compressed_chunks])

Frequently Asked Questions

Which strategy should I use for 50-page PDF RAG contexts?

Use RAG Document Chunk strategy with compress_documents(docs, query='...') to rank sentences by query relevance.