As you probably integrate Artificial Intelligence (AI) more and more into your study routine, it comes with a hidden cost that you might not immediately consider when prompting ChatGPT
: energy consumption and its associated carbon footprint. Although we’ve previously discussed the energy usage of AI models, you might still be wondering: “How much CO2 am I actually responsible for when using AI?” or “What are my CO2 ChatGPT emissions?” Understanding this impact can lead to more environmentally conscious choices.
AI Environmental Costs: This article focuses on the CO2 emissions when using ChatGPT during specific interactions. This omits other significant environmental impacts when using AI models, such as: Model Training: Training AI models consumes substantial energy and heavily contributes to their overall carbon footprint. Data Storage and Transfer: Storing and transferring large datasets requires energy, adding to the environmental cost. Infrastructure: The data centers and servers powering AI also have their own environmental footprint, from construction to maintenance. Read more in this article.
You can measure your environmental impact, specifically in terms of CO2 emissions, directly using CodeCarbon
. This open-source software package integrates into a Python
codebase and tracks the amount of carbon dioxide (CO2) produced by the cloud or local computing resources used to execute your code. This means that if you are using the API, it will indicate the CO2 impact of your prompt.
CodeCarbon
isn’t just solely created for AI emissions tracking; it’s a tool for anyone running code inside Python
. Whether you’re developing machine learning models, running simulations, or processing large datasets, CodeCarbon
can help you understand and reduce your carbon emissions.
How to Use CodeCarbon
to Track Your AI’s Carbon Emissions
Here is a step-by-step manual on how to set up your computer and Python
script up for integrating CodeCarbon
:
Step 1: Install CodeCarbon
on your computer
To begin, you’ll need to install CodeCarbon
.
- Open your command line interface
- The
Terminal
onmacOS
andCommand Prompt
onWindows
- The
- run the following command:
pip install codecarbon
This command will download and install the CodeCarbon
package, setting you up to monitor emissions right away. Think of this as installing a personal CO2 calculator for your Python
scripts!
Step 2: Incorporate CodeCarbon within Your Python Code
With CodeCarbon
installed, you’re ready to start tracking.
Let’s walk through how you can incorporate this tool into a Python
script that interacts with the OpenAI API.
Step 2a: Import Necessary Libraries:
At the top of your Python
script, you’ll need to import a few libraries:
codecarbon
to track emissions,os
to handle environment variables where your OpenAI API key is stored,openai
to interact with the OpenAI API.
Your script will start like this:
from openai import OpenAI
import os
from codecarbon import EmissionsTracker
Don’t have an OpenAI API key
yet? No worries! Check out our guide to setting up the OpenAI API in Python.
Step 2b: Set Up the OpenAI Client and EmissionsTracker:
Now, it’s time to initialize the tools. First, set up OpenAI client
using your API key, which should be stored securely as an environment variable (OPENAI_API_KEY
). Next, create an instance of the EmissionsTracker
class to start monitoring emissions:
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
tracker = EmissionsTracker()
tracker.start()
By calling the start()
method, you activate the emissions tracker, which will now keep an eye on the energy consumption of your script.
Step 3: Run Your AI Task
With everything set up, you’re ready to run your AI task! Run the AI task you want to measure. For instance, here’s how you can use the OpenAI
API to generate a response based on a prompt:
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": """I want you to do the following things:
- Tell a good joke in the form of a question. Do not yet give the answer.
- Provide a brief summary of environmental tips.
- Explain the concept of batch processing in AI."""}
]
)
print(completion.choices[0].message.content)
Check out our article for tips on reducing your AI emissions! With tips such as batching prompts, just like we’ve done here!
Step 4: Stop the Tracker and Display Emissions:
Once your AI task is complete, stop the emissions tracker and display the estimated CO2 emissions:
emissions = tracker.stop()
print(f"Estimated CO2 emissions for this batch of AI tasks: {emissions} kg")
The stop()
method halts the tracker and calculates the total emissions generated during the script’s execution. The result is printed, showing the estimated CO2 emissions in kilograms.
Full Template
Here’s the full Python
script, combining all the steps we’ve outlined:
from openai import OpenAI
import os
from codecarbon import EmissionsTracker
# Initialize OpenAI client
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
# Set up the emissions tracker
tracker = EmissionsTracker()
tracker.start()
# Create a chat completion request
completion = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "user", "content": """I want you to do the following things:
- Tell a good joke in the form of a question. Do not yet give the answer.
- Provide a brief summary of environmental tips.
- Explain the concept of batch processing in AI."""}
]
)
# Extract and format the output message
output_message = completion.choices[0].message.content
# Print the output
print("### AI Response ###\n")
print(output_message)
print("\n### End of Response ###\n")
# Stop the tracker and print the estimated emissions
emissions = tracker.stop()
print(f"Estimated CO2 emissions for this batch of AI tasks: {emissions:.6f} kg")
This script will track the emissions for the interactions you run through the API. This means you have an exact statement of your environmental impact.
When it comes to the environmental impact of AI, different models have varying levels of CO2 emissions. For example, running the same batch of AI tasks of our prompt template results in the following estimated emissions:: GPT-3.5 Turbo: 0.000003 kg of CO2. GPT-4: 0.000019 kg of CO2 and GPT-4 Optimized (GPT-4o): 0.000006 kg of CO2. These numbers highlight a key point: smaller models like GPT-3.5 Turbo tend to use less energy and therefore produce fewer CO2 emissions. In contrast, more complex models like GPT-4, have significantly higher emissions.