Skip to main content

Estimate tokens for Gemini and display tree from local/git sources.

Project description

Gemini Tree Token Counter (gtc)

🇯🇵 日本語 (Japanese)

A CLI tool to estimate tokens for Gemini models across local directories and GitHub repositories. It visualizes the file tree with token counts and supports "Time Travel" (analyzing specific Git commits/dates).

Note: The token counting logic is based on regex approximations (ported from smartprocure/gemini-token-estimator) and is designed for speed. It is accurate within ~10% for most cases.

Features

  • 🚀 Fast Estimation: Uses regex-based logic (no API calls required).
  • 🌳 Tree Visualization: Displays directory structure with token counts per node.
  • 🐙 GitHub Support: Analyze remote repositories directly.
  • Time Travel: Check token counts for a specific branch, commit hash, or date (e.g., "how many tokens was this repo in 2023?").
  • 📄 Content Inspection: Optionally output file contents for context generation.
  • 🧹 Exclude Patterns: Ignore files via .gtcignore or --exclude.

Installation

Prerequisites

  • Python 3.7+
  • Git (The git command must be available in your system path)

Install via pip

pip install gemini-tree-token-counter

Usage

Basic usage scans the current directory:

gtc

Options

  • target: Local path or GitHub URL (default: current directory).
  • -d, --dir: Target specific subdirectories (can be used multiple times).
  • -c, --content: Display file contents (useful for piping to LLMs).
  • -e, --exclude: Exclude patterns (glob). Can be used multiple times.
  • -b, --branch: Checkout a specific Git branch.
  • --commit: Checkout a specific Git commit hash.
  • --date: Checkout the latest commit before a specific date (format: YYYY-MM-DD).
  • --first: Checkout the first (initial) commit of the repo.

Exclude Patterns

You can exclude files/folders that are tracked by Git but unnecessary for token counting.

  1. CLI (--exclude)

    gtc . --exclude "*.test.ts" --exclude "__snapshots__" --exclude "telemetry"
    
  2. .gtcignore (project root)

    # Tests and snapshots
    *.test.ts
    __snapshots__/
    __mocks__/
    
    # Heavy directories
    telemetry/
    packages/core/src/mcp/
    

Examples

# 1. Local Directory
gtc                       # Scan current directory
gtc . -d src -d lib       # Scan specific dirs
gtc . -c > context.txt    # Dump all code and tokens to a file
gtc . -e "*.test.ts" -e "__snapshots__"  # Exclude patterns

# 2. GitHub Repository
gtc https://github.com/user/repo
gtc https://github.com/user/repo -b develop

# 3. Time Travel (Git History)
gtc . --date 2023-01-01   # How big was this project last year?
gtc . --first             # How small was the first commit?

Output Example (ver 0.1.2)

$ gtc

Source       : .
Working Path : /Users/sakasegawa/src/github.com/nyosegawa/gemini-tree-token-counter
------------------------------------------------------------
├── src/ (4679 tokens)
│   └── gemini_tree_token_counter/ (4679 tokens)
│       ├── __init__.py (11 tokens)
│       └── main.py (4668 tokens)
├── tests/ (2596 tokens)
│   ├── __init__.py (0 tokens)
│   └── test_tokenizer.py (2596 tokens)
├── .gitignore (682 tokens)
├── LICENSE (0 tokens)
├── pyproject.toml (249 tokens)
├── README.md (1130 tokens)
└── README_ja.md (594 tokens)
------------------------------------------------------------
Grand Total Tokens: 9930
------------------------------------------------------------

License & Acknowledgments

This project is licensed under the MIT License.

However, the tokenizer regex logic is a port of gemini-token-estimator by GovSpend, which is licensed under the ISC License.

Tokenizer Logic Copyright

Copyright (c) GovSpend

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

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

gemini_tree_token_counter-0.1.6.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

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

gemini_tree_token_counter-0.1.6-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file gemini_tree_token_counter-0.1.6.tar.gz.

File metadata

File hashes

Hashes for gemini_tree_token_counter-0.1.6.tar.gz
Algorithm Hash digest
SHA256 29dd9d4a85e4fc1be01326b25e7757cae9dc8502db353c7146ec4e48e4bfff8b
MD5 80c41e17386cf9588dd161ecc15e55af
BLAKE2b-256 5bc77bda7330ebb5e7ade4552029f29e85312fb563e4a72d73c0cf57a7913181

See more details on using hashes here.

Provenance

The following attestation bundles were made for gemini_tree_token_counter-0.1.6.tar.gz:

Publisher: publish.yml on nyosegawa/gemini-tree-token-counter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gemini_tree_token_counter-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for gemini_tree_token_counter-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 a7b18fca3e98288f0c54b1c32834c27b91544f4bf72533fd9bec55a3358b4d52
MD5 4f9e48c2e5a80a7fcc89b8dd2a2ad4e4
BLAKE2b-256 13c9fbdfb0611540e35698b1ad6821672b620a929545f4a3f7a8506155dd0485

See more details on using hashes here.

Provenance

The following attestation bundles were made for gemini_tree_token_counter-0.1.6-py3-none-any.whl:

Publisher: publish.yml on nyosegawa/gemini-tree-token-counter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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