Tutorials

How to Track AI Data Center Power Usage

Build a lightweight Python pipeline to monitor real-time power draw and carbon intensity for local AI model inference workloads.

AM
Alfian Majid
//6 min read
How to Track AI Data Center Power Usage

Why Local Power Monitoring Matters for AI Workloads

Every single token you stream from GPT-5.6 Sol or a local Llama 4 instance carries a measurable energy footprint. Recent infrastructure shifts in Texas and across major power grids mean energy costs are no longer an afterthought. If you run heavy transformer models locally or orchestrate multi-agent loops via LangGraph, your electricity bill will spike. Let us build a practical monitoring pipeline using Python, Prometheus, and a custom script that reads thermal and power metrics directly from your hardware.

You will need a dedicated workstation or a server node equipped with an NVIDIA GPU, because that is where the vast majority of inference power gets burned. We are going to poll the system management interface every five seconds, calculate watt-hours per million tokens, and stream those telemetry points to a local dashboard. No corporate fluff here. Just raw telemetry code you can drop into your CI or local dev environment right now.

What Hardware and Software You Need

Before writing code, let us establish the baseline environment. Do not attempt this on an underpowered laptop. You need direct kernel-level access to hardware sensors.

  • Operating System: Ubuntu 24.04 LTS or Debian 12 with a low-latency kernel.
  • Python Version: Python 3.12 or higher.
  • Hardware: NVIDIA GPU with nvidia-smi support (RTX 4090, H100, or similar enterprise/consumer silicon), plus an Intel or AMD CPU with RAPL (Running Average Power Limit) driver enabled.
  • Python Libraries: pynvml, prometheus-client, psutil, and requests.
  • Access: Root or sudo privileges to read hardware energy counters via /sys/class/powercap/.

Run the following terminal command to install your core dependencies before moving on to the script:

pip install pynvml prometheus-client psutil requests==2.31.0

How to Write the Power Telemetry Collector Script

We need a persistent worker script that queries the NVIDIA Management Library for GPU power draw, reads CPU package power from the Linux kernel powercap interface, and exposes these metrics over an HTTP endpoint. Create a file named power_tracker.py and paste the following implementation.

import time
import os
import psutil
from pynvml import nvmlInit, nvmlDeviceGetHandleByIndex, nvmlDeviceGetPowerUsage, nvmlShutdown
from prometheus_client import start_http_server, Gauge

# Initialize Prometheus metrics
GPU_POWER_GAUGE = Gauge('ai_gpu_power_watts', 'Current GPU power consumption in watts', ['gpu_index'])
CPU_POWER_GAUGE = Gauge('ai_cpu_power_watts', 'Current CPU power consumption in watts')
SYSTEM_RAM_GAUGE = Gauge('ai_system_ram_usage_bytes', 'Current system RAM usage in bytes')

def read_cpu_rapl_power():
    path = '/sys/class/powercap/intel-rapl:0/energy_uj'
    if not os.path.exists(path):
        return 0.0
    try:
        with open(path, 'r') as f:
            energy_start = int(f.read().strip())
        time.sleep(1)
        with open(path, 'r') as f:
            energy_end = int(f.read().strip())
        # energy is in microjoules, delta over 1 second gives microwatts -> divide by 1e6 for watts
        return (energy_end - energy_start) / 1000000.0
    except Exception:
        return 0.0

def main():
    print('[*] Initializing NVML subsystem...')
    nvmlInit()
    handle = nvmlDeviceGetHandleByIndex(0)
    
    print('[*] Starting Prometheus metrics server on port 9102...')
    start_http_server(9102)
    
    try:
        while True:
            # GPU power usage comes in milliwatts from NVML
            gpu_milliwatts = nvmlDeviceGetPowerUsage(handle)
            gpu_watts = gpu_milliwatts / 1000.0
            GPU_POWER_GAUGE.labels(gpu_index='0').set(gpu_watts)
            
            # CPU power via RAPL
            cpu_watts = read_cpu_rapl_power()
            CPU_POWER_GAUGE.set(cpu_watts)
            
            # RAM usage
            ram_bytes = psutil.virtual_memory().used
            SYSTEM_RAM_GAUGE.set(ram_bytes)
            
            time.sleep(5)
    except KeyboardInterrupt:
        print('[*] Shutting down power tracker...')
        nvmlShutdown()

if __name__ == '__main__':
    main()

How to Run and Verify the Telemetry Pipeline

Now that your script is written, execute it in your terminal while running an active inference workload like a local LLM benchmark or a heavy agentic loop. Open a separate terminal tab to verify that the Prometheus endpoint is actively scraping data.

python3 power_tracker.py

You should see standard startup logs indicating that the NVML subsystem has locked onto your GPU device and the HTTP server is bound to port 9102. To check the live output, run a curl command against your local metrics loop:

curl -s http://localhost:9102/metrics | grep ai_

Your terminal should return real-time floating-point values representing current wattage across your hardware stack. If your GPU is idle, you will likely see around 15 to 30 watts. Kick off a heavy token generation run, and watch that metric instantly jump to 350 or 450 watts depending on your hardware tier.

What Are the Most Common Hardware Access Failures?

Building low-level telemetry scripts always exposes environment quirks. Here are three common errors you will encounter and how to fix them immediately in the terminal.

  • NVML Library Not Found: If you see NVMLError: Driver Not Loaded, your NVIDIA proprietary drivers are either missing or the kernel module was detached. Fix this by reinstalling the driver toolkit or running sudo nvidia-smi to verify hardware recognition.
  • Permission Denied on RAPL: Reading Intel or AMD RAPL energy counters in /sys/class/powercap/ often requires root permissions. If your CPU power metric returns 0.0, execute your script with elevated privileges using sudo python3 power_tracker.py or adjust udev rules for your user group.
  • Prometheus Port Collision: If port 9102 is already bound by another exporter on your infrastructure node, pass a custom port argument to start_http_server() or terminate the conflicting PID using sudo lsof -i :9102 followed by sudo kill -9 [PID].

How to Handle Production Rate Limits and Security

Deploying this monitoring architecture across multiple production nodes requires disciplined configuration management. Never expose your Prometheus telemetry port directly to the public internet. Always bind your metrics server to an internal VPC interface or localhost, and scrape it securely using a centralized Prometheus or Grafana Agent instance running within your trusted cluster network.

Also, avoid polling hardware sensors faster than once every two to five seconds. Aggressive polling intervals via NVML can introduce micro-stutter in heavy transformer inference pipelines, slightly degrading your tokens-per-second throughput. Keep your logging asynchronous and decouple the monitoring loop from your core model execution threads.

What to Build Next for Advanced Infrastructure

Once you have basic wattage tracking functional, extend this pipeline into a full cost-optimization daemon. You can hook the Prometheus metrics into an automated scaling script that pauses non-essential background vector searches or multi-agent evaluations when local grid carbon intensity peaks or electricity spot prices spike. Combine this script with a Grafana dashboard to visualize watt-hours per successful API response, giving your engineering team precise unit economics on every AI model deployment.

Bottom Line

Power consumption is the ultimate bottleneck for modern AI infrastructure. Ignoring energy telemetry means flying blind while your electricity bills skyrocket during long inference runs. Deploy this monitoring script today, tie it into your internal dashboards, and enforce hard power ceilings across your development clusters before grid instability impacts your uptime.

Share this article

Alfian Majid
Alfian Majid/Founder & Editor-in-Chief

Solo developer and blogger from Indonesia. Runs CogitoDaily as a passion project - covering AI news, testing tools, and writing guides. Background in web development and game tech. When not writing about AI, you'll find me deep in anime or gaming.