Metadata-Version: 2.1
Name: tokenmeter
Version: 0.1.0
Summary: A library for calculating and estimating costs of language model usage
Home-page: https://github.com/rehan-ai/tokenmeter
Author: Rehan Asif
Author-email: Rehan Asif <asif.rehan@icloud.com>
Project-URL: Homepage, https://github.com/rehan-ai/tokenmeter
Project-URL: Bug Tracker, https://github.com/rehan-ai/tokenmeter/issues
Keywords: nlp,language models,cost estimation,openai
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Requires-Python: >=3.6
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: requests
Requires-Dist: tiktoken
Requires-Dist: openai
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: black; extra == "dev"
Requires-Dist: isort; extra == "dev"
Requires-Dist: flake8; extra == "dev"

# 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:

```bash
pip install tokenmeter
```

## Usage

Here are some basic usage examples:

```python
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](LICENSE) file for details.

## Acknowledgments

- This project uses pricing data from [LiteLLM](https://github.com/BerriAI/litellm).
- Token counting is performed using the [tiktoken](https://github.com/openai/tiktoken) library.

## Support

If you encounter any problems or have any questions, please open an issue on the [GitHub repository](https://github.com/rehan-ai/tokenmeter/issues).
