Compress natural-language instructions into compact PCM format before they reach your upstream LLM. Code blocks, documents, and other payloads pass through unchanged.
PCM is a pre-processing layer that sits between your application and your LLM API. It intercepts natural language instructions and compresses them into a token-efficient format, then forwards the optimized prompt to your LLM provider.
Client → PCM Proxy (:8090) → Mistral / OpenAI-compatible API
↓
Ollama (local compressor: granite4.1:3b | pcm-granite)
Natural language prompts are often verbose and repetitive:
"Please review this Python code very carefully for any potential race conditions,
memory leaks, or optimization opportunities that might exist. Then provide
a detailed Markdown report that lists all issues found, ordered by their
severity level from most critical to least important."
This is 48 tokens of instruction that could be conveyed in far fewer.
PCM transforms the above into:
TASK=review INPUT=python CHECK=race,leak,perf FORMAT=markdown ORDER=severity
22 tokens — a 54% reduction while preserving all semantic meaning.
| Case | Original | Compressed | Savings |
|---|---|---|---|
| Dev triage prompt | 160 tokens | 31 tokens | 81% |
| Code review request | 85 tokens | 24 tokens | 72% |
| Data analysis ask | 120 tokens | 45 tokens | 63% |
| General instructions | 48 tokens | 22 tokens | 54% |
Full message savings (instruction + payload):
See PCM + COE guide for detailed benchmarks.
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Client │────▶│ PCM Proxy │────▶│ LLM API │
│ (your app) │ │ (:8090) │ │ (Mistral, etc)
└─────────────┘ └─────────────┘ └─────────────┘
│
▼
┌─────────────┐
│ Ollama │
│ (compressor)│
└─────────────┘
✅ Instructions — Natural language tasks and requests
✅ System prompts — Long contextual instructions
❌ Code blocks — Pass through verbatim
❌ Documents — Pass through verbatim
❌ JSON data — Pass through verbatim
# Clone and start
git clone https://github.com/ntnglz/Prompt-Compression-Middleware.git
cd Prompt-Compression-Middleware
cp .env.example .env # Set your MISTRAL_API_KEY
docker compose up --build
# Test it
curl http://localhost:8090/health
# Use it
curl http://localhost:8090/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"messages": [{"role": "user", "content": "Review this code..."}]}'
pip install -e ".[dev]"
python run.py --proxy
# Listening on :8090
python run.py --http
# POST /compress endpoint
curl -X POST http://localhost:8000/compress \
-H "Content-Type: application/json" \
-d '{"text": "Review this Python code for race conditions..."}'
from pcm.compressor import compress_instruction
original = "Review this Python code carefully for race conditions..."
compressed = compress_instruction(original)
print(compressed) # TASK=review INPUT=python CHECK=race FORMAT=markdown
For integration with Cursor, Claude Desktop, and other AI tools:
# Generate Cursor config
python scripts/mcp/print_cursor_config.py
# Run MCP server
python run.py --stdio # For stdio
python run.py --mcp-http # For HTTP MCP (:8001/mcp)
| Scenario | Recommended | Reason |
|---|---|---|
| Prompts > 12 tokens | ✅ Yes | Threshold for activation |
| Long, verbose instructions | ✅ Yes | High savings potential |
| Already in PCM format | ❌ Skip | Detects TASK= prefix |
| Short prompts (< 12 tokens) | ❌ Skip | Below threshold |
| No upstream LLM | ⚠️ Use REST/MCP | Compression only mode |
TASK= are left as-isPOST /compress or MCP if you only need compressionPCM adds informative headers to responses:
| Header | Description |
|---|---|
X-PCM-Messages-Compressed |
Number of messages compressed |
X-PCM-Compression-Ratio |
Token savings ratio (0 if skipped) |
X-PCM-Tokens-Saved |
Input tokens saved |
X-PCM-Compression-Time-Ms |
Compression processing time |
X-PCM-Upstream-Provider |
Provider (mistral, openai, etc.) |
X-PCM-Upstream-Model |
Model used upstream |
Disable per request: Add header x-pcm-disable: true
# Fastest (no Ollama, no API key):
git clone https://github.com/ntnglz/Prompt-Compression-Middleware.git
cd Prompt-Compression-Middleware
pip install -e ".[dev]"
python run.py --demo-stub
# Production (Docker + upstream):
cp .env.example .env # Set MISTRAL_API_KEY
docker compose up --build
curl http://localhost:8090/health
| Aspect | Details |
|---|---|
| Language | Python 3.11+ |
| License | Research prototype |
| Status | Active development |
| Stars | GitHub |
| Documentation | Full docs |
| Pair with | Context Optimization Engine (COE) |
Compress long instructions in IDE plugins, code review bots, and automated testing systems.
Optimize detailed support ticket analysis prompts.
Compress complex analysis requests while preserving data payloads.
Reduce token usage in conversational AI systems with long system prompts.
┌─────────────────────────────────────────────────────────────┐
│ Your Application │
└─────────────────┬───────────────────────────────────┬───────┘
│ │
▼ ▼
┌─────────────────────────────────────────────────────────────┐
│ PCM Proxy (:8090) │
│ ┌─────────────┐ ┌─────────────┐ ┌────────┐ │
│ │ Receive │ │ Compress │ │Forward │ │
│ │ Request │────────▶│ Instruction │────────▶│ to LLM│ │
│ └─────────────┘ └─────────────┘ └────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────┐ │
│ │ Local Compressor │ │
│ │ (Ollama + Model) │ │
│ └─────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Upstream LLM API │
│ (Mistral, OpenAI, etc.) │
└─────────────────────────────────────────────────────────────┘
Research prototype — use at your own risk. Not production-ready.