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.
Glossary
CodeCarbon
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.
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.
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.
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
pynvmlto 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.
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.
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_emissionsDecorator: 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
EmissionsTrackerclass 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.
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.
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.
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.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Core concepts and complementary tools that form the sustainable AI measurement and optimization landscape.

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.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us