Prompt Compression: Methods for Cutting Tokens Without Losing Output Quality

A stack of books on a table, representing how prompt compression condenses information into fewer tokens

Prompt compression is any technique that reduces the number of tokens in a prompt while keeping the information the model needs to do the task. It ranges from simple editing (cutting repetition, replacing prose with lists) to automated tools such as LLMLingua that drop low-value tokens, and to summarizing or clearing old conversation history. The goal is lower cost, lower latency and room in the context window, without a measurable drop in output quality.

That last condition is the hard part. Compression always removes something, and the question is whether the model needed it. This guide covers the main methods, a worked example of compressing a real-looking system prompt by hand, how to tell whether compression hurt quality, and how compression interacts with prompt caching, which can make a shorter prompt more expensive.

Why prompt length matters

Prompt length matters because API providers bill input by the token, longer inputs take longer to process, and every model has a maximum context window. A token is a chunk of text, roughly a short word or part of a longer word; tokenization explains how text is split. The context window is the total number of tokens a model can consider at once, covering instructions, documents, history and its own answer. A detailed explanation is in this guide to context windows.

There is also a quality reason. The 2023 paper “Lost in the Middle” by Liu and colleagues found that models answered questions best when the relevant information was at the beginning or end of a long input, and noticeably worse when it sat in the middle, even for models built for long contexts. Removing material that does not matter can make the material that does matter easier for the model to use.

The four main families of prompt compression

Prompt compression methods fall into four broad families, which differ in who does the compressing and what gets lost.

MethodHow it worksBest forMain risk
Manual editingA person rewrites the prompt to remove repetition, filler and unused instructionsSystem prompts and templates reused thousands of timesAccidentally deleting a rule that handled an edge case
Selective context (retrieval, filtering)Only the relevant chunks of a document or knowledge base are includedLong documents, knowledge bases, codebasesThe retriever misses the chunk that held the answer
SummarizationA model condenses long history or documents into a shorter summaryLong chats and agent runsExact figures, names and quotes disappear in the summary
Token-level compressionA small model scores tokens and drops the least informative onesLarge retrieved contexts where some loss is acceptableOutput is hard for humans to read and debug; unpredictable loss on precise tasks

Manual editing

Manual editing is the cheapest method to start with and often the most effective for prompts you send repeatedly. Long system prompts tend to accumulate the same rule written three ways, polite filler, background the model does not need and instructions for features that were removed months ago. Because a system prompt is sent on every call, trimming it pays off on every call.

Selective context

Selective context means sending only the parts of a large source that are relevant to the current question, rather than the whole thing. This is the core idea behind retrieval-augmented generation (RAG): split documents into chunks, find the chunks that match the query and include only those. It is compression by omission, and its quality depends entirely on retrieval quality.

Summarization and compaction

Summarization replaces long material with a shorter version written by a model. In chat and agent applications this is often called compaction: when the conversation gets too long, earlier turns are summarized and the summary replaces them. Anthropic’s API offers this as a built-in feature: its context management documentation describes server-side compaction, which summarizes the conversation and replaces the full history, plus “context editing” strategies that clear old tool results or earlier thinking blocks once the context passes a threshold. The same documentation notes that compaction is less suitable when precise recall of early details is needed, which is the general weakness of summaries.

Token-level compression (LLMLingua and similar)

Token-level compression uses a smaller model to decide which individual tokens the large model can do without. The best-known research line is Microsoft’s LLMLingua. The original LLMLingua paper (2023) combined a budget controller, iterative token-level compression and alignment between the small and large models, and reported compression of up to 20x with little performance loss on the benchmarks it tested. Its successor, LLMLingua-2 (2024), treats compression as a token classification task using a bidirectional encoder model trained on data distilled from a large model; the authors report it is 3x to 6x faster than earlier compressors and reduces end-to-end latency by 1.6x to 2.9x at 2x to 5x compression.

Those are the authors’ results on their chosen benchmarks, not guarantees for your workload. Compressed text often looks like broken English, which makes failures hard to debug, and tasks that depend on exact wording (legal clauses, code, numbers) are where dropped tokens are most likely to cause errors.

Worked example: compressing a system prompt by hand

The example below is illustrative: a support-assistant system prompt of the kind that grows over time as different people add rules. It shows the before version, the after version and what was removed.

Before

You are a very helpful, friendly and professional customer support
assistant working for Acme, which is a company that makes project
management software that is used by teams all around the world.
It is very important that you always try your best to be helpful
to the customer and answer their questions as well as you possibly can.

Please always make sure you are polite. Always be polite and
respectful to customers, even if they are rude or upset.

When a customer asks about refunds, you should know that our refund
policy is that customers can get a full refund within 30 days of
purchase. After 30 days, refunds are not possible. Please make sure
to tell customers about the 30-day refund window if they ask about
refunds.

If you don't know the answer to something, please do not make
something up. It is really important not to make things up. Instead
you should tell the customer that you will escalate their question
to a human agent.

Please answer in the same language that the customer uses to write
to you. For example if they write in Spanish, answer in Spanish.

Note: we used to offer a Legacy plan but it was discontinued in 2024,
so you do not need to worry about it unless someone asks.

Keep your answers reasonably short if possible.

After

You are Acme's customer support assistant. Acme makes project
management software.

Rules:
- Stay polite and calm, including with upset customers.
- Refunds: full refund within 30 days of purchase; none after.
- Legacy plan: discontinued in 2024. Mention only if asked.
- If you are not sure of an answer, do not guess. Say you will
  escalate to a human agent.
- Reply in the customer's language.
- Keep replies under 120 words unless the customer asks for detail.

What changed and why each change is safe:

  • Duplicated rules merged. “Be polite” appeared twice and “don’t make things up” appeared twice. Repeating an instruction does not reliably make it stronger; stating it once, clearly, is enough.
  • Generic praise removed. “Very helpful, friendly and professional” and “try your best” describe default behavior and carry no task-specific information.
  • Policy kept word for word in substance. The 30-day refund rule is the one fact the model cannot infer, so it stays exact. Compression should never touch numbers, names or policy terms.
  • Vague rule made concrete. “Reasonably short if possible” became “under 120 words”. This is shorter and more precise, which is the ideal compression.
  • The example for language was dropped, because the instruction is unambiguous without it. Keep examples when the instruction alone would be misread.

The after version is roughly a third of the original length. Whether it is equally good is not something you can tell by reading it; you have to test it, as described below.

A reusable prompt for compressing prompts

A language model can do a first pass of manual-style compression for you, as long as you constrain what it may remove. This prompt is designed to produce a reviewable draft, not a final version:

You are editing a system prompt to make it shorter without changing
its behavior.

Rules for your edit:
1. Keep every fact, number, name, policy, threshold and output format
   exactly as written.
2. Merge instructions that say the same thing.
3. Remove filler, generic praise and politeness phrases aimed at the
   model ("please", "it is very important that").
4. Convert explanatory prose into short bullet rules where possible.
5. Do not add new rules or examples.

Return three sections:
<compressed_prompt> the new prompt </compressed_prompt>
<removed> a bullet list of everything you removed or merged </removed>
<at_risk> anything you removed that might have changed behavior in
an edge case </at_risk>

<original_prompt>
{{paste prompt here}}
</original_prompt>

The removed and at_risk sections exist so a human can audit the edit quickly. Rule 1 is the most important line: models compressing text tend to round numbers and generalize specific terms, which is exactly where behavior changes.

How to check that compression did not hurt quality

The only reliable way to know whether a compressed prompt is as good as the original is to run both on the same inputs and compare. A minimal process:

  1. Build a test set of 20 to 50 real inputs, deliberately including edge cases the removed text might have covered (refund on day 31, a rude customer, a question about the discontinued plan).
  2. Run both prompt versions on every input with the same model and settings.
  3. Score outputs blind, without knowing which version produced which, against a written rubric: correct facts, rules followed, format respected.
  4. Compare token counts from the API’s usage fields rather than estimating them.
  5. Ship only if quality holds. If an edge case breaks, restore the specific rule, not the whole original.

Compression and prompt caching: check the math first

Prompt compression and prompt caching both reduce input cost, but they can work against each other. Prompt caching bills repeated, identical prompt prefixes at a fraction of the normal price, often around a tenth on major APIs. If your 8,000-token system prompt is already served from cache on most requests, cutting it to 4,000 tokens saves far less than the raw token count suggests.

Dynamic compression can also break caching outright. Caching requires an exact match of the prefix; token-level compression or summarization that produces slightly different output each time changes the prefix on every request, so nothing is ever reused. Anthropic’s context editing documentation makes a related point about clearing thinking blocks: keeping them preserves the prompt cache, while clearing them frees context space. The practical rules:

  • Compress static content (system prompts, tool descriptions, reference documents) once, offline, and then cache the result.
  • Apply dynamic compression only to the variable part of the prompt that comes after the cached prefix.
  • When compacting a long conversation, expect the next request to pay full price for the new summary; compact in larger, less frequent steps rather than trimming a little every turn.

When not to compress

Compression is the wrong tool in several common situations:

  • The prompt is short. Saving 200 tokens on a 600-token prompt rarely matters next to output tokens, which are usually priced higher.
  • The task depends on exact text. Contract review, code changes and quoting sources need the original wording.
  • Examples are doing the real work. Few-shot examples often look like “extra” text but carry the format and edge-case behavior. Remove them only after testing.
  • The real problem is too much context. If you are sending an entire knowledge base on every request, better selection beats compression. Deciding what belongs in the context at all is the broader discipline of context engineering.

Sources

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top