Skip to main content

token-trim banner

token-trim

Intelligently prune Python code and log files to reduce LLM context window usage and lower API costs.

token-trim is a lightweight command-line utility built with Python, AST, Typer, Rich, and tiktoken. It removes docstrings, strips comments, normalizes whitespace, and measures token savings while preserving valid Python syntax.


Overview

Large Language Models (LLMs) have limited context windows and typically charge based on the number of input tokens. Python source files often contain docstrings, comments, blank lines, and formatting that increase token usage without affecting program execution.

token-trim removes unnecessary content while preserving the behavior of your code, making it ideal for preparing files before sending them to an LLM.


Features

  • AST-based Python code pruning
  • Removes module, class, and function docstrings
  • Preserves valid Python syntax, including empty function bodies
  • Falls back to line-based cleaning for non-Python files (such as logs)
  • Reports token counts before and after pruning
  • Calculates token savings and percentage reduction
  • Supports copying pruned output directly to the clipboard (-c, --copy)
  • Supports recursive directory and batch processing
  • Supports overwriting files in place (-w, --write)
  • Clean command-line interface powered by Rich and Typer

How It Works

For Python files, token-trim:

  1. Parses source code using Python's built-in ast module.
  2. Traverses the Abstract Syntax Tree (AST).
  3. Removes module, class, and function docstrings.
  4. Reconstructs valid Python code using ast.unparse().
  5. Calculates token counts before and after pruning using tiktoken.

If the input is not valid Python, token-trim automatically switches to a line-based cleanup that removes comments, blank lines, and unnecessary whitespace.


Installation

Requirements

  • Python 3.8 or later

Clone the Repository

git clone https://github.com/pareshrnayak/token-trim.git
cd token-trim

Create a Virtual Environment (Recommended)

macOS / Linux

python3 -m venv venv
source venv/bin/activate

Windows

python -m venv venv
venv\Scripts\activate

Install the Package

Install token-trim in editable mode:

pip install -e .

Install with development dependencies:

pip install -e .[dev]

Usage

Basic Usage

Prune a Python file or log file:

token-trim path/to/file.py

Copy Output to the Clipboard

Copy the pruned output directly to your system clipboard.

token-trim path/to/file.py --copy

or

token-trim path/to/file.py -c

Overwrite the Original File

Replace the original file with the pruned version.

token-trim path/to/file.py --write

or

token-trim path/to/file.py -w

Specify a Tokenizer Model

By default, token-trim uses the gpt-4o tokenizer.

Use a different tokenizer:

token-trim path/to/file.py --model gpt-3.5-turbo

or

token-trim path/to/file.py -m gpt-3.5-turbo

Process an Entire Directory

Recursively prune all supported files in a directory.

token-trim path/to/folder/

View Available Options

token-trim --help

Example Output

╭────────────────────── Token-Trim Execution Complete ──────────────────────╮
│ Original Tokens: 758                                                      │
│ Pruned Tokens:   670                                                      │
│ Tokens Saved:    88 (11.6% reduction)                                     │
│                                                                           │
│ Pruned Output Preview:                                                    │
│                                                                           │
│   1 │ import ast                                                          │
│   2 │ import typer                                                        │
│   3 │ from rich.console import Console                                    │
│   ...                                                                     │
╰───────────────────────────────────────────────────────────────────────────╯

Development

Run Unit Tests

pytest

Run the Linter

ruff check .

Format the Code

black .

Build Package Distributions

python -m build

Tech Stack

  • Python
  • AST (ast)
  • Typer
  • Rich
  • tiktoken

Contributing

Contributions are welcome.

Please read the project's CONTRIBUTING.md before opening an issue or submitting a pull request.


License

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

Metadata

Release files for token-trim-cli 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for token-trim-cli 0.1.0
File Size Uploaded
token_trim_cli-0.1.0.tar.gz 11.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for token-trim-cli 0.1.0
File Interpreter ABI Platform
token_trim_cli-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.6 kB

Release files / token_trim_cli-0.1.0.tar.gz

Download URL token_trim_cli-0.1.0.tar.gz
Size 11.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1b54c5ff9bc5300a4cb686b94302f7c469b08a8bbc8f4c895ca98e58ba2e1f7d
BLAKE2b-256 checksum
How to use checksums
edf3f0f60b736f29fc28f6e711cb9ee3972276dc4da37be6d6728940e5a2005e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.3

Release files / token_trim_cli-0.1.0-py3-none-any.whl

Download URL token_trim_cli-0.1.0-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
393b6412d837f2b7cd895ab9796248ed561615d2024fd42f005b93d8a430a27a
BLAKE2b-256 checksum
How to use checksums
70f3f11813f525215124442c7aa9bf52e29f0a172ff05cd10313291af2deb257
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.3

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page