← All posts

Context Engineering · 5 min read

Prompt and context compression, explained

Fewer tokens can mean lower cost, lower latency and sometimes better answers. A practical guide to token pruning, summarisation and the small models that do the compressing — and when compression backfires.

Subhash Nunna ·

Every token you send to a large language model costs money and time. In agents and retrieval systems, the context often fills up with long documents, tool output and conversation history — much of it repetitive or irrelevant.

Prompt compression shrinks that input while keeping what matters. Done well, it cuts cost and latency. Done carelessly, it quietly removes the one fact the model needed.

This post covers the main approaches, what the research says, and how to use a small language model (SLM) to do the compressing.

Three ways to compress

1. Token pruning (extractive). Keep the original words, but drop the ones that carry little information. The output reads like a telegram, but large models are surprisingly good at understanding it.

2. Summarisation (abstractive). Rewrite the content into a shorter version in new words. This is easier for humans to read and works well for conversation history, but it can drop details or introduce errors.

3. Structural trimming. Before reaching for any model, cut what you obviously don’t need: boilerplate, duplicate retrieved chunks, huge tool outputs, HTML markup and unused tool definitions. This is cheap and often gives the biggest win.

In practice you combine them: structural trimming everywhere, pruning for long retrieved documents, and summarisation for older conversation turns.

How LLMLingua-style compression works

The best-known family of methods comes from Microsoft Research’s LLMLingua project. The core idea is simple: use a small language model to score how important each token is, then drop the least important ones.

  • LLMLingua scores tokens using a small model’s perplexity — roughly, how surprising each token is. Predictable tokens carry little information and can go. Microsoft reports up to 20x compression with minimal loss on its benchmarks.
  • LongLLMLingua is built for long contexts. It compresses with the user’s question in mind and reorders content so the important parts aren’t lost in the middle. Microsoft reports a 17.1% performance improvement with 4x compression on its long-context tests.
  • LLMLingua-2 treats compression as a classification task: a small BERT-sized encoder, trained on labels distilled from a large model, decides which tokens to keep. Microsoft reports it is 3–6x faster than the original LLMLingua and handles out-of-domain text better.

The appeal is that the compressor is small and cheap, while the model it feeds is large and expensive.

What the research says about trade-offs

An independent 2025 empirical study compared six compression methods across several models and tasks. Its findings are a useful reality check:

  • The LLMLingua family performed best overall, especially at high compression ratios.
  • Long contexts can benefit. On long-context benchmarks, moderate compression sometimes improved answers. LongLLMLingua was particularly strong on question answering over long documents.
  • Short contexts lose accuracy as compression increases. If your prompt is already short, compressing it rarely pays.
  • Every method increased hallucination somewhat, mainly because information was lost. LongLLMLingua and LLMLingua-2 had the lowest rates in the study; random token removal was by far the worst.
  • Multimodal results were inconsistent, which makes sense: these methods were designed for text.
  • Odd details matter. In one test, removing the word “the” had almost no effect, but removing “a” hurt performance. Tiny tokens can still be doing work.

The lesson: compression is a tool for long, noisy contexts, not something to switch on everywhere.

Using a small model to summarise context

Pruning works on documents. For conversation history and agent memory, summarisation is usually the better fit, and a small model can do it well.

A common pattern for agents:

  1. Keep the last few turns verbatim.
  2. When history passes a threshold, send older turns to a small summariser model.
  3. Ask for a structured summary, not free prose: decisions made, facts established, open questions, and the user’s stated preferences.
  4. Store the summary and replace the old turns with it.

A structured summary prompt might look like this:

Summarise the conversation below for an AI assistant that will continue it.
Return exactly these sections:
- Goal: what the user is trying to achieve
- Decisions: what has been agreed, with key numbers
- Facts: details the assistant must not forget
- Open questions: what is still unresolved
Do not add information that is not in the conversation.

Structured output makes it much harder for the summariser to silently drop the one number that matters, and easier for you to check.

Why a small model? Summarising is a narrow, well-defined task that runs constantly. A small model is cheaper, faster, and can run on your own infrastructure — useful when the history contains data you don’t want to send to a third party. Many teams start with a general small instruct model and later fine-tune one on examples of good summaries produced by a larger model (we’ll cover that distillation process in a future post).

When compression backfires

Watch out for these:

  • Exact values. Prices, IDs, dates, code and legal wording must survive intact. Exclude them from pruning, or extract them into a separate “must keep” block.
  • Instructions. Never compress the system prompt or safety rules. Compress data, not directions.
  • Compounding loss. Summaries of summaries drift. Re-summarise from the original turns where you can.
  • Cache effects. If your provider caches long, stable prefixes, compressing them can increase cost by breaking the cache. Compress the changing parts instead.
  • Debugging. Keep the original, uncompressed context in your logs, so you can tell whether a bad answer came from the model or the compressor.

How to measure it

Before rolling out compression, run your evaluation set three ways: no compression, moderate, and aggressive. Track:

  • answer quality (your usual evals),
  • the rate of unsupported claims,
  • tokens in, latency and cost,
  • the compressor’s own latency and cost.

Pick the setting where quality holds and cost drops — and keep it per use case. A support bot and a legal summariser will land in very different places.

Where to start

  1. Log a week of real prompts and find the biggest contributors to token count. It is usually retrieved documents, tool output or history.
  2. Apply structural trimming first. It is free and safe.
  3. For long retrieved contexts, try a query-aware pruning method and compare on your eval set.
  4. For agent history, add structured summarisation with a small model, keeping recent turns verbatim.
  5. Re-measure monthly. Models change, and so does the right setting.

prompt compressioncontext compressionsummarisationsmall language modelsLLMLingua