Built with Python AST & tiktoken

Ship the same Python with fewer tokens

token-trim is a Python CLI that strips docstrings, comments and redundant whitespace from your source before it ever reaches a model — measured with tiktoken, rebuilt with ast.unparse(), so nothing breaks.

$ pip install token-trim-cli
token-trim — zsh

$ token-trim app.py --copy

Token Report

Original Tokens
758
Pruned Tokens
670
Tokens Saved
88 (11.6% reduction)

✓ Pruned source copied to clipboard

def summarize(rows, key):
    totals = defaultdict(float)
    for row in rows:
        totals[row[key]] += row['amount']
    return dict(totals)

Built for real codebases

Every feature exists to cut tokens without ever cutting correctness.

AST-Based Code Pruning

Parses your file into a real Python syntax tree, then removes docstrings, comments and dead whitespace — never regex guesswork.

tiktoken Precision

Counts before and after with the same tokenizer the models use, so the savings you see are the savings you pay for.

Clipboard & In-Place

Pipe the pruned source straight to your clipboard with -c, or rewrite the file in place with -w when you mean it.

Batch & Directory Processing

Point it at a folder and it walks the tree, trimming every Python file and reporting the aggregate token delta.

Non-Python Fallback

Hand it Markdown, JSON or config files and it falls back to line-based cleanup instead of failing out.

Zero Syntax Corruption

Output is regenerated with ast.unparse(), so what comes out is always valid, runnable Python.

Quick reference

The four commands that cover almost everything.

  • $ token-trim script.py

    Print the pruned source and token report

  • $ token-trim script.py -c

    Copy the pruned source to your clipboard

  • $ token-trim script.py -w

    Rewrite the file in place

  • $ token-trim ./src

    Trim every Python file in a directory