A green prompt tool to estimate energy usage of LLM prompts
Project description
GreenPrompt 🌱
Overview
GreenPrompt is a local-first tool that tracks and visualizes the real-world energy consumption of AI prompts, locally. It intercepts or wraps LLM calls (via Ollama’s local API), samples CPU/GPU power, logs prompt metadata and energy usage, and provides both CLI and Web UI interfaces for analysis.
macOS only (uses
powermetricsfor power sampling)
Features
- Baseline & Prompt Sampling: Continuously samples system power in a 10‑minute sliding window; computes a 1‑minute baseline before each prompt and measures power during execution.
- Token & Model Tracking: Captures prompt and completion token counts, model used.
- Energy Estimation: Calculates energy in Wh based on sampled power data and execution duration.
- Persistent Logging: Stores every prompt run in a SQLite database (
greenprompt_usage.db). - CLI Interface: Run prompts, view immediate stats.
- Web API & Dashboard: Flask-based endpoints for retrieving usage and a simple HTML dashboard.
- Proxy Server: (Optional) Intercept existing Ollama API traffic to log usage without code changes.
Architecture & Flow
- Power Sampling
samplerMac.pydefinesPowerMonitor, a background thread that samples combined CPU/GPU power every second and retains a 10‑minute history.
- Prompt Execution
core.py’srun_promptfunction:- Retrieves a 1‑minute baseline average from
PowerMonitor. - Sends the user prompt to Ollama at
127.0.0.1:5000:11434(or via proxy). - Records start and end timestamps.
- Queries
PowerMonitorfor samples during execution. - Calculates average power and energy.
- Retrieves a 1‑minute baseline average from
- Data Persistence
dbconn.pyprovidesinit_db()andsave_prompt_usage(), storing each run’s metadata in SQLite.
- Interfaces
- CLI (
cli.py):greenprompt <prompt>to run and log. - Web App (
web.py/api.py):POST /api/generateto run prompts.GET /api/usage/all/model/<model>/timeframeto retrieve logs.
- Setup Script (
setup.py): Initializes the DB and rebinds Ollama port.
- CLI (
File Structure
greenprompt/
├── cli.py # CLI entry point
├── core.py # Prompt runner & energy estimator
├── dbconn.py # SQLite connection, init, and save/query functions
├── samplerMac.py # macOS PowerMonitor for sampling power
├── sysUsage.py # OS-agnostic wrappers & estimators
├── setup.py # Tool setup: DB init, Ollama rebind
├── analytics.py # Plotly graphs for the dashboard
├── web.py # Flask web server, HTML dashboard
├── templates/
│ └── index.html # Web UI template
├── static/ # Optional assets (CSS)
└── constants.py # User-editable settings
Installation & Setup
PyPI Installation
You can install GreenPrompt directly from PyPI:
pip install greenprompt
Or, can build it using Poetry
# If you don't have Poetry, install it first:
pip install poetry
# Install dependencies and build the package
poetry install
# (Optional) To build a distributable wheel and sdist:
poetry build
And install it
# If you want to install the built package locally (after poetry build):
pip install dist/*.whl
Or, if you just want to install in your current environment for development:
poetry install
This will install both the greenprompt and gp CLI commands.
Development Installation
-
Clone & Install
git clone <repo-url> cd greenprompt poetry install
-
Initialize
python setup.py- Creates
greenprompt_usage.db - Rebinds Ollama to port 11435 (via setup instructions)
- Creates
Usage
CLI Usage
Once installed, use the following commands:
# Setup Ollama port, initialize database, and generate constants
greenprompt setup
# or using alias
gp setup
# Start the web API server and dashboard
greenprompt run --port 5000
gp run --port 5000
# Send a prompt and display stats
greenprompt prompt "Hello, world!" --model llama2
gp prompt "Hello, world!" --model llama2
# Monitor the last 10 prompt usage entries
greenprompt monitor --count 10
gp monitor --count 10
# Launch Dashboard
greenprompt dashboard
gp dashboard
API Endpoints
-
POST /api/prompt
Send a JSON body withpromptand optionalmodel.
Returns the generated response and all energy/token metrics. -
GET /api/usage/all
Retrieve all prompt usage records as a JSON array. -
GET /api/usage/model/
Retrieve prompt usage records filtered by model. -
GET /api/usage/timeframe?start=ISO&end=ISO
Retrieve prompt usage records betweenstartandendISO timestamps. -
GET /dashboard
Serve the HTML dashboard with embedded Plotly analytics. -
Proxy all Ollama API calls via ANY /ollama/api/
Forward requests to the local Ollama server (default port 11434), preserving method, headers, query params, and body.
Limitations
- macOS only (relies on
powermetrics) - Per-process power usage is estimated proportionally from system metrics
- CLI proxy requires redirecting Ollama to use
127.0.0.1:5000:11434
Future Work
- Linux & Windows support (via Intel Power Gadget,
rapl,nvidia-smi) - VS Code & browser extensions
- Carbon offset integrations
- Team dashboards & enterprise reporting
License
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 greenprompt-0.0.1.tar.gz.
File metadata
- Download URL: greenprompt-0.0.1.tar.gz
- Upload date:
- Size: 17.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2f602ee29369aea206619d631fd61a485e2f38aab7eb538a3b2f924cf628231a
|
|
| MD5 |
d2936186228a6d58104a2d11fc83315e
|
|
| BLAKE2b-256 |
f1d9bb05feaa54790852af5637e149facdd3c098a9c97a938901be976794fb45
|
File details
Details for the file greenprompt-0.0.1-py3-none-any.whl.
File metadata
- Download URL: greenprompt-0.0.1-py3-none-any.whl
- Upload date:
- Size: 19.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
07c1f5fd3f6d72986320bfd2fe4f6c1bf9e46535ecafb0702d61108c530d26a9
|
|
| MD5 |
b926ae51f721a0b1f5efa3e979749ad4
|
|
| BLAKE2b-256 |
46fe61e4295aec426b17dc1d9937085aecd573f567a3dc2efe904aa4ba0aff4a
|