Prompt compression is the technique of algorithmically removing non-essential tokens from a prompt while preserving its semantic intent. The goal is to reduce the number of tokens sent to an LLM — cutting costs, reducing latency, and sometimes even improving output quality by eliminating noise. The most prominent family of algorithms in this space is Microsoft Research’s LLMLingua, which can achieve up to 20x compression ratios with minimal performance degradation.
The motivation is simple arithmetic: if your RAG pipeline retrieves 15,000 tokens of context but only 4,000 of those tokens are truly relevant to the query, you’re paying 3.75x more than necessary — and you’re diluting the model’s attention across irrelevant material. Prompt compression sits between retrieval and generation, acting as an intelligent filter that strips away filler words, redundant phrasing, and low-information-density passages.
Unlike simple truncation (which blindly cuts from the end) or summarization (which requires an expensive LLM call), prompt compression operates at the token level using small, fast models. A 335M-parameter BERT-class model can compress a prompt in milliseconds, making it practical as a pipeline stage even in latency-sensitive applications.
The original LLMLingua algorithm (EMNLP 2023) is built on an elegant insight: tokens that a small language model finds unsurprising (low perplexity) are likely redundant and can be safely removed. A token like “the” before “transformer architecture” adds almost no information — the model expects it — so it can be dropped. Conversely, “HNSW” in a sentence about vector databases has high perplexity (the model didn’t predict it) and must be preserved.
Perplexity measures how surprised a language model is by a token given its preceding context. Low perplexity = predictable = removable. High perplexity = surprising = important. LLMLingua uses a small model (e.g., LLaMA-7B) to compute per-token perplexity scores, then removes tokens below a threshold to achieve the target compression ratio.
The original LLMLingua uses a coarse-to-fine three-module pipeline:
LLMLingua-2 (ACL 2024) fundamentally rethinks the approach. Instead of using perplexity as a proxy for importance, it directly trains a model to classify each token as “keep” or “discard”:
LLMLingua-2 uses GPT-4 to generate training data: given an original prompt and its task output, GPT-4 identifies which tokens were essential. This labeled data trains a lightweight BERT-class encoder (~335M parameters) to predict token importance using bidirectional context — a major improvement over the unidirectional perplexity approach of v1.
Key improvements in LLMLingua-2:
LongLLMLingua extends the family specifically for long-context RAG scenarios. It adds a question-aware compression step: rather than compressing documents independently, it scores token importance relative to the user’s query. Tokens that are semantically related to the query are preserved even if they have low perplexity, while tokens that are predictable and query-irrelevant are aggressively pruned.
from llmlingua import PromptCompressor
compressor = PromptCompressor(
model_name="microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank",
device_map="cpu"
)
# Retrieved chunks from vector search
retrieved_context = [chunk1, chunk2, chunk3, chunk4, chunk5]
full_prompt = system_prompt + "\n".join(retrieved_context) + user_query
# Compress to fit budget
compressed = compressor.compress_prompt(
full_prompt,
instruction=system_prompt, # preserve instructions
question=user_query, # preserve the query
target_token=4000, # target compressed size
context_budget="+200", # allow slight overflow
dynamic_context_compression_ratio=0.3 # 70% compression on context
)
# Send compressed prompt to LLM
response = llm.generate(compressed["compressed_prompt"])
# Original: 15,000 tokens → Compressed: 4,000 tokens
# Cost savings: 73% | Latency reduction: ~60%
For a system handling 100,000 queries/day with an average prompt size of 12,000 tokens, even 5x compression saves significant costs. At GPT-4’s input pricing (~$10/M tokens), compression from 12K to 2.4K tokens per query saves roughly $960/day — over $28,000/month. The compression model itself runs on CPU and costs a fraction of a cent per call.
LLM inference time scales roughly linearly with input token count. Compressing 20K tokens to 5K tokens can reduce time-to-first-token by 40-60%, which is critical for real-time chat interfaces, voice assistants, and search experiences where users expect sub-second responses.
When using models with smaller context windows (4K-8K tokens), prompt compression enables you to fit the equivalent of 40K tokens of retrieved context into the available space. This is particularly valuable when using cost-effective smaller models that have tight context limits but sufficient reasoning capability for the task.
Prompt compression is not appropriate for every use case:
<pre> blocks.