Skip to main content

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.

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.

Metadata

Release files for codebase-token-counter 0.1.1

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

Source distribution (sdist)

Source distribution for codebase-token-counter 0.1.1
File Size Uploaded Provenance Actions
codebase_token_counter-0.1.1.tar.gz 9.7 kB Without provenance Details for codebase_token_counter-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for codebase-token-counter 0.1.1
File Interpreter ABI Platform Actions
codebase_token_counter-0.1.1-py3-none-any.whl Python 3 none any Details for codebase_token_counter-0.1.1-py3-none-any.whl

Total release size: 20.2 kB

Release files / codebase_token_counter-0.1.1.tar.gz

Download URL codebase_token_counter-0.1.1.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
fa6f3154c0868c6c3677d99d33d9cd9ac54862527ec80c979801e75105c07b5c
BLAKE2b-256 checksum
How to use checksums
15f312594553d828eb595fd74da86210cc78836b6c4d1d26e47576166cf36873
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / codebase_token_counter-0.1.1-py3-none-any.whl

Download URL codebase_token_counter-0.1.1-py3-none-any.whl
Size 10.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fb905c9c0b628cbcbf3807053605cebfd096b3e8e3d6e770ab288d207229cc0a
BLAKE2b-256 checksum
How to use checksums
6ad1c9752e40d37675c35da7784d166dcce603a7329fa8a5e76c0ac1a769b396
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Security for codebase-token-counter 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

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