Inferensys

Glossary

CodeCarbon

An open-source software package that estimates the carbon footprint of computing operations by tracking hardware power consumption and applying regional grid emission factors, integrated directly into Python pipelines.
Research scientist tracking AI experiments on laptop, experiment results visible, casual lab environment.
EMISSIONS TRACKING PACKAGE

What is CodeCarbon?

CodeCarbon is an open-source Python library that programmatically estimates the carbon footprint of computational workloads by tracking hardware power consumption and applying real-time regional grid emission factors.

CodeCarbon is a lightweight, open-source software package that integrates directly into Python pipelines to estimate the carbon dioxide equivalent (CO2eq) emissions of computing operations. It functions by wrapping code execution, monitoring hardware power draw from CPUs, GPUs, and RAM, and multiplying that energy consumption by the marginal carbon intensity of the local electrical grid using a global emissions data API.

Designed for Green AI and GreenOps workflows, the tool outputs emissions data in a dashboard or as structured logs, enabling data scientists and MLOps engineers to compare the climate impact of different model architectures, cloud regions, and training durations. It supports carbon-aware scheduling by revealing the emissions differential between compute locations, directly facilitating compliance with Scope 2 and Scope 3 greenhouse gas reporting under the GHG Protocol.

EMISSIONS TRACKING

Key Features of CodeCarbon

CodeCarbon is an open-source Python library that provides granular, programmatic estimation of the carbon footprint of computing operations by tracking hardware power consumption and applying regional grid emission factors.

01

Hardware-Aware Power Tracking

CodeCarbon monitors real-time power draw from the physical hardware executing the code, not just theoretical thermal design power (TDP).

  • CPU Tracking: Queries Intel RAPL (Running Average Power Limit) interfaces on Linux and Intel Power Gadget on macOS/Windows for direct socket-level energy measurement.
  • GPU Tracking: Integrates with NVIDIA Management Library (NVML) via pynvml to sample actual GPU power draw in watts during CUDA kernel execution.
  • RAM Monitoring: Estimates memory power consumption as a linear function of allocated capacity, accounting for the baseline and dynamic power states of DRAM modules.
  • Fallback Strategy: If hardware interfaces are unavailable, defaults to a conservative TDP-based estimation model using the processor's rated maximum power multiplied by a utilization coefficient.
Intel RAPL
CPU Interface
NVML
GPU Interface
02

Regional Grid Emission Mapping

CodeCarbon translates raw energy consumption (kWh) into carbon dioxide equivalent (CO2eq) by applying a regional carbon intensity factor specific to the geographic location of the compute infrastructure.

  • Global Coverage: Maintains a public CSV dataset of carbon intensities for all countries and major cloud regions, sourced from the Carbon Intensity API and the Global Energy Monitor.
  • Cloud Provider Detection: Automatically infers the compute region for major cloud providers (AWS, Azure, GCP) by parsing instance metadata endpoints, eliminating manual configuration.
  • Private Infrastructure: For on-premise data centers, users can manually specify a country code or a custom carbon intensity value (gCO2eq/kWh) to override the automatic detection.
  • Dynamic Updates: The emission factor dataset is versioned and can be updated independently of the library to reflect real-world grid decarbonization trends.
gCO2eq/kWh
Emission Unit
03

Decorator-Based Integration

CodeCarbon is designed for minimal friction integration into existing Python data science and machine learning pipelines using a simple decorator pattern.

  • @track_emissions Decorator: Wrapping a training function with this decorator automatically starts an emissions tracker before execution and stops it upon completion, logging the total footprint.
  • Context Manager: The EmissionsTracker class can be used as a context manager (with EmissionsTracker() as tracker:) for fine-grained control over the measurement window within a script.
  • Task-Level Granularity: Supports tracking multiple discrete tasks within a single process, allowing users to isolate the footprint of data preprocessing, training, and evaluation phases separately.
  • Minimal Overhead: The power sampling loop runs in a background thread with a configurable interval (default 15 seconds), adding negligible computational overhead to the primary workload.
15s
Default Sampling Interval
04

Structured Emissions Output

CodeCarbon generates structured, machine-readable output artifacts that feed directly into sustainability reporting and data engineering pipelines.

  • CSV Logging: Appends per-run emission data to a local CSV file, including project name, timestamp, duration, energy consumed (kWh), and emissions (CO2eq).
  • Dashboard Visualization: Provides an optional web dashboard (codecarbon visualize) that renders interactive charts of cumulative emissions over time from the logged CSV data.
  • Prometheus Integration: Can expose emission metrics as a Prometheus endpoint, enabling integration with existing cloud-native monitoring stacks like Grafana for real-time GreenOps dashboards.
  • JSON Export: Supports exporting the final emission summary as a JSON object for programmatic consumption by CI/CD pipelines or internal carbon accounting APIs.
CSV
Log Format
Prometheus
Metrics Export
05

Cloud & CI/CD Pipeline Support

CodeCarbon is architected to function in ephemeral, headless compute environments common in modern MLOps workflows.

  • Offline Mode: When the machine has no internet access, the tracker uses a cached local copy of the emission factors database, preventing pipeline failures in air-gapped environments.
  • CI/CD Integration: Designed to run inside GitHub Actions, GitLab CI, or Jenkins pipelines to track the carbon cost of automated model retraining and report it as a build artifact.
  • Logging Callbacks: Provides hooks to stream emission data to external logging services or custom data sinks, enabling integration with enterprise ELK (Elasticsearch, Logstash, Kibana) stacks.
  • Stateless Operation: Each run is independent and appends to the central log, requiring no persistent daemon or database, which aligns with the stateless nature of containerized workloads.
CODECARBON EXPLAINED

Frequently Asked Questions

Clear, technical answers to the most common questions about tracking and estimating the carbon footprint of computational workloads using the CodeCarbon open-source package.

CodeCarbon is an open-source software package that estimates the carbon footprint of computing operations by tracking hardware power consumption and applying regional grid emission factors, integrated directly into Python pipelines. It operates as a lightweight background process that samples the power draw of key components—primarily the CPU, GPU, and RAM—at regular intervals during code execution. The tool then multiplies the measured energy consumption (in kilowatt-hours) by the marginal carbon intensity of the local electrical grid where the computation is physically running. This grid data is sourced from public APIs like WattTime or static global averages. The result is a real-time or post-hoc report of CO₂-equivalent emissions (kgCO₂eq) attributed to a specific script, function, or training run. Unlike cloud provider dashboards that rely on billing data, CodeCarbon provides hardware-level granularity, making it a critical tool for Green AI practitioners and engineers implementing Software Carbon Intensity (SCI) measurements.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.