Metadata-Version: 2.4
Name: minicahe
Version: 0.1.0
Summary: Minicahe - Mini Token Optimizer: talk smart, use fewer tokens
Author-email: "Vu Hoang Khoi Nguyen (toilanguyen2910)" <toilanguyen2910@example.com>
License: MIT
Project-URL: Homepage, https://github.com/toilanguyen2910/Minicahe
Project-URL: Repository, https://github.com/toilanguyen2910/Minicahe.git
Keywords: tokens,compression,llm,cli,tiktoken,openai,claude
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Operating System :: OS Independent
Classifier: Topic :: Text Processing :: Linguistic
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: click>=8.0
Requires-Dist: tiktoken>=0.5.0
Dynamic: license-file

# Minicahe 🪨

> **Extreme LLM Context Compressor** — Maximize your context window, minimize your costs.

Minicahe is a highly optimized, rule-based text compression tool inspired by [headroom](https://github.com/headroomlabs-ai/headroom), [codebase-memory-mcp](https://github.com/DeusData/codebase-memory-mcp), and [caveman](https://github.com/JuliusBrussee/caveman). 

By acting as a proxy layer before sending text to Large Language Models (LLMs), Minicahe aggressively strips away non-essential tokens while perfectly preserving the core semantic keywords.

🔥 **The Result:** Guaranteed **>50% token reduction** while maintaining **>90% precision quality**.

---

## ✨ Features

- 🚀 **Extreme Token Reduction**: Consistently halves your token usage (50%+ reduction on average).
- 🧠 **Smart Code Compression**: Dedicated mode for Python code to strip docstrings, comments, and empty lines without breaking AST.
- 🔄 **Auto-Acronymizer**: Automatically finds frequent long phrases and replaces them with acronyms.
- 🔑 **100% Keyword Preservation**: Ensures no critical domain-specific keywords are lost.
- ⚡ **Zero-Latency**: Pure Python string manipulation. No LLM calls required to compress text.
- 📊 **Tiktoken Integration**: Accurate token counting using OpenAI's `tiktoken` (with a fast fallback estimator).
- 🛠️ **CLI Ready**: Compress strings, files, or pipe data directly from your terminal.

## 🔬 How It Works (The Magic)

To achieve the impossible balance of halving token size while keeping LLM comprehension intact, Minicahe employs three extreme techniques in its `Aggressive` mode:

1. **Keyword Deduplication**: 
   If a long technical keyword (e.g., `transformer`, `architecture`) appears multiple times in your context, Minicahe keeps it once and drops the redundancies. The LLM still receives the exact vocabulary needed for its attention mechanism, but you save massive amounts of tokens.
   
2. **Auto-Acronymizer**: 
   Dynamically scans for frequent long N-grams (e.g. "natural language processing") and injects acronyms (e.g. "NLP") to save tokens without losing meaning.

3. **Extreme Lexical Trimming**: 
   Minicahe ruthlessly strips out all stop words `< 4` characters (`the`, `a`, `to`, `is`, `in`, `on`, etc.) and heavily filters longer filler words (`which`, `would`, `should`, `about`, `there`). LLMs are incredibly robust to broken grammar and can perfectly reconstruct the meaning from the remaining keyword salad.
   
4. **Whitespace Optimization**: 
   Removes unnecessary spaces around punctuation (e.g., `word ,` -> `word,`). Tiktoken tokenizes punctuation correctly without spaces, giving you cleaner outputs without generating garbage tokens.

## 🚀 Quick Start

### Installation

```bash
# Clone the repository
git clone https://github.com/toilanguyen2910/Minicahe.git
cd Minicahe

# Install from source
pip install -e .
```

### CLI Usage

```bash
# Compress an inline string (Aggressive Mode)
minicahe compress --aggressive "In the field of natural language processing, transformer-based models have become the dominant approach for a wide range of tasks."

# Compress a text file
minicahe compress --file data/long_log.txt --aggressive

# Compress a Python codebase file (Strips comments & docstrings)
minicahe compress --file src/main.py --code

# Pipe mode (Great for CI/CD or logging)
cat my_code.py | minicahe compress --code > compressed_code.py

# View your overall token savings stats
minicahe stats
```

## 📊 Benchmark

Minicahe includes a built-in strict benchmark (`tests/benchmark.py`) that evaluates compression based on precision (how well the compressed sequence matches the original without hallucinating).

```text
======================================================================
  MINICAHE BENCHMARK - TOKEN REDUCTION & QUALITY
======================================================================
Mode NORMAL:
  Avg Token Reduction: 4.4% 
  Avg Quality Score:   99.8% 

Mode AGGRESSIVE:
  Avg Token Reduction: 50.9% (🎯 TARGET MET >50%)
  Avg Quality Score:   95.3% (🎯 TARGET MET >90%)
======================================================================
```

## 🏗️ Architecture

```text
src/minicahe/
├── __init__.py          # Package initialization
├── cli.py               # Click CLI application
├── compressor.py        # Core Engine (Deduplication, Trimming, Phrase mapping)
├── code_compressor.py   # Specialized Python AST/Token compressor
├── tokenizer.py         # Token calculation (Tiktoken + fallback)
└── stats.py             # Global savings tracker
```

## 💡 Use Cases

- **RAG Pipelines**: Compress retrieved documents before injecting them into the LLM prompt. Fit double the documents in the same context window!
- **Agentic Memory**: Store massive logs and conversation history (like `codebase-memory-mcp`) at a fraction of the cost.
- **Codebase Analysis**: Feed entire codebases into Claude/GPT-4 by stripping out structural bloat and redundant keywords.

## ⚖️ License

MIT License. See `LICENSE` for more information.
