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
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


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.0.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.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

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

Hashes for toknc-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9845240f08061fdee039f961cf58ba3385850f5d49b19de5cd71786d95d157ab
MD5 300224ce46116239cd67ad4f4fe360d2
BLAKE2b-256 7e933b60cd13938df345367d1d6a9b42f375799f48b777b8a8ef5f9ed8abcea5

See more details on using hashes here.

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

Hashes for toknc-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 54b975f8dfa2e2188b1d69ae5efba70dcd658e54ca1e716e72e51f932fa8f73d
MD5 c66350cd41a180e0bb65adb75bbf0558
BLAKE2b-256 1ed7114153b2a9cfa019e657a12fbd1c024e11f7ed5657d2116ea0103e8c82c8

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