Skip to main content

Context compression sidecar for LLM applications. Reduce token costs 3-5x before calling any AI API.

Project description

๐Ÿ—œ๏ธ llm-zip

Version PyPI License Python FastAPI Docker

LLMLingua-2 MarkItDown LiteLLM Ruff

Context compression sidecar for LLM applications.
Reduce token costs 3โ€“5ร— before calling any AI API.

Why ยท How it works ยท Quickstart ยท CLI ยท API response ยท File support ยท Benchmarks ยท Limitations ยท Contributing


Why llm-zip exists

AI inference is getting expensive. As context windows grow and agentic workflows multiply, the tokens you send to GPT, Claude, or Gemini compound fast โ€” and the "free ride" subsidy era is ending.

llm-zip was built out of a simple need: compress context before it reaches the model, without changing anything else in the stack. No proxy. No middleware. No API keys. Just a sidecar you call over HTTP, that hands back a smaller text and tells you exactly how much you saved.

It started as an internal tool. It's open source because the problem is universal.

Curious about how this started? Read STORY.md.


How it works

Your app  โ”€โ”€โ†’  POST /v1/compress  โ”€โ”€โ†’  llm-zip
                                           โ”‚
                              compresses with LLMLingua-2
                              scores semantic preservation
                              calculates USD savings
                                           โ”‚
              compressed text + metrics  โ—„โ”€โ”˜

Your app  โ”€โ”€โ†’  calls OpenAI / Anthropic / Gemini
               with the smaller context

llm-zip never touches your API keys or your model calls. It only compresses.


Quickstart

Requires Docker. First-time model download is ~700MB and takes 2โ€“5 min.

Step 1 โ€” Configure

Windows (CMD / PowerShell):

copy .llmzip.config.example .llmzip.config
notepad .llmzip.config

Linux / macOS:

cp .llmzip.config.example .llmzip.config
nano .llmzip.config

Step 2 โ€” Download models (only on first run)

docker-compose run llmzip download-models

Step 3 โ€” Start

docker-compose up -d

โ†’ API: http://localhost:8000 ยท Docs: http://localhost:8000/docs


The CLI Experience

llm-zip isn't just an API โ€” it comes with a UNIX-friendly CLI powered by Typer and Rich. It supports stdin pipes, outputs clean JSON, and tracks live model prices.

# Check current prices (fetches live from LiteLLM)
$ llmzip prices -p anthropic

Rates from LiteLLM as of 2026-06-05
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”“
โ”ƒ Model                                     โ”ƒ Input $/M โ”ƒ Output $/M โ”ƒ
โ”กโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฉ
โ”‚ anthropic.claude-3-5-sonnet-20241022-v2:0 โ”‚    3.0000 โ”‚    15.0000 โ”‚
โ”‚ anthropic.claude-3-haiku-20240307-v1:0    โ”‚    0.2500 โ”‚     1.2500 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Model names come directly from LiteLLM and may use provider-specific naming (e.g. Bedrock-style). The exact list depends on live data at execution time.

# Compress directly from the terminal via pipes
$ cat huge_context.txt | llmzip compress --ratio 0.4 --model gpt-4o-mini > compressed.txt
โœ“ 24000 โ†’ 9600 tokens (2.5ร—) | score: 0.92 | saved: ~$0.0021 (gpt-4o-mini)

What you get back

{
  "compressed": "...",
  "original_tokens": 8400,
  "compressed_tokens": 1680,
  "compression_ratio": 5.0,
  "preservation_score": 0.91,
  "estimated_savings": {
    "gpt-4o-mini":           "$0.0010",
    "claude-haiku-4-5":      "$0.0013",
    "gemini-2.5-flash-lite": "$0.0006"
  },
  "skipped": false,
  "warning": null
}

Prices are fetched live from LiteLLM with local caching โ€” no manual database updates needed.


MarkItDown Integration

One of the practical problems that motivated llm-zip was RAG over internal documents. In private systems โ€” invoices, reports, manuals, internal specs โ€” the source content lives in PDFs, Word files, and spreadsheets.

llm-zip integrates MarkItDown as an optional preprocessing layer. When FILE_CONVERSION=true, you can send a file directly to /v1/compress/file and get back compressed text ready for your RAG pipeline.

curl -X POST http://localhost:8000/v1/compress/file \
  -F "file=@invoice.pdf" \
  -F "ratio=0.5"

Supported formats: PDF, Word (.docx), Excel (.xlsx), PowerPoint (.pptx), and more.


Key Features

Feature Detail
๐Ÿ“„ Text and files Compress plain text or upload PDFs, Word, Excel, and PowerPoint directly
๐Ÿช“ Smart Chunking Handle documents of any length by splitting text into chunks based on paragraphs
๐Ÿ–ฅ๏ธ CPU-Friendly Default model requires only ~700MB of RAM โ€” no GPU needed
๐ŸŒ Global i18n CLI in English, Spanish, Portuguese, Chinese, and Japanese
โš™๏ธ Configurable thresholds Define when llm-zip should and shouldn't intervene (MIN_TOKENS_TO_COMPRESS)
๐Ÿšซ Ignore rules Exclude texts or system prompts via .llmzipignore and .llmzipignore.local
๐Ÿ”’ Self-hosted Your data never leaves your infrastructure

Environment Variables

For CI or Docker deployments, you can override defaults with environment variables:

Variable Description
LLMZIP_LANG Forces CLI language (en, es, pt, zh, ja). Overrides system locale.

๐Ÿ“Š The Science & Benchmarks

llm-zip is powered by Microsoft's LLMLingua-2. According to their research paper evaluated on the LongBench dataset:

  • Compression sweet spot: optimal performance between 2ร— and 5ร— compression
  • Quality retention: 90โ€“98% Exact Match performance vs. uncompressed prompts
  • Latency reduction: end-to-end latency improvement of 1.6ร— to 2.9ร— (task-agnostic, no prompt needed to start)

๐Ÿงช Internal Test Results

The following results were obtained by running llm-zip against real documents during development. They are not synthetic benchmarks โ€” they reflect actual compression output on representative material.

How savings are calculated: tokens_saved ร— input_price_per_token, using live prices fetched from LiteLLM at run time. For OpenAI models, tokenization is exact (tiktoken). For Anthropic, Google and DeepSeek, a character-ratio heuristic is used (ยฑ10% margin). Only input token savings are counted โ€” output token reduction, which also occurs in many workflows, is not included. The figures below are approximate lower-bound estimates: real savings may be higher or lower depending on your actual usage pattern and the model you call.

Document Lang Ratio Tokens In Tokens Out Compression Preservation Est. Savingยน
A Survey of LLMs (100+ p.) EN def 296,726 172,684 1.72ร— 0.9564 ~$0.398
Attention Is All You Need EN 0.5 11,993 6,240 1.92ร— 0.9087 ~$0.016
Generic customs manualยฒ (Technical) ES 0.9 11,996 4,765 2.52ร— 0.8942 ~$0.018

ยน Estimated saving for claude-sonnet-4-6 input tokens only. See methodology note above. ยฒ Synthetic test dataset modeled after Argentine customs documentation (Cรณdigo Aduanero, NCM, ARCA/DGA). Not a real operational document.

๐Ÿ“‹ Hardware benchmarks (click to expand)

Community-maintained โ€” no verified numbers yet. If you run llm-zip on real hardware, please submit your results via PR.

Hardware Model Tokens Time
โ€” โ€” โ€” โ€”
๐Ÿ“‹ Use-case benchmarks (click to expand)

Community-maintained โ€” no verified numbers yet. If you use llm-zip in a real workload, please submit your results via PR.

Use case Tokens In Ratio Preservation Est. Saving
โ€” โ€” โ€” โ€” โ€”

Limitations & Caveats

  • Language support โ€” LLMLingua-2 was trained on English meeting transcripts. Spanish and other languages work but may see 10โ€“15% lower compression quality. Community benchmarks are welcome.
  • Extractive compression โ€” tokens are removed, not rewritten. Some nuance may be lost at aggressive ratios (above 0.7).
  • Pricing accuracy โ€” savings are estimates. Non-OpenAI tokenizers use a character-ratio heuristic with a ยฑ10% margin of error.
  • Format loss โ€” file conversion extracts plain text only. Layout, tables, and complex formatting are not preserved.
  • Production testing โ€” this is v0.1.0. Concurrent batch processing should be monitored closely in high-throughput environments.

Contributing

The most valuable contributions right now are benchmark results across different languages, domains, and models. If you use llm-zip in a real project, a PR with your numbers helps everyone calibrate expectations.

Bug reports, feature requests, and code contributions are also welcome โ€” see CONTRIBUTING.md.

git clone https://github.com/finktech-dev/llm-zip.git
cd llm-zip
pip install -e ".[dev]"
pytest

License & Disclaimer

MIT ยฉ FinkTech

Built utilizing LLMLingua-2 and MarkItDown open-source libraries.

This project is an independent, community-driven open-source tool. It is not affiliated with, endorsed by, or sponsored by Microsoft Corporation, OpenAI, Anthropic, or any other LLM provider. All trademarks and registered trademarks are the property of their respective owners.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llm_zip-0.1.9.tar.gz (36.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_zip-0.1.9-py3-none-any.whl (42.6 kB view details)

Uploaded Python 3

File details

Details for the file llm_zip-0.1.9.tar.gz.

File metadata

  • Download URL: llm_zip-0.1.9.tar.gz
  • Upload date:
  • Size: 36.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for llm_zip-0.1.9.tar.gz
Algorithm Hash digest
SHA256 6a021b04a484e31463c5bfcd79ba0ff8d81cdc37d22847decd8a365717e5298a
MD5 59f0915fe4e0139c5449e1252e9bafb9
BLAKE2b-256 1bbb37a35ecf25df57ea836ceb6cb89447c045bbfb0f3ede74352531bb353ddd

See more details on using hashes here.

File details

Details for the file llm_zip-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: llm_zip-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 42.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for llm_zip-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 b45815a950b582c5706c6abc28fef68c3e7fb3aebdcd783084ced81aeeedf389
MD5 73ee5563367fbe7d3ffc324157f42509
BLAKE2b-256 61ad1b5cdf848b903b4047d07e344e59fb8c9da6ee7982aa93c3182c6651a182

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page