mlco2/codecarbon
Track emissions from Compute and recommend ways to reduce their impact on the environment.
CodeCarbon — Track the CO₂ emissions of your computer
What it is: A free, open-source Python library that estimates how much carbon dioxide your computer's computing produces, so you can measure and reduce the environmental impact of your code.
What it does
CodeCarbon watches your computer while your code runs and estimates the CO₂ emissions from that work. It does this in two steps:
- Estimates electricity use — It measures or estimates the power consumed by your CPU, GPU, and RAM (the main compute components).
- Applies regional carbon intensity — It multiplies that electricity by the carbon intensity of the region where your computer is located (how much CO₂ is produced per unit of electricity in that area).
What it does not track: Disk I/O, network transfers, displays, cooling, or other peripherals. The README explains these are usually much smaller for local code-level experiments and aren't exposed through the same low-overhead measurement interfaces.
Key features
- Simple Python API — Start a tracker, run your code, stop the tracker, get emissions in kg CO₂.
- Command-line tool — Track a command without changing your code (
codecarbon monitor --no-api -- python train.py), or detect your hardware (codecarbon detect). - Visual outputs — See your emissions on a web dashboard or locally with a tool called carbonboard, with real-world equivalents to put the numbers in context.
- Flexible configuration — Set options via a global config file (
~/.codecarbon.config), a project-local file (./.codecarbon.config), environment variables (CODECARBON_*), or Python arguments.
How accurate is it?
On Linux, CodeCarbon reads Intel RAPL hardware energy counters when they're available — these are real hardware measurements. If those counters can't be read, it falls back to estimating from CPU load, which is less accurate. The README points to documentation on how to enable the accurate counters and on how close the numbers are to reality.
Quick example (Python)
from codecarbon import EmissionsTracker
tracker = EmissionsTracker()
tracker.start()
# Your code here
emissions = tracker.stop()
print(f"Emissions: {emissions} kg CO₂")
Important distinction: local vs. remote AI
CodeCarbon measures local computing — emissions from your own hardware. If you're calling remote GenAI APIs (OpenAI, Anthropic, Mistral, etc.), the README points you to a complementary tool called EcoLogits for tracking those emissions instead. The two tools work together.
Community and support
- Open source and community-driven, with contribution guidelines and an active Discord server.
- Originally developed by volunteers from Mila, DataForGoodFR, with donated time from Comet.ml and BCG GAMMA engineers.
- Now supported by Code Carbon, a French non-profit organization.
- Sponsored by Clever Cloud, Data For Good, GitHub, and Mozilla.
- Published on Zenodo with a DOI (version 3.3.2) for citing in research.
Installation
pip install codecarbon
(Conda users: activate your environment, then pip install codecarbon.)
Bottom line
CodeCarbon is a lightweight tool that answers one question: How much CO₂ did my code just emit? It's designed for developers and researchers who want to quantify and reduce the carbon footprint of their local computing, especially in machine learning contexts.
Related
- Project
- Project
- Project
- Dispatch
- Project