A Python CLI application that estimates token usage for files in a directory
Project description
Token Counter CLI
A Python CLI application that estimates token usage for files in a directory, helping you understand how much context window space your codebase would consume.
Features
- ๐ Analyzes token usage for all text files in a directory
- ๐ค Supports multiple OpenAI model encodings
- ๐ Shows context window usage percentages for popular models
- ๐ Ranks files by token consumption
- ๐ซ Automatically excludes binary files and common build/cache directories
- ๐ Supports file inclusion/exclusion patterns
- ๐ JSON output option for integration
- โ ๏ธ Fallback token estimation when tiktoken is not available
Installation
# Install via pip
pip install toknc
# For accurate token counting, install tiktoken:
pip install toknc[tiktoken]
Note: The tool will work without tiktoken using rough estimation (โ1 token per 4 characters).
Usage
# Analyze current directory
tokenc
# Analyze specific directory
tokenc /path/to/project
# Use specific model encoding
tokenc --model gpt-4
# Only analyze Python files
tokenc --include "*.py"
# Exclude test files
tokenc --exclude "test_*" --exclude "*_test.py"
# Show all files in detail
tokenc --details
# Output as JSON
tokenc --json
# Show top 20 files
tokenc --top 20
Options
directory: Directory to analyze (default: current directory)--model: OpenAI model for token encoding (default: gpt-3.5-turbo)--include: File patterns to include (can be used multiple times)--exclude: File patterns to exclude (can be used multiple times)--top: Number of top files to show (default: 10)--details: Show all files in detail--json: Output results as JSON
Example Output
๐ Token Analysis for: /Users/adminx/test
๐ค Model: gpt-3.5-turbo
๐ Total files: 4
๐ช Total tokens: 2,897
๐พ Total size: 11.4 KB
๐ Context Window Usage:
GPT-3.5 Turbo 4096 tokens: โโโโโโโโโโโโโโโโโโโโ 70.7%
GPT-4 8192 tokens: โโโโโโโโโโโโโโโโโโโโ 35.4%
GPT-4 Turbo 128000 tokens: โโโโโโโโโโโโโโโโโโโโ 2.3%
Claude 3 200000 tokens: โโโโโโโโโโโโโโโโโโโโ 1.4%
Gemini Pro 32768 tokens: โโโโโโโโโโโโโโโโโโโโ 8.8%
๐ Top 4 files by tokens:
1. token_counter.py
Tokens: 2,877 (99.3%)
Size: 11.3 KB
Supported File Types
The tool automatically detects text files and includes common source code extensions:
- Python (.py), JavaScript (.js, .jsx), TypeScript (.ts, .tsx)
- Web files (.html, .css)
- Config files (.json, .xml, .yaml, .yml)
- Documentation (.md, .rst)
- Shell scripts (.sh, .bash, .zsh)
- C/C++ (.c, .cpp, .h, .hpp)
- Java (.go, .rs, .php, .rb)
- And many more...
Automatically Excluded
- Version control: .git/, .svn/, .hg/
- Dependencies: node_modules/, pycache/
- Compiled files: .pyc, .pyo, .pyd
- System files: .DS_Store, Thumbs.db
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file toknc-0.1.0.tar.gz.
File metadata
- Download URL: toknc-0.1.0.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9845240f08061fdee039f961cf58ba3385850f5d49b19de5cd71786d95d157ab
|
|
| MD5 |
300224ce46116239cd67ad4f4fe360d2
|
|
| BLAKE2b-256 |
7e933b60cd13938df345367d1d6a9b42f375799f48b777b8a8ef5f9ed8abcea5
|
File details
Details for the file toknc-0.1.0-py3-none-any.whl.
File metadata
- Download URL: toknc-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54b975f8dfa2e2188b1d69ae5efba70dcd658e54ca1e716e72e51f932fa8f73d
|
|
| MD5 |
c66350cd41a180e0bb65adb75bbf0558
|
|
| BLAKE2b-256 |
1ed7114153b2a9cfa019e657a12fbd1c024e11f7ed5657d2116ea0103e8c82c8
|