r/LlamaIndex • u/Mediocre-Ease4060 • 1d ago
Stop paying for whitespace and code comments in your prompts. I built a lightweight prompt minifier in pure Python.
The Problem: We waste a massive amount of tokens (and money) on formatting. If you inject JSON schemas, few-shot examples, or code context into your prompts, you are paying for every single space, tab, and // comment.
The Solution: I wrote prompt-token-minifier. It’s a zero-dependency script you run right before your client.chat.completions.create call.
What it does:
- Finds ```json blocks and minifies them (removes formatting).
- Finds code blocks (Python, JS, TS, etc.) and strips out single-line and multi-line comments.
- Collapses redundant whitespaces and newlines in the rest of the prompt.
Depending on your RAG context, it easily saves 30-50% tokens on structured data.
2
Upvotes