Skip to main content

A lightweight pre-processor to minimize LLM token usage

Project description

💻 TokenSaver

PyPI version
License: MIT
Hugging Face Space


📖 Overview

TokenSaver is a lightweight Python library + CLI tool to minimize LLM token usage before sending prompts to GPT, Claude, Mistral, etc.

Every unnecessary newline, space, or repeated entity increases your LLM bill. TokenSaver preprocesses text and JSON to make them as compact as possible — cutting costs without losing meaning.


✨ Features

  • 🧹 Text Cleaning → collapse whitespace, normalize newlines
  • 📦 JSON Minify → compact JSON into a single line
  • 🏷 Entity Compression → replace long repeated names/entities with short placeholders

📦 Installation

pip install tokensaver

🚀 Usage

1. Python API

from tokensaver import clean_text, minify_json, compress_entities

# --- Text Cleaning ---
messy = "Hello     World   \n\nThis   is   a test.   "
print(clean_text(messy))
# -> "Hello World\nThis is a test."

# --- JSON Minify ---
pretty_json = """{
  "name": "John Doe",
  "age": 30,
  "skills": [
    "Python",
    "AI"
  ]
}"""

print(minify_json(pretty_json))
# -> {
#   'result': {'name': 'John Doe', 'age': 30, 'skills': ['Python', 'AI']},
#   'minified': '{"name":"John Doe","age":30,"skills":["Python","AI"]}'
# }

# --- Entity Compression ---
entity_text = """The International Business Machines Corporation 
makes enterprise software. International Business Machines Corporation 
is also known as IBM."""

print(compress_entities(entity_text))
# -> {
#   'compressed': 'E1 makes enterprise software. E1 is also known as IBM.',
#   'mapping': {'International Business Machines Corporation': 'E1'}
# }

2. CLI Usage

When installed, TokenSaver also provides a command-line tool:

# Clean messy text
tokensaver clean "Hello     World"
# -> "Hello World"

# Minify JSON
tokensaver json '{"name":"John","age":30}'
# -> {"result": {"name": "John", "age": 30}, "minified": "{\"name\":\"John\",\"age\":30}"}

# Compress repeated entities
tokensaver entities "International Business Machines Corporation makes software. International Business Machines Corporation sells software."
# -> {"compressed": "E1 makes software. E1 sells software.", "mapping": {"International Business Machines Corporation": "E1"}}

🧑‍💻 Why TokenSaver?

Every token costs 💰. Formatting intended for humans (pretty-printed JSON, repeated names, redundant whitespace) doesn’t help AI models — but does increase token count.

TokenSaver helps you:

  • Minimize cost for API calls
  • Preprocess logs, transcripts, or JSON before sending to LLMs
  • Keep your pipelines fast & efficient

🔗 Links


📝 License

This project is licensed under the MIT License – see the LICENSE file for details.

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

tokensaver-0.1.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

tokensaver-0.1.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file tokensaver-0.1.1.tar.gz.

File metadata

  • Download URL: tokensaver-0.1.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.6

File hashes

Hashes for tokensaver-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2359817044ac3b3c48047a0558edf87fdb3bff06a674bad35542db4b8cc65547
MD5 465dc4d01e3f9ca6580d0ebc6a0eafa5
BLAKE2b-256 71c3ca3816622145e7eb4b242d789bc09d7694041a12720644fc923a364923e5

See more details on using hashes here.

File details

Details for the file tokensaver-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: tokensaver-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.6

File hashes

Hashes for tokensaver-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b1bc4349c9499de9eee1e1c2cadf1b92b14e18229792a765029517aecbb6ef38
MD5 ca9da044d00eda09f138876e58272996
BLAKE2b-256 6b52d3d952a67b6fe670158cbce007b8305b372598533c036e42dbe5260c1770

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