A zero-dependency Python micro-tool to shrink your LLM prompts before sending them to the API. It saves tokens and reduces costs by:
- Minifying JSON schemas and examples inside Markdown blocks.
- Stripping comments (
//,#,/* */) from code blocks. - Collapsing redundant whitespaces and newlines.
pip install prompt-token-minifierfrom prompt_token_minifier import minify_prompt
raw_prompt = """
You are a helpful assistant.
Here is the JSON schema you must follow:
{
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
And some context code:
# This function calculates the sum
def add(a, b):
return a + bprint(minify_prompt(raw_prompt))Output: (Saves ~40% tokens depending on formatting)
You are a helpful assistant.
Here is the JSON schema you must follow:
{"type":"object","properties":{"name":{"type":"string"}}}
And some context code:
def add(a, b):
return a + b