AI Wisdom

Context Compression

Compress conversation history or retrieved documents to fit within context window limits while preserving critical information.

In this article

Problem: Long conversations and large document retrievals exceed context window limits, causing truncation and loss of important context.

Solution: Apply compression strategies to reduce context size while preserving the most relevant information for the current query.

Compression strategies:

  1. Conversation summarisation: After every N turns (e.g., 10), summarise the full conversation history into a concise summary and replace the old turns
  2. Selective retention: Score each historical turn for relevance to the current query (embedding similarity); discard low-scoring turns
  3. Document compression: For retrieved chunks, use an LLM to extract only the relevant sentences from each chunk before inserting into context
  4. LLMLingua (Microsoft): Token-level compression โ€” removes redundant tokens from prompts using a small surrogate model, achieving 3-20x compression with minimal quality loss

Trade-Offs:

  • โœ” Pro: Enables infinite-length conversations within finite context windows
  • โœ” Pro: Reduces tokens โ†’ reduces cost per turn
  • โœ– Con: Summarisation loses nuance; selective retention may discard information that later becomes relevant
  • โœ– Con: Adds latency for compression LLM calls

When To Use: Long multi-turn conversations, multi-document RAG, agent loops with many tool outputs. When to avoid: Short, stateless API calls where context never grows.

Discussion

Sign in to share your feedback and join the discussion.