compression-methods.md

reference

← Back to skill

Content hash: 38c9a7c699dda5c46a10e012f8b820f12937c3a58307cce33c994975220650c5
## Prompt Compression Methods Reference

### The compression stack (highest to lowest leverage)

| # | Method | Compression | Quality risk | Cost |
|---|--------|------------|-------------|------|
| 1 | Manual restructuring | 20-40% | None | Free |
| 2 | Selective Context | ~50% | Low | Low |
| 3 | LLMLingua (token-level) | 5-10x | <2% degradation | Medium |
| 4 | LongLLMLingua (question-aware) | 4-20x | Negative (often IMPROVES) | Medium |
| 5 | LLMLingua-2 (fast) | 5-10x | <2% | Low |

### Manual restructuring (do this first, it's free)
```
Tighten system prompts
Cut repeated instructions
Deduplicate static blocks
Remove "please" / "could you" / padding
```

### What to compress (and what NEVER to)

| Compress | NEVER compress |
|----------|---------------|
| Retrieved RAG chunks | User's actual question |
| Conversation history | Task/instruction text |
| Few-shot examples (if many) | Key facts/entities |
| System prompt boilerplate | Output format constraints |

### LLMLingua vs LongLLMLingua

| Feature | LLMLingua | LongLLMLingua |
|---------|-----------|---------------|
| Question-aware | No | Yes |
| Reorders documents | No | Yes (fights position bias) |
| Best for | General compression | RAG specifically |
| Latency | Iterative (slower) | Conditional scoring |

### The ratio-accuracy curve
```
Ratio 2x:  nearly lossless
Ratio 4x:  very safe for RAG
Ratio 10x: needs eval validation
Ratio 20x: sharp quality cliff — measure carefully
```

### Cost calculator
```
monthly_cost = (tokens_per_request * requests_per_month * price_per_token)
savings = monthly_cost_raw - (monthly_cost_raw / compression_ratio)
```

### Faithfulness verification
After compression, run your eval set comparing:
- Answer with compressed context vs full context
- BERTScore / RAGAS faithfulness metric
- The goal: same-or-better quality, lower cost