Token Analyzer Library
TokenAnalyzer is a Python library to analyze and calculate token usage costs for various LLM models such as OpenAI, Anthropic, and others. The library supports live currency conversion using the FreeCurrencyAPI or uses default exchange rates if the API key is not provided.
Features
- Calculate costs for input/output tokens across various LLM models.
- Supports multiple currencies (INR, USD, EUR, etc.).
- Can write token usage information to a CSV file.
- Logging options for detailed analysis of costs.
- Use FreeCurrencyAPI for real-time currency conversion.
Installation
You can install the library directly from PyPI (after publishing):
pip install token-analyzer
Usage
Here is a basic usage example:
from analyzer.token_analysis import TokenAnalysis
# Response data from a model
response = {
'usage': {'prompt_tokens': 1000, 'completion_tokens': 500},
'model': 'claude-2.1'
}
# Initialize the analyzer
analyzer = TokenAnalysis(output_currency="INR", output_csv="output/token_costs.csv", create_csv=True, log_info=True)
# Calculate token cost
total_cost = analyzer.token_analysis(response, "ANTHROPIC")
print(f"Total Cost: {total_cost}")
Example without CSV output and logging disabled:
analyzer = TokenAnalysis(output_currency="USD", create_csv=False, log_info=False)
total_cost = analyzer.token_analysis(response, "OPENAI")
print(f"Total Cost: {total_cost}")
License
This project is licensed under the MIT License - see the LICENSE file for details.
Links
Metadata
Release files for token-analyzer 0.1.2
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 |
|---|---|---|---|---|
| token-analyzer-0.1.2.tar.gz | 5.0 kB | Without provenance | Details for token-analyzer-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| token_analyzer-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for token_analyzer-0.1.2-py3-none-any.whl |
Total release size: 10.4 kB
Release files / token-analyzer-0.1.2.tar.gz
| Download URL | token-analyzer-0.1.2.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
92373e16d151ab3d1f667c8b314b6b1d8b4a6d0b1350e91e7838878e7b6beffc
|
|
BLAKE2b-256 checksum How to use checksums |
fee63b982ebddd93bec1bd1e1b3dba1aceb6201a5c2714b3efeb1bbc54ed3a59
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.11.6
|
Release files / token_analyzer-0.1.2-py3-none-any.whl
| Download URL | token_analyzer-0.1.2-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1b96373843864e12a768e0740cdd8f0e9d9d85f010c3b36ed8b87b592c901392
|
|
BLAKE2b-256 checksum How to use checksums |
a85808d5282b040d1697a6119aa59eedbbe848816892795edf42013e4bfb5c2f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.11.6
|
Security for token-analyzer 0.1.2
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.