Prompt Compression with Context-Aware Sentence Encoding for Fast and Improved LLM Inference

Barys Liskavets, Maxim Ushakov, Shuvendu Roy, Mark Klibanov, Ali Etemad, Shane K. Luke

Abstract

Large Language Models (LLMs) face significant computational challenges when processing long prompts, leading to high latency and increased operational costs. While existing token-level compression methods attempt to mitigate these issues, they often compromise semantic coherence and suffer from slow processing speeds due to autoregressive overhead. This paper introduces Context-aware Prompt Compression (CPC), a sentence-level approach that utilizes a novel context-aware encoder to filter irrelevant information. CPC maintains grammatical integrity and human readability while achieving up to 11x faster inference and superior accuracy compared to state-of-the-art token-level compressors across multiple benchmarks, including LongBench and ZeroSCROLLS.

Introduction

As LLMs are increasingly deployed for complex tasks like Retrieval-Augmented Generation (RAG) and few-shot learning, the prompts fed into them have grown massively in length. Long prompts are computationally expensive, slow to process, and increase API costs.

To address this, researchers have developed "prompt compression" techniques. Current state-of-the-art methods, such as the LLMLingua family, are primarily token-level compressors. They calculate the perplexity of individual tokens and discard those deemed unimportant. However, this approach has two major flaws: it often results in "token soup"—grammatically broken, non-coherent sentences that hamper LLM reasoning—and it introduces high latency because calculating token-level perplexity using autoregressive models is computationally slow.

Methodology: Context-Aware Prompt Compression (CPC)

CPC shifts the paradigm from token-level to sentence-level compression. Instead of deleting individual words, CPC identifies and removes entire irrelevant sentences, preserving the structural integrity of the remaining text.

Overview of the Context-aware Prompt Compression (CPC) framework, illustrating the pipeline from original context to sorted relevance selection.

The core innovation is a context-aware sentence encoder. Standard sentence encoders (like Sentence-BERT) embed sentences in isolation, which fails to capture the meaning of context-dependent phrases (e.g., anaphoric references like "He decided..."). To overcome this, the authors:

  1. Curated a Synthetic Dataset: They developed the Context-aware Question-Relevance (CQR) Dataset, using an LLM to generate (Context, Question, Positive Sentence, Negative Sentence) tuples with rigorous filtering to ensure relevance.
  2. Trained a Context-Aware Encoder: They fine-tuned a Mistral-7B model using LoRA in a contrastive learning setup. The model learns to align question embeddings with relevant sentences that are encoded alongside their surrounding context.
  3. Enabled Fast Inference: During inference, the prompt is split into sentences. The encoder processes the question and all context sentences in a single pass. Sentences are ranked by cosine similarity to the question, and the top $T$ sentences are retained to meet the desired token limit while maintaining their original sequence.

Key Results and Performance

CPC demonstrates significant improvements in both accuracy and efficiency over existing methods.

Accuracy and Generalization

On the LongBench dataset, CPC outperformed the previous state-of-the-art (LongLLMLingua) by 1.5% at a 2,000-token constraint. It also showed remarkable domain generalization, beating LLMLingua-2 by 6.8 points on keyword extraction (Krapivin-2009) and 1.35 points on medical summarization (PubMed).

Efficiency and Latency

One of the most striking advantages of CPC is its speed. Because it relies on a single forward pass and simple cosine similarity, it bypasses the slow, token-by-token probability calculations of prior methods.

Latency comparison between LongLLMLingua and CPC, showing CPC's linear and significantly lower processing time as token length increases.

As shown in the comparison above, CPC is up to 10.93x faster than LongLLMLingua. Its latency scales linearly and predictably with input length, whereas prior methods suffer from severe latency spikes depending on the text structure.

Discussion and Insights

  • Coherence is Crucial: By keeping full sentences intact, CPC ensures the compressed prompt remains human-readable. This allows the downstream LLM to leverage its pre-trained language understanding more effectively than when processing fragmented tokens.
  • Context-Awareness is Essential: The authors proved that using off-the-shelf semantic encoders fails at this task, scoring significantly lower. A sentence's relevance to a question is inextricably linked to its surrounding context.

Limitations and Future Work

While effective, CPC's reliance on synthetic data for training the encoder risks inheriting biases or hallucinations from the source LLM. Additionally, sentence-level compression is less granular than token-level methods; a selected sentence may still contain some redundant filler words. Future research may explore scaling the backbone encoders or hybrid approaches to further refine token efficiency without losing semantic coherence.

Summary updated

BibTeX
@inproceedings{liskavets2025prompt,
  author = {Barys Liskavets and Maxim Ushakov and Shuvendu Roy and Mark Klibanov and Ali Etemad and Shane K. Luke},
  title = {{Prompt Compression with Context-Aware Sentence Encoding for Fast and Improved LLM Inference}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2025},
  doi = {10.1609/aaai.v39i23.34639},
}
Figure