AST-Based Code Pruning
Parses your file into a real Python syntax tree, then removes docstrings, comments and dead whitespace — never regex guesswork.
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 app.py --copy
Token Report
✓ Pruned source copied to clipboard
def summarize(rows, key):
totals = defaultdict(float)
for row in rows:
totals[row[key]] += row['amount']
return dict(totals)Every feature exists to cut tokens without ever cutting correctness.
Parses your file into a real Python syntax tree, then removes docstrings, comments and dead whitespace — never regex guesswork.
Counts before and after with the same tokenizer the models use, so the savings you see are the savings you pay for.
Pipe the pruned source straight to your clipboard with -c, or rewrite the file in place with -w when you mean it.
Point it at a folder and it walks the tree, trimming every Python file and reporting the aggregate token delta.
Hand it Markdown, JSON or config files and it falls back to line-based cleanup instead of failing out.
Output is regenerated with ast.unparse(), so what comes out is always valid, runnable Python.
The four commands that cover almost everything.
$ token-trim script.pyPrint the pruned source and token report
$ token-trim script.py -cCopy the pruned source to your clipboard
$ token-trim script.py -wRewrite the file in place
$ token-trim ./srcTrim every Python file in a directory