repotokens
repotokens is a Python package for analyzing entire source repositories to count tokens and estimate costs for various LLMs. It can be used both as a command-line tool and as an importable Python library.
Features
- Analyzes entire repositories to count tokens on common source file types.
- Opinionated about excluding those filetypes / utilities that are unlikely to be useful for LLM reasoning on top of the repository.
- Estimates costs of for different LLMs based on token count
- Respects .gitignore files and common ignore patterns
- Can be used as both a Python library and a command-line tool
Installation
You can install repotokens using pip:
pip install repotokens
Usage as a Python Library
You can use repotokens in your Python scripts by importing it as follows:
from repotokens import analyze_directory, calculate_costs, MODELS
Analyzing a Directory
To analyze a directory and get token counts:
results = analyze_directory("/path/to/your/repo")
print(f"Total tokens: {results['total_tokens']}")
print(f"Processed files: {results['processed_files']}")
# Print token counts for each file
for file, tokens in results['file_tokens'].items():
print(f"{file}: {tokens} tokens")
Calculating Costs for a Specific Model
To calculate costs for a specific model:
model = "gpt-4o"
total_tokens = results['total_tokens']
costs = calculate_costs(total_tokens, model)
print(f"Costs for {model}:")
print(f"Input cost: ${costs['input_cost']:.2f}")
print(f"Output cost: ${costs['output_cost']:.2f}")
Getting Information for All Models
You can access information about all available models:
for model, prices in MODELS.items():
print(f"{model}:")
print(f" Input price: ${prices['input_price']} per 1M tokens")
print(f" Output price: ${prices['output_price']} per 1M tokens")
Command-Line Usage
repotokens can also be used as a command-line tool:
Analyze Current Directory
repotokens
Analyze a Specific Directory
repotokens /path/to/your/repo
Analyze a Directory and Show Costs for a Specific Model
repotokens /path/to/your/repo --model gpt-4o-mini
Show Version
repotokens --version
Metadata
Release files for repotokens 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| repotokens-1.0.1.tar.gz | 5.1 kB | Without provenance | Details for repotokens-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| repotokens-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for repotokens-1.0.1-py3-none-any.whl |
Total release size: 10.6 kB
Release files / repotokens-1.0.1.tar.gz
| Download URL | repotokens-1.0.1.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e4694aa58736031df80130afa0207aaf6a4b64b234b592ce98e66bc4f51543e6
|
|
BLAKE2b-256 checksum How to use checksums |
f51a303a6db57c42aac7134c2f905c0f5eac1ca00278e0f8b84e3b61a899ca2a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.5
|
Release files / repotokens-1.0.1-py3-none-any.whl
| Download URL | repotokens-1.0.1-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
54db55465c22eff051ada5eeef09162454f21cdce4f12348c07ca7f907b5ade4
|
|
BLAKE2b-256 checksum How to use checksums |
c871fade256ac324dd2d1167a1bef7f1bbe6e9a9ac1cb9ec01309a39fe0060ff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.5
|
Security for repotokens 1.0.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.