Skip to main content

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
toknc

# Analyze specific directory
toknc /path/to/project

# Use specific model encoding
toknc --model gpt-4

# Only analyze Python files
toknc --include "*.py"

# Exclude test files
toknc --exclude "test_*" --exclude "*_test.py"

# Show all files in detail
toknc --details

# Output as JSON
toknc --json

# Show top 20 files
toknc --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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

toknc-0.1.1.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

toknc-0.1.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file toknc-0.1.1.tar.gz.

File metadata

  • Download URL: toknc-0.1.1.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

Hashes for toknc-0.1.1.tar.gz
Algorithm Hash digest
SHA256 def204d93b93fb4f44fe5d45fefbef162a8731cc3af7a593fa7b38b6d3ce7ee4
MD5 8ebdc73ecd325589f128f7cd11f831e9
BLAKE2b-256 75182188c283ea8061ef0fd8dc0c6decafbe3367a542e269bcd633eaf350e336

See more details on using hashes here.

File details

Details for the file toknc-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: toknc-0.1.1-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

Hashes for toknc-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5cd828c4db13a2c33919be235783c944f44cea20e41b82dfd867f9a099104732
MD5 ce4b856d32e8eac003cc1697599fc337
BLAKE2b-256 a573977e1bd5157dc06021d6bd2a2a08fc2848567416261b6af733f8bdf51b4f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page