What is prompt compression?
Prompt compression is the general name for methods that express the prompt sent to a language model in fewer tokens while keeping the information the task needs. The aim is faster, cheaper inference and fitting long content into the context window.
Unlike context compression, a wider family that also covers conversation history and internal caches, it shortens the prompt text itself; methods that trim internal caches cannot easily serve black-box models reached through an API.
- Topic
- Prompts, context, reasoning and generation
- Also known as
- Prompt shortening
- Origin
- 2023, Selective Context, gist tokens and LLMLingua
How prompt compression works
- Hard methods delete less informative tokens or sentences; the result is still text.
- Selective Context and LLMLingua decide what to drop using self-information or perplexity computed by a small language model.
- LLMLingua-2 decides to keep or drop each token with a Transformer encoder that sees bidirectional context.
- Soft methods such as gist tokens compress the prompt into a few learnt vectors and require training the model.
In more detail
Hard and soft prompt compression
A survey groups prompt compression techniques into hard prompt methods and soft prompt methods. Hard methods select or delete natural language tokens; soft methods compress the prompt into special tokens or vectors learnt by the model. The survey names combining the two approaches as a future research direction.
Commonly confused terms
| Term | How it differs |
|---|---|
| Prompt caching | Prompt caching stores the computation for a repeated prompt prefix and reuses it on later calls; the prompt content does not change. Prompt compression shortens the content itself, cutting tokens on every call but risking information loss. |
Why prompt compression matters for on-premises AI
Chain-of-thought prompting and in-context learning can push prompts past tens of thousands of tokens. LLMLingua reported up to 20x compression with little performance loss. At 2x to 5x compression LLMLingua-2 cut end-to-end latency by 1.6 to 2.9 times. Gist tokens achieved up to 26x compression and up to 40% fewer FLOPs.
An insurer's Retrieval-Augmented Generation-based assistant compresses long passages retrieved from policy documents before sending them to the model. Rules check that fields such as policy number and date survive; the compression ratio is chosen by comparing answers from compressed and full prompts on a fixed evaluation set.
Common questions
Does prompt compression reduce answer quality?
It can. Published methods report small losses at high ratios, but results depend on the task and data. The compression ratio is therefore chosen on the organisation's own evaluation set.
Can prompt compression be used with a model reached only through an API?
Yes. Hard methods that delete tokens still produce text, so they work with black-box models. Soft methods such as gist tokens require training the model.
Related terms
Sources
- LLMLingua: Compressing Prompts for Accelerated Inference of Large Language Models, EMNLP 2023
- LLMLingua-2: Data Distillation for Efficient and Faithful Task-Agnostic Prompt Compression, Findings of ACL 2024
- Learning to Compress Prompts with Gist Tokens, NeurIPS 2023
- Prompt Compression for Large Language Models: A Survey, 2024
In the glossary: Glossary, M to Q