A Python library for tracking LLM usage, costs, and environmental impact
Project description
Ecobench
A comprehensive Python library for tracking LLM usage, costs, and environmental impact. Monitor your AI model consumption with detailed analytics and environmental metrics.
Features
- Easy Integration: Simple API for tracking LLM usage in your applications
- Cost Tracking: Monitor API costs with support for input, output, and cached tokens
- Environmental Impact: Track energy consumption and water usage
- Multiple Models: Built-in support for popular models (GPT-4o, GPT-4o-mini, o3-mini, Mixtral-8x22B)
- Custom Models: Support for custom model configurations
- Chain of Thought: Support for CoT reasoning with token multipliers
Installation
From PyPI (Recommended)
pip install ecobench
From Source
git clone https://github.com/ecobench/ecobench.git
cd ecobench
pip install -e .
Development Installation
git clone https://github.com/ecobench/ecobench.git
cd ecobench
pip install -e ".[dev]"
Quick Start
from ecobench import Tracker
# Initialize tracker
tracker = Tracker("GPT-4o")
# Track usage
tracker.update_state(input_tokens=100, output_tokens=50, cached_tokens=20)
# Get summary
tracker.print_summary()
Usage Examples
Basic Usage
from ecobench import Tracker
# Initialize tracker with GPT-4o
tracker = Tracker("GPT-4o")
# Track an API call
result = tracker.update_state(
input_tokens=100,
output_tokens=50,
cached_tokens=20
)
print(f"Cost: ${result['cost']:.4f}")
print(f"Energy: {result['energy_wh']:.4f} Wh")
Running Examples
The library includes several example scripts:
# Simple model comparison
python examples/simple_comparison.py
# Comprehensive visualization
python examples/visualize_model_comparison.py
# Notebook integration
python examples/notebook_integration.py
Integration with Chat Applications
class ChatApp:
def __init__(self):
self.tracker = Tracker("GPT-4o")
def make_api_call(self, prompt, response, cached_tokens=0):
# Estimate tokens (in practice, get from API response)
input_tokens = len(prompt.split())
output_tokens = len(response.split())
# Track usage
result = self.tracker.update_state(
input_tokens=input_tokens,
output_tokens=output_tokens,
cached_tokens=cached_tokens
)
return response, result
Model Comparison
# Compare different models
tracker_gpt4o = Tracker("GPT-4o")
tracker_mini = Tracker("GPT-4o-mini")
result_gpt4o = tracker_gpt4o.update_state(input_tokens=1000, output_tokens=500)
result_mini = tracker_mini.update_state(input_tokens=1000, output_tokens=500)
print(f"GPT-4o cost: ${result_gpt4o['cost']:.4f}")
print(f"GPT-4o-mini cost: ${result_mini['cost']:.4f}")
Custom Models
from ecobench import Model, Tracker
# Create custom model
custom_model = Model(
name="Custom Model",
d_model=4096,
d_ff=11008,
layers=32,
num_query_heads=32,
cost_per_input_token=0.001/1000,
cost_per_output_token=0.002/1000,
cost_per_cache_token=0.0005/1000
)
# Use with tracker
tracker = Tracker(custom_model=custom_model)
Available Models
- GPT-4o: OpenAI's flagship model
- GPT-4o-mini: Smaller, faster version of GPT-4o
- o3-mini: OpenAI's reasoning model with CoT support
- Mixtral-8x22B: Open-source MoE model
API Reference
Tracker
__init__(model_name: str = "GPT-4o", custom_model: Model = None)
Initialize tracker with a model.
update_state(input_tokens: int, output_tokens: int, cached_tokens: int = 0, use_cot_reasoning: bool = False, cache_percent: float = None) -> dict
Update tracker with new usage data.
get_summary() -> dict
Get summary of all tracked usage.
print_summary()
Print formatted summary to console.
reset()
Reset all tracking data.
Environmental Impact
The library tracks:
- Energy Consumption: In Watt-hours (Wh)
- Water Usage: In liters
- Carbon Footprint: Through energy calculations
Development Setup
- Fork the repository
- Clone your fork:
git clone https://github.com/yourusername/ecobench.git - Install in development mode:
pip install -e ".[dev]" - Run tests:
pytest - Run linting:
flake8 src/ tests/ - Format code:
black src/ tests/
License
This project is licensed under the MIT License - see the LICENSE file for details.
## Support
- 📖 [Documentation](https://github.com/KnutOplandMoen/ecobench#readme)
- 🐛 [Bug Reports](https://github.com/KnutOplandMoen/ecobench/issues)
- 💬 [Discussions](https://github.com/KnutOplandMoen/ecobench/discussions)
- 📧 Email: knutomoe@stud.ntnu.no
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 ecobench-1.0.0.tar.gz.
File metadata
- Download URL: ecobench-1.0.0.tar.gz
- Upload date:
- Size: 29.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9b9fd952349757e4b3e57537184ceb55d05a567cd5f0b6665cb2672b79e506af
|
|
| MD5 |
2145ae92d8dd1e60f453f95c0ad2ffd3
|
|
| BLAKE2b-256 |
a89d83c6bdc901ee34e0f1564d3ce3c433423757d06c38c08f9897b0c870998d
|
Provenance
The following attestation bundles were made for ecobench-1.0.0.tar.gz:
Publisher:
python-publish.yml on KnutOplandMoen/ecobench
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ecobench-1.0.0.tar.gz -
Subject digest:
9b9fd952349757e4b3e57537184ceb55d05a567cd5f0b6665cb2672b79e506af - Sigstore transparency entry: 587857992
- Sigstore integration time:
-
Permalink:
KnutOplandMoen/ecobench@3bc0fb87cd4f93b09dcb0252ee0bd991d39fe6d2 -
Branch / Tag:
refs/tags/v1.0.0.0-alpha - Owner: https://github.com/KnutOplandMoen
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@3bc0fb87cd4f93b09dcb0252ee0bd991d39fe6d2 -
Trigger Event:
release
-
Statement type:
File details
Details for the file ecobench-1.0.0-py3-none-any.whl.
File metadata
- Download URL: ecobench-1.0.0-py3-none-any.whl
- Upload date:
- Size: 14.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
31bd1b2e5626c68adc59214a3234f2e1e4370a55e9532f5712f9126896f40450
|
|
| MD5 |
6d1ac560106e0399ad3c399672e85775
|
|
| BLAKE2b-256 |
0c09330e7b19d18191a7a61356e5d034b915bc3684f03b33ed618ea01574e95f
|
Provenance
The following attestation bundles were made for ecobench-1.0.0-py3-none-any.whl:
Publisher:
python-publish.yml on KnutOplandMoen/ecobench
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ecobench-1.0.0-py3-none-any.whl -
Subject digest:
31bd1b2e5626c68adc59214a3234f2e1e4370a55e9532f5712f9126896f40450 - Sigstore transparency entry: 587858079
- Sigstore integration time:
-
Permalink:
KnutOplandMoen/ecobench@3bc0fb87cd4f93b09dcb0252ee0bd991d39fe6d2 -
Branch / Tag:
refs/tags/v1.0.0.0-alpha - Owner: https://github.com/KnutOplandMoen
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@3bc0fb87cd4f93b09dcb0252ee0bd991d39fe6d2 -
Trigger Event:
release
-
Statement type: