|
| 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 | + |
| 7 | +Context compression proxy for LLM agents. Reduces token usage 40-70% with zero code changes. |
| 8 | + |
| 9 | +```bash |
| 10 | +uv run kompact proxy --port 7878 |
| 11 | +export ANTHROPIC_BASE_URL=http://localhost:7878 |
| 12 | +# That's it. Your agent now uses fewer tokens. |
| 13 | +``` |
| 14 | + |
| 15 | +## Results |
| 16 | + |
| 17 | +Evaluated on [BFCL](https://gorilla.cs.berkeley.edu/) (1,431 real API schemas), the standard benchmark for tool-calling agents. End-to-end through Claude via [claude-relay](https://github.com/npow/claude-relay), scored with [context-bench](https://pypi.org/project/context-bench/). |
| 18 | + |
| 19 | +### Does compression hurt quality? |
| 20 | + |
| 21 | +Quality impact vs no compression (positive = better, negative = worse): |
| 22 | + |
| 23 | +| Model | Kompact | [Headroom](https://github.com/headroom-ai/headroom) | [LLMLingua-2](https://github.com/microsoft/LLMLingua) | |
| 24 | +|-------|--------:|--------:|---------:| |
| 25 | +| Haiku | **+4.3%** | +0.7% | -25.9% | |
| 26 | +| Sonnet | -3.9% | -3.5% | -20.6% | |
| 27 | +| Opus | -0.5% | -0.5% | -27.3% | |
| 28 | + |
| 29 | +Kompact **improves** quality on Haiku — compression removes noise from tool schemas, helping the model focus. On Sonnet/Opus, all compressors are within a few percent of baseline. LLMLingua-2 destroys tool schemas regardless of model. |
| 30 | + |
| 31 | +### How much does it save? |
| 32 | + |
| 33 | +Offline compression measured on 12,795 examples across 3 datasets: |
| 34 | + |
| 35 | +| Dataset | Examples | Kompact | Headroom | LLMLingua-2 | |
| 36 | +|---------|----------|--------:|---------:|------------:| |
| 37 | +| BFCL (tool schemas) | 1,431 | **55.3%** | ~0% | 55.4% | |
| 38 | +| Glaive (tool calling) | 3,959 | **56.6%** | ~0% | ~50% | |
| 39 | +| HotpotQA (prose QA) | 7,405 | 17.9% | ~0% | 49.9% | |
| 40 | + |
| 41 | +Headroom's SmartCrusher doesn't compress JSON — it's designed for prose. LLMLingua-2 compresses aggressively but destroys information (see quality table above). |
| 42 | + |
| 43 | +### What does that mean in dollars? |
| 44 | + |
| 45 | +For a team running 1,000 agentic requests/day with ~10K token contexts: |
| 46 | + |
| 47 | +| Model | Without Kompact | With Kompact | Monthly Savings | |
| 48 | +|-------|----------------:|-------------:|----------------:| |
| 49 | +| Sonnet ($3/M) | $900/mo | $405/mo | **$495/mo** | |
| 50 | +| Opus ($15/M) | $4,500/mo | $2,025/mo | **$2,475/mo** | |
| 51 | +| GPT-4o ($2.50/M) | $750/mo | $338/mo | **$412/mo** | |
| 52 | + |
| 53 | +Savings scale linearly. 10K requests/day = 10x the numbers above. |
| 54 | + |
| 55 | +## How it works |
| 56 | + |
| 57 | +Kompact is a transparent HTTP proxy. It intercepts LLM API requests, compresses the context, then forwards to the provider. No agent code changes needed. |
| 58 | + |
| 59 | +``` |
| 60 | + ┌──────────────────────────────────────────────┐ |
| 61 | + │ Kompact Proxy (:7878) │ |
| 62 | + │ │ |
| 63 | +Agent ─>│ 1. Schema Optimizer (TF-IDF selection) │─> LLM Provider |
| 64 | + │ 2. Content Compressors (TOON, JSON, code) │ |
| 65 | + │ 3. Extractive Compress (TF-IDF sentences) │ |
| 66 | + │ 4. Observation Masker (history mgmt) │ |
| 67 | + │ 5. Cache Aligner (prefix caching) │ |
| 68 | + │ │ |
| 69 | + └──────────────────────────────────────────────┘ |
| 70 | +``` |
| 71 | + |
| 72 | +8 transforms, each targeting a different content type. The pipeline adapts automatically — short contexts get light compression, long contexts get aggressive optimization. Sub-millisecond overhead. |
| 73 | + |
| 74 | +## Running benchmarks |
| 75 | + |
| 76 | +```bash |
| 77 | +# Offline compression (no LLM calls, measures compression + needle preservation) |
| 78 | +uv run python benchmarks/run_dataset_eval.py --dataset bfcl |
| 79 | + |
| 80 | +# End-to-end quality (sends through proxy chain, measures LLM answer quality) |
| 81 | +# Requires: claude-relay running on :8084, kompact on :7878 |
| 82 | +uv run python benchmarks/run_e2e_eval.py --dataset bfcl --model haiku --workers 20 |
| 83 | +``` |
| 84 | + |
| 85 | +See [`benchmarks/README.md`](benchmarks/README.md) for full methodology. |
| 86 | + |
| 87 | +## Development |
| 88 | + |
| 89 | +```bash |
| 90 | +uv sync --extra dev |
| 91 | +uv run pytest # 48 tests |
| 92 | +uv run ruff check src/ tests/ |
| 93 | +``` |
| 94 | + |
| 95 | +## License |
| 96 | + |
| 97 | +MIT |
0 commit comments