A Python library for analyzing and calculating token usage costs for various LLM models like OpenAI and Anthropic, with support for real-time currency conversion.
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file token-analyzer-0.1.2.tar.gz.
File metadata
- Download URL: token-analyzer-0.1.2.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
92373e16d151ab3d1f667c8b314b6b1d8b4a6d0b1350e91e7838878e7b6beffc
|
|
| MD5 |
6b246322b9b21de4e8105c3cad207a92
|
|
| BLAKE2b-256 |
fee63b982ebddd93bec1bd1e1b3dba1aceb6201a5c2714b3efeb1bbc54ed3a59
|
File details
Details for the file token_analyzer-0.1.2-py3-none-any.whl.
File metadata
- Download URL: token_analyzer-0.1.2-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1b96373843864e12a768e0740cdd8f0e9d9d85f010c3b36ed8b87b592c901392
|
|
| MD5 |
27e361539386bcc5f5daf972dd39e3d0
|
|
| BLAKE2b-256 |
a85808d5282b040d1697a6119aa59eedbbe848816892795edf42013e4bfb5c2f
|