Skip to main content

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.

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)

Source distribution for token-analyzer 0.1.2
File Size Uploaded
token-analyzer-0.1.2.tar.gz 5.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for token-analyzer 0.1.2
File Interpreter ABI Platform
token_analyzer-0.1.2-py3-none-any.whl Python 3 none any Details

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

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

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