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.
Metadata
Release files for tokenmeter 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| tokenmeter-0.1.0.tar.gz | 7.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tokenmeter-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.5 kB
Release files / tokenmeter-0.1.0.tar.gz
| Download URL | tokenmeter-0.1.0.tar.gz |
|---|---|
| Size | 7.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
33ece34efdd166e463d5cd3c1d710c68d3102a2c70ac1bca869665c3fcba2d18
|
|
BLAKE2b-256 checksum How to use checksums |
cd7cf8a3043ac6be347a141231cff68543704d0011aeb8873202229ce5253dc0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.9
|
Release files / tokenmeter-0.1.0-py3-none-any.whl
| Download URL | tokenmeter-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
86865d9cbcf973c58c13bd6710e8aca242a2845bb8276360053682b0c6660642
|
|
BLAKE2b-256 checksum How to use checksums |
614d6d1f465c8d570d66e71684c136386b8b0b538400def48aeaf6d2a4a032e5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.9
|