Skip to main content

A Python library for tracking LLM usage, costs, and environmental impact

Project description

Ecobench

PyPI version Python 3.8+ License: MIT Code style: black

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

  1. Fork the repository
  2. Clone your fork: git clone https://github.com/yourusername/ecobench.git
  3. Install in development mode: pip install -e ".[dev]"
  4. Run tests: pytest
  5. Run linting: flake8 src/ tests/
  6. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ecobench-1.0.0.tar.gz (29.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ecobench-1.0.0-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

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

Hashes for ecobench-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9b9fd952349757e4b3e57537184ceb55d05a567cd5f0b6665cb2672b79e506af
MD5 2145ae92d8dd1e60f453f95c0ad2ffd3
BLAKE2b-256 a89d83c6bdc901ee34e0f1564d3ce3c433423757d06c38c08f9897b0c870998d

See more details on using hashes here.

Provenance

The following attestation bundles were made for ecobench-1.0.0.tar.gz:

Publisher: python-publish.yml on KnutOplandMoen/ecobench

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for ecobench-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 31bd1b2e5626c68adc59214a3234f2e1e4370a55e9532f5712f9126896f40450
MD5 6d1ac560106e0399ad3c399672e85775
BLAKE2b-256 0c09330e7b19d18191a7a61356e5d034b915bc3684f03b33ed618ea01574e95f

See more details on using hashes here.

Provenance

The following attestation bundles were made for ecobench-1.0.0-py3-none-any.whl:

Publisher: python-publish.yml on KnutOplandMoen/ecobench

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page