A library for calculating and estimating costs of language model usage
Project description
TokenMeter
TokenMeter is a Python library for calculating and estimating costs associated with using various language models. It provides tools for token counting, cost calculation, and includes integration with OpenAI's API.
Features
- Calculate costs for language model usage based on input and output tokens
- Estimate costs before making API calls
- Retrieve model information including pricing and token limits
- Integrated OpenAI API handler for easy response generation
- Utility functions for token counting, text truncation, and text splitting
Installation
You can install TokenMeter using pip:
pip install tokenmeter
Usage
Here are some basic usage examples:
from tokenmeter import TokenMeter, calculate_cost, estimate_cost, get_model_info, use_openai
# Initialize TokenMeter
tm = TokenMeter()
# Calculate cost for a completed interaction
model_name = "gpt-3.5-turbo"
prompt = "Tell me a joke about programming."
response = "Why do programmers prefer dark mode? Because light attracts bugs!"
cost = tm.calculate_cost(model_name, prompt, response)
print(f"Actual cost: ${cost['total_cost']:.6f}")
# Estimate cost before making an API call
estimated_output_tokens = 50
estimated_cost = tm.estimate_cost(model_name, prompt, estimated_output_tokens)
print(f"Estimated cost: ${estimated_cost['total_cost']:.6f}")
# Get model information
model_info = tm.get_model_info(model_name)
print(f"Model info: {model_info}")
# You can also use the convenience functions directly
direct_cost = calculate_cost(model_name, prompt, response)
print(f"Direct cost calculation: ${direct_cost['total_cost']:.6f}")
Contributing
Contributions to TokenMeter are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- This project uses pricing data from LiteLLM.
- Token counting is performed using the tiktoken library.
Support
If you encounter any problems or have any questions, please open an issue on the GitHub repository.
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 tokenmeter-0.1.0.tar.gz.
File metadata
- Download URL: tokenmeter-0.1.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33ece34efdd166e463d5cd3c1d710c68d3102a2c70ac1bca869665c3fcba2d18
|
|
| MD5 |
9d38723d499fbf21a7db8f070e86c448
|
|
| BLAKE2b-256 |
cd7cf8a3043ac6be347a141231cff68543704d0011aeb8873202229ce5253dc0
|
File details
Details for the file tokenmeter-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tokenmeter-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
86865d9cbcf973c58c13bd6710e8aca242a2845bb8276360053682b0c6660642
|
|
| MD5 |
0d0bf3df033e3d0a1e59c60aebf13c49
|
|
| BLAKE2b-256 |
614d6d1f465c8d570d66e71684c136386b8b0b538400def48aeaf6d2a4a032e5
|