|
| 1 | +# Kompact |
| 2 | + |
| 3 | +[](https://github.com/npow/kompact/actions/workflows/ci.yml) |
| 4 | +[](https://pypi.org/project/kompact/) |
| 5 | +[](https://www.python.org/downloads/) |
| 6 | +[](LICENSE) |
| 7 | +[](https://github.com/astral-sh/ruff) |
| 8 | + |
| 9 | +Multi-layer context optimization proxy for LLM agents. Reduces token usage by 40-70% with zero information loss. |
| 10 | + |
| 11 | +``` |
| 12 | + ┌──────────────────────────────────────────────┐ |
| 13 | + │ Kompact Proxy (:7878) │ |
| 14 | + │ │ |
| 15 | +Agent ─>│ 1. Schema Optimizer (TF-IDF selection) │─> LLM Provider |
| 16 | + │ 2. Content Compressors (TOON, JSON, code) │ |
| 17 | + │ 3. Extractive Compress (TF-IDF sentences) │ |
| 18 | + │ 4. Observation Masker (history mgmt) │ |
| 19 | + │ 5. Cache Aligner (prefix caching) │ |
| 20 | + │ │ |
| 21 | + └──────────────────────────────────────────────┘ |
| 22 | +``` |
| 23 | + |
| 24 | +## Quick Start |
| 25 | + |
| 26 | +```bash |
| 27 | +# Install |
| 28 | +uv sync |
| 29 | + |
| 30 | +# Start proxy |
| 31 | +uv run kompact proxy --port 7878 |
| 32 | + |
| 33 | +# Point your agent at it |
| 34 | +export ANTHROPIC_BASE_URL=http://localhost:7878 |
| 35 | +claude # or any Anthropic/OpenAI-compatible agent |
| 36 | +``` |
| 37 | + |
| 38 | +## How It Works |
| 39 | + |
| 40 | +Kompact is a transparent HTTP proxy. No code changes needed — just change your base URL. It intercepts LLM API requests, applies a pipeline of transforms to compress the context, then forwards the optimized request to the provider. |
| 41 | + |
| 42 | +| Transform | Target | Savings | Cost | |
| 43 | +|-----------|--------|--------:|------| |
| 44 | +| **TOON** | JSON arrays of objects | 30-60% | Zero (string manipulation) | |
| 45 | +| **JSON Crusher** | Structured JSON data | 40-80% | Minimal (Counter stats) | |
| 46 | +| **Code Compressor** | Code in tool results | ~70% | Regex parse | |
| 47 | +| **Log Compressor** | Repetitive log output | 60-90% | Regex dedup | |
| 48 | +| **Content Compressor** | Long prose/text | 25-55% | TF-IDF scoring | |
| 49 | +| **Schema Optimizer** | Tool definitions | 50-90% | TF-IDF cosine similarity | |
| 50 | +| **Observation Masker** | Old tool outputs | ~50% | Zero (placeholder swap) | |
| 51 | +| **Cache Aligner** | System prompts | Provider cache discount | Regex substitution | |
| 52 | + |
| 53 | +The pipeline adapts automatically — short contexts get light compression, long contexts get aggressive optimization. |
| 54 | + |
| 55 | +## Configuration |
| 56 | + |
| 57 | +```bash |
| 58 | +# Disable specific transforms |
| 59 | +uv run kompact proxy --port 7878 --disable toon --disable log_compressor |
| 60 | + |
| 61 | +# Verbose mode |
| 62 | +uv run kompact proxy --port 7878 --verbose |
| 63 | + |
| 64 | +# View live dashboard |
| 65 | +open http://localhost:7878/dashboard |
| 66 | +``` |
| 67 | + |
| 68 | +## Benchmarks |
| 69 | + |
| 70 | +6 synthetic scenarios (search, code, logs, schemas, conversation, mixed) evaluated with [context-bench](https://pypi.org/project/context-bench/). Each scenario has 3 needles that must survive compression. |
| 71 | + |
| 72 | +**Overall (6 scenarios, 18 examples):** |
| 73 | + |
| 74 | +| System | Compression | NIAH | Recall | Effective Ratio | Cost-of-Pass | |
| 75 | +|--------|------------:|-----:|-------:|----------------:|-------------:| |
| 76 | +| No Compression | 0.0% | 100% | 100.0% | 0.0% | 14,372 | |
| 77 | +| JSON Minification | 3.9% | 100% | 100.0% | 3.9% | 13,806 | |
| 78 | +| Truncation (50%) | 50.9% | 28% | 39.5% | -23.8% | 25,387 | |
| 79 | +| **Kompact** | **45.5%** | **83%** | **86.3%** | **33.7%** | **9,405** | |
| 80 | + |
| 81 | +**Highlights by scenario:** |
| 82 | + |
| 83 | +| Scenario | Kompact Compression | NIAH | Best Transform | |
| 84 | +|----------|--------------------:|-----:|----------------| |
| 85 | +| search_heavy (JSON) | 47.7% | 100% | TOON + JSON Crusher | |
| 86 | +| code_heavy (Python) | 81.8% | 100% | Code Compressor | |
| 87 | +| log_heavy (logs) | 22.2% | 100% | Log Compressor | |
| 88 | +| schema_heavy (tools) | 50.4% | 100% | Schema Optimizer | |
| 89 | +| conversation_heavy | 62.2% | 67% | Observation Masker | |
| 90 | +| mixed_realistic | 45.0% | 33% | All transforms | |
| 91 | + |
| 92 | +**Key metrics:** |
| 93 | +- **NIAH** (Needle In A Haystack) — did the answer substring survive compression? |
| 94 | +- **Effective Ratio** — retry-adjusted compression. If NIAH fails, you pay for both the failed attempt and the retry with full context. Negative = worse than no compression. |
| 95 | +- **Cost-of-Pass** — total output tokens / number of examples with recall >= 0.7. Lower = better. |
| 96 | + |
| 97 | +See [`benchmarks/README.md`](benchmarks/README.md) for full per-scenario results and methodology. |
| 98 | + |
| 99 | +```bash |
| 100 | +# Run synthetic scenarios |
| 101 | +uv run python benchmarks/run_comparison.py |
| 102 | + |
| 103 | +# Run on real datasets (BFCL, HotpotQA, Glaive, LongBench) |
| 104 | +uv run python benchmarks/run_dataset_eval.py --dataset bfcl -n 100 |
| 105 | +``` |
| 106 | + |
| 107 | +## Development |
| 108 | + |
| 109 | +```bash |
| 110 | +# Install with dev deps |
| 111 | +uv sync --extra dev |
| 112 | + |
| 113 | +# Run tests |
| 114 | +uv run pytest |
| 115 | + |
| 116 | +# Lint |
| 117 | +uv run ruff check src/ tests/ |
| 118 | + |
| 119 | +# Run single transform test |
| 120 | +uv run pytest tests/test_toon.py -v |
| 121 | +``` |
| 122 | + |
| 123 | +## Architecture |
| 124 | + |
| 125 | +``` |
| 126 | +src/kompact/ |
| 127 | +├── proxy/server.py # FastAPI proxy (Anthropic + OpenAI) |
| 128 | +├── parser/messages.py # Provider format ↔ internal types |
| 129 | +├── transforms/ |
| 130 | +│ ├── pipeline.py # Orchestration + adaptive scaling |
| 131 | +│ ├── toon.py # JSON array → tabular (TOON format) |
| 132 | +│ ├── json_crusher.py # Statistical JSON compression |
| 133 | +│ ├── code_compressor.py # Code → skeleton extraction |
| 134 | +│ ├── log_compressor.py # Log deduplication |
| 135 | +│ ├── content_compressor.py # Extractive text compression (TF-IDF) |
| 136 | +│ ├── schema_optimizer.py # TF-IDF tool selection |
| 137 | +│ ├── observation_masker.py # History management |
| 138 | +│ └── cache_aligner.py # Prefix cache optimization |
| 139 | +├── cache/store.py # Compression store + artifact index |
| 140 | +├── config.py # Per-transform configuration |
| 141 | +├── types.py # Core data models |
| 142 | +└── metrics/tracker.py # Per-request metrics |
| 143 | +``` |
| 144 | + |
| 145 | +## License |
| 146 | + |
| 147 | +MIT |
0 commit comments