Microsoft LLMLingua (EMNLP 2023, LongLLMLingua ACL 2024) est la référence académique de la compression par perplexité : un petit LM note chaque token, jusqu'à 20×. Headroom (chopratejas/headroom) prend la voie ingénierie—proxy local 8787, CCR réversible, routeurs par type, intégrations agent dont Runbook OpenClaw + Headroom.
Les équipes agent LLM always-on choisissent : PromptCompressor en Python (recherche) ou ANTHROPIC_BASE_URL vers Headroom (ops). Guide matrice de décision : élagage perplexité vs proxy CCR, headroom proxy --llmlingua, runbook 8 étapes.
Modèles de compression côte à côte
LLMLingua family (Microsoft Research)
Small LM (GPT-2-small / LLaMA-7B class)
→ token perplexity p(token | context)
→ drop low-perplexity tokens (budget controller + iterative passes)
→ optional distribution alignment to target LLM
LongLLMLingua adds:
→ contrastive perplexity p(question | document)
→ document reorder ("lost in the middle" mitigation)
→ coarse-to-fine compression for RAG stacks
Headroom (engineering stack)
Incoming messages (tools, logs, JSON, code, chat)
→ CacheAligner (KV-cache-friendly prefixes)
→ ContentRouter
├─ SmartCrusher (JSON arrays/objects)
├─ CodeCompressor (AST: Py/JS/Go/Rust/…)
└─ Kompress-base (agentic prose, HF model)
→ CCR stores originals locally; model calls headroom_retrieve
→ Proxy forwards /v1/messages to Anthropic/OpenAI/Bedrock
Citation : LLMLingua supprime par perplexité SLM ; Headroom route par type de contenu et garde un CCR réversible.
Matrice de décision : académique vs ingénierie
| Dimension | Microsoft LLMLingua / LongLLMLingua | Headroom |
|---|---|---|
| Primary mechanism | Perplexity + contrastive perplexity token pruning | Multi-algorithm ContentRouter + optional LLMLingua mode |
| Max cited compression | Up to 20× (paper); LongLLMLingua 4× with +21.4% NQ multi-doc QA | 60–95% on agent traces (SRE 65,694 → 5,118 tokens) |
| Reversibility | Lossy—dropped tokens gone unless originals kept | CCR default—verbatim retrieve on demand |
| Deployment | pip install llmlingua; embed in Python RAG | headroom proxy, wrap, library, MCP |
| Agent zero-code path | Requires pipeline integration | ANTHROPIC_BASE_URL=http://127.0.0.1:8787 |
| JSON / log tool dumps | Generic token pruning | SmartCrusher tuned for agent tool output |
| Query-aware RAG | LongLLMLingua strength | IntelligentContext + semantic similarity |
| Cold start / RAM | SLM + optional torch stack | ~1 GB default; +2 GB if --llmlingua |
| KV-cache compression | First-class research feature | CacheAligner for provider prefix stability |
| License | Microsoft Research, Apache-2.0 | Apache-2.0; optional --llmlingua |
Scénario A : RAG sur piles de documents
Profile: Legal, support, or internal wiki QA—10–50 PDFs chunked into a single prompt, user question appended.
LLMLingua fit: LongLLMLingua was built for this. Use condition_in_question="after_condition", reorder_context="sort", rate=0.55 per Microsoft's examples. Contrastive perplexity beats vanilla when documents are noisy.
Headroom fit: Strong when chunks mix JSON metadata + prose (ticket exports, CI logs in KB). Proxy mode compresses without rewriting LangChain/LlamaIndex glue.
If X, do Y: If bottleneck is multi-document ordering and lost-in-the-middle, do prototype LongLLMLingua first. If bottleneck is heterogeneous tool+json context in an agent loop, do prototype Headroom proxy first.
Scénario B : agents code/ops always-on (classe OpenClaw)
Profile: Nightly repo audits, MCP stdio tools, megabyte linter JSON—context grows every turn.
LLMLingua fit: Works as a pre-step if you batch-compress static prompts offline. Per-request compress_prompt() adds SLM inference latency on every gateway call unless cached.
Headroom fit: Designed for this shape—documented OpenClaw plugin, /stats Prometheus metrics, headroom mcp install. See Runbook OpenClaw + Headroom for LaunchAgent wiring.
If X, do Y: If you need drop-in proxy on macOS launchd gateways, do Headroom. If you publish research pipelines with frozen prompts, do LLMLingua in the ingest stage.
Scénario C : pile hybride
Headroom supports headroom proxy --llmlingua—Microsoft's perplexity compressor as an optional deeper pass after structural crushers. Trade-off: ~2 GB extra dependencies, 10–30s cold start per Headroom proxy docs.
If X, do Y: If eval shows SmartCrusher leaves >30% fat JSON, do enable --llmlingua on a 24 GB Mac mini M4 only. If latency SLO < 2s p95, do stay on structural crushers + CCR without ML pass.
Chemin recommandé
- If you optimize ACL-style RAG benchmarks, do start with LongLLMLingua
PromptCompressorand Microsoftratesweeps. - If you operate OpenClaw / Claude Code / Cursor fleets, do start with Headroom proxy and measure
/stats-historyfor seven nights. - If compliance requires verbatim audit trails, do prefer Headroom CCR over lossy perplexity-only pipelines.
- If you need KV-cache compression research, do evaluate LLMLingua-2 and Microsoft's cache line per Microsoft Research.
- If neither hits 40% savings on your traces, do fix prompt design first—compression cannot rescue redundant tool round-trips.
Runbook d'évaluation en 8 étapes
1. Geler un jeu de prompts doré
Capture N≥20 real agent turns: tool JSON, stack traces, instructions. Store SHA-256 per fixture under ~/compression-eval/fixtures/.
2. Compter les tokens de base (non compressés)
Record input tokens from provider dashboard or tiktoken for each fixture.
3. Exécuter le bras LLMLingua / LongLLMLingua
pip install llmlingua
from llmlingua import PromptCompressor
pc = PromptCompressor(model_name="microsoft/llmlingua-2-xlm-roberta-large-meetingbank")
out = pc.compress_prompt(prompt_list, question=question, rate=0.55,
condition_in_question="after_condition", reorder_context="sort",
rank_method="longllmlingua")
compressed = out["compressed_prompt"]
Log origin_tokens, compressed_tokens, wall-clock ms.
4. Exécuter le bras proxy Headroom
pip install "headroom-ai[proxy]"
headroom proxy --port 8787 --log-file ~/.headroom/eval.jsonl
POST fixtures through /v1/compress or route live agent traffic; read tokens_saved from /stats.
5. Bras hybride optionnel
headroom proxy --port 8788 --llmlingua --llmlingua-rate 0.3
Compare p95 latency vs savings uplift.
6. Porte qualité (même LLM aval)
Re-run each compressed fixture through your production model with identical temperature. Score: exact-match for structured fields, LLM-judge for summaries, human spot-check 5%.
7. Suite de régression agent
For OpenClaw operators: replay nightly audit job with each arm; compare finding counts and false-negative rate on known seeded bugs.
8. Choisir le gagnant par charge
Document: RAG ingest → LongLLMLingua, live gateway → Headroom proxy, max compression lab → hybrid—publish internally with token $/month math.
Dépannage
LLMLingua collapsed instruction-following
Symptôme : Compressed prompt drops negation or JSON keys.
Correctif : Lower rate (0.55 → 0.75). Use budget controller to exempt instruction block. Compare LLMLingua-2 per Microsoft Research.
Headroom proxy saves tokens but agent misses line numbers
Symptôme : Audit agent cites wrong file:line.
Correctif : Instruct model to headroom_retrieve before closing findings; set x-headroom-bypass: true on one repro. Narrow SmartCrusher if schema keys stripped.
Both arms slower than uncompressed
Symptôme : p95 latency > 3× baseline.
Correctif : LLMLingua—cache SLM on GPU/MPS, batch offline. Headroom—disable --llmlingua, keep structural crushers only; co-locate proxy on same host as gateway.
FAQ
Headroom est-il un fork de LLMLingua ?
Non. Headroom est un projet Apache-2.0 indépendant qui peut invoquer LLMLingua via --llmlingua. Le chemin par défaut utilise SmartCrusher, CodeCompressor et Kompress-base.
Quand l'élagage par perplexité bat-il les broyeurs par type de contenu ?
Quand les prompts sont du langage naturel homogène et que LongLLMLingua est réglé avec une question ancre. Les sorties d'outils agent hétérogènes favorisent le routeur Headroom.
LongLLMLingua dans OpenClaw sans Headroom ?
Oui—pré-compressez le contexte statique avec PromptCompressor dans les skills. Vous perdez la transparence proxy et le CCR sauf retrieval maison.
LLMLingua-2 vs LongLLMLingua ?
LLMLingua-2 reformule en classification de tokens avec encodeur BERT—3–6× plus rapide selon Microsoft. Headroom peut l'empiler via --llmlingua ; évaluez la vitesse vs SLO séparément.
Que doit approuver la finance pour 20 dépôts en audit nocturne ?
Évaluez huit étapes sur un dépôt semaine 1. JSON d'outils dominant → Headroom proxy + OpenClaw ; RAG documentaire statique → LongLLMLingua peut gagner en qualité/dollar.