Skip to main content

A tool to count tokens in your codebase

Project description

Code Token Counter

PyPI version Test

A tool for analyzing codebases to understand their token usage and compatibility with various Large Language Models (LLMs). This tool helps developers understand if their code can fit within different LLM context windows and how it's distributed across different technologies.

Features

  • Local & Remote Analysis: Analyze both local directories and remote Git repositories
  • Smart File Detection: Automatically detects and processes text-based files while ignoring binaries
  • Technology Categorization: Groups files by their technology/language (Python, JavaScript, Markdown, etc.)
  • Comprehensive LLM Comparisons: Compares token counts against popular LLM context windows:
    • OpenAI Models (GPT-3.5, GPT-4, GPT-4 Turbo)
    • Anthropic Models (Claude 2, Claude 3 variants)
    • Google Models (Gemini Pro, PaLM 2)
    • Meta Models (Llama 2, Code Llama)
    • Other Models (Mistral, Mixtral, Yi, Cohere)
  • Intelligent Directory Exclusion: Automatically excludes common non-source directories (venv, .git, pycache, etc.)

Installation

You can install and run this tool using either traditional pip or the modern uv package manager.

Using uv (Recommended)

The script includes inline dependencies, so you can run it directly with uv:

# Install uv if you haven't already
curl -LsSf https://astral.sh/uv/install.sh | sh

# Run the script directly (no virtual environment needed)
uv run token_counter.py <path_or_repo>

Using pip

# Create and activate a virtual environment (recommended)
python -m venv venv
source venv/bin/activate  # On Windows: .\venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

Usage

You can use the script to analyze both local directories and remote Git repositories:

# Using uv (recommended)
uv run token_counter.py https://github.com/username/repo
uv run token_counter.py .

# Using traditional python
python token_counter.py https://github.com/username/repo
python token_counter.py .

Output Format

The tool provides a detailed breakdown of token usage:

  1. Total Token Count: Overall tokens in the codebase
  2. File Extension Breakdown: Tokens and file count per extension
  3. Technology Distribution: Tokens and file count grouped by programming language/technology
  4. Context Window Analysis: Percentage of various LLM context windows used

Example output (text only):

Results:
Total tokens: 5.9K (5,942)

      Tokens by file extension
┏━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━┓
┃ Extension ┃       Tokens ┃  Files ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━━┩
│ .py       │ 4.8K (4,828) │ 1 file │
│ .md       │ 1.1K (1,086) │ 1 file │
│ .txt      │      28 (28) │ 1 file │
└───────────┴──────────────┴────────┘

         Tokens by Technology
┏━━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━┓
┃ Technology ┃       Tokens ┃  Files ┃
┡━━━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━━┩
│ Python     │ 4.8K (4,828) │ 1 file │
│ Markdown   │ 1.1K (1,086) │ 1 file │
│ Plain Text │      28 (28) │ 1 file │
└────────────┴──────────────┴────────┘

        Context Window Comparisons
┏━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Model                  ┃ Context Usage ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ GPT-3.5 (4K)           │        145.1% │
│ GPT-4 (8K)             │         72.5% │
│ GPT-4 (32K)            │         18.1% │
│ GPT-4 Turbo (128K)     │          4.6% │
│ Claude 2 (100K)        │          5.9% │
│ Claude 3 Opus (200K)   │          3.0% │
│ Claude 3 Sonnet (200K) │          3.0% │
│ Claude 3 Haiku (200K)  │          3.0% │
│ Gemini Pro (32K)       │         18.1% │
│ PaLM 2 (8K)            │         72.5% │
│ Llama 2 (4K)           │        145.1% │
│ Code Llama (100K)      │          5.9% │
│ Mistral Large (32K)    │         18.1% │
│ Mixtral 8x7B (32K)     │         18.1% │
│ Yi-34B (200K)          │          3.0% │
│ Cohere Command (128K)  │          4.6% │
└────────────────────────┴───────────────┘

Example output (image w/ colors):

Result Example

Installation

You can install the package directly from PyPI:

pip install codebase-token-counter

Usage

After installation, you can use the tool from the command line:

# Analyze a local directory
token-counter /path/to/your/codebase

# Analyze a remote Git repository
token-counter https://github.com/username/repo.git

Supported File Types

The tool supports a wide range of file types including:

  • Programming Languages (Python, JavaScript, TypeScript, Java, C/C++, etc.)
  • Web Technologies (HTML, CSS, SCSS, Vue, React, etc.)
  • Documentation (Markdown, reStructuredText)
  • Configuration (YAML, TOML, JSON)
  • And many more

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests for:

  • Adding support for new file types
  • Including new LLM context windows
  • Improving token counting accuracy
  • Enhancing performance for large codebases

License

MIT License - Feel free to use and modify as needed.

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

codebase_token_counter-0.1.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

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

codebase_token_counter-0.1.0-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file codebase_token_counter-0.1.0.tar.gz.

File metadata

  • Download URL: codebase_token_counter-0.1.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for codebase_token_counter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a8d292f27020c48aca46d225ba2a87ff373871e4968c04fa980d8fd889bf6bb2
MD5 c73c12713473b1b56d27a388c78e9e00
BLAKE2b-256 9a29edd0a0d318837e4048ddb083784bf3fe792e54585cb3851b5d400e3ad3d8

See more details on using hashes here.

File details

Details for the file codebase_token_counter-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for codebase_token_counter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f30598ff9f61e0326c374babc3f0b24d7360221b104b65ec40bfc9875d787009
MD5 a47f48530f877bac48635d824a5fdee0
BLAKE2b-256 47ff8747b609e1e0f309b156a6f83c08229b47b4bece421368d1edcbc2824f42

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