ashrit25/prompt-compression-agent
Prompt Compression Agent
๐ Live demo: ashrit25-prompt-compression-agent.hf.space

Hosted free on Hugging Face Spaces (CPU). The Space sleeps after ~48h idle โ the first request after sleeping takes ~30s to reload the model, then it's responsive.
Compresses verbose LLM prompts using LLMLingua-2 (local, zero API cost) and verifies meaning preservation with an OpenAI model (one API call per run).
Architecture
User โ FastAPI โ Agent Loop โ LLMLingua-2 (local compression)
โ OpenAI (gpt-4o-mini, verification)
โ WebSocket โ Browser UI (streaming trace)The agent (OpenAI gpt-4o-mini by default) drives a fixed tool sequence:
analyze_promptโ token/word counts, filler detection, redundancy estimatecompress_with_llmlinguaโ local compression at a target retention ratioverify_meaningโ the OpenAI model scores 0โ100 how well intent is preservedcheck_token_reductionโ exact tiktoken token savings
Retry rules adapt the compression ratio based on the verification score and realized reduction, up to 4 compression attempts.
Setup
git clone https://github.com/ashritkvs/prompt-compression-agent
cd prompt-compression-agent
pip install -r requirements.txt
cp .env.example .env
# Add your OPENAI_API_KEY to .env (optionally set OPENAI_MODEL)
uvicorn api.main:app --reload
# Open http://localhost:8000The first start downloads and loads the LLMLingua-2 model (a few hundred MB); it is loaded once at startup via the FastAPI lifespan and reused for every request.
Deploy to Render
- Push the repo to GitHub.
- New Web Service on Render โ connect the repo.
- Runtime: Docker.
- Add env var:
OPENAI_API_KEY(and optionallyOPENAI_MODEL). - Deploy. The health check at
/healthreturns 503 until the model finishes loading, then 200.
Run benchmark
python benchmark/run_benchmark.pyRuns 20 fixed prompts (verbose โ concise), prints a table + summary, and saves benchmark/benchmark_results.json.
Run tests
pytest tests/Network-dependent tests (real OpenAI calls) are skipped automatically when OPENAI_API_KEY is not set.
API
Docs: http://localhost:8000/docs
POST /compress
curl -X POST http://localhost:8000/compress \
-H "Content-Type: application/json" \
-d '{"prompt": "Could you please help me understand what ML is?"}'GET /health
{
"status": "ok",
"version": "1.0.0",
"compression_model": "llmlingua-2",
"verification_model": "gpt-4o-mini"
}GET /metrics
Aggregate stats across all runs (in-memory): average reduction, meaning score, steps, tokens saved, and success rate.
WebSocket /ws/compress
ws://localhost:8000/ws/compress
Send: {"prompt": "your verbose prompt"}Streams {"type": "step", ...} messages as each tool executes, then a final {"type": "complete", "result": ...} payload.
Models
- Compression:
microsoft/llmlingua-2-bert-base-multilingual-cased-meetingbank(local CPU) - Agent + verification:
gpt-4o-miniby default (OpenAI API); override withOPENAI_MODEL
