What is context compression?
Context compression is the general name for methods that try to represent the prompt, retrieved documents or conversation history given to a language model with fewer tokens while keeping the information the task needs.
Unlike context extension, which enlarges the window a model can handle, context compression shrinks what goes into it. Unlike text summarisation, written for people, compressed context is designed as model input and need not read well to a human.
- Topic
- Prompts, context, reasoning and generation
- Also known as
- Prompt compression
How context compression works
- A small language model estimates how much information each token carries; low-information tokens or sentences are dropped.
- Retrieved documents are cut to question-relevant sentences or replaced by short summaries.
- In long conversation history, older turns are summarised while recent ones stay intact.
- Some methods compress the prompt into a few special tokens the model has learned, which requires training.
In more detail
KV cache compression and eviction methods
Beyond compressing input text (hard-prompt compression) or learning soft gist tokens, modern serving stacks also perform KV-cache compression directly in GPU memory. Techniques such as H2O (Heavy-Hitter Oracle), SnapKV, StreamingLLM and KV-cache quantisation evict or compress low-attention tokens inside the cache without altering the input text seen by the user.
Syntax and identifier damage and the interplay with prefix caching
Perplexity-based token pruning can accidentally remove syntax characters, identifiers, UUIDs, SQL keywords or negation words ("not", "except"), causing silent failures in code or structured workflows. It can also break KV prefix caching when static system instructions are altered per request, so production pipelines compress only dynamic retrieved chunks and protect structured spans.
Three approaches: hard, soft and query-aware compression
Context compression spans three distinct paradigms: hard prompt compression (pruning tokens/sentences so any off-the-shelf LLM can read the shortened text), soft prompt compression (encoding text into continuous embeddings or gist tokens, requiring model training and tying compressed representations to one model) and extractive/abstractive RAG compression (like LongLLMLingua or query-focused extractors).
Commonly confused terms
| Term | How it differs |
|---|---|
| Retrieval and reranking | Retrieval/reranking selects which relevant chunks enter the pipeline before generation, whereas context compression reduces the token footprint of the already-selected chunks, prompt instructions or KV cache. |
Why context compression matters for on-premises AI
Context compression can let the same in-house GPU capacity handle longer documents and more retrieved passages while cutting time to first token and memory use. Its risk is information loss: dropping an exception in a regulation or a figure makes the answer wrong. The compression rate is therefore chosen by task, critical fields are left uncompressed and results with compressed and full context are compared on the same test set. Original text is kept where citation is needed.
A capital markets body shrinks context in its internal regulation assistant by keeping only question-relevant sentences from retrieved documents; sentences with article numbers and dates are always kept, with answers citing the original articles.
Common questions
How does context compression differ from summarisation?
A summary is readable text for people. Compressed context is designed as model input and may not suit human reading.
What is the risk of context compression?
Dropping a needed detail. It is managed by protecting critical information types and comparing results with full context.
Related terms
- Context window
- Context engineering
- Context extension
- Long-context model
- Text summarisation
- Lost in the middle
Sources
- Jiang et al., LLMLingua: Compressing Prompts for Accelerated Inference of Large Language Models, 2023
- Mu, Li and Goodman, Learning to Compress Prompts with Gist Tokens, 2023
In the glossary: Glossary, A to C