Find helpful tutorials or share inspiring use cases on AI technology for higher education.

Shared content may not reflect the policies of Tilburg University on the use of AI. 

5 Practical API Techniques to Lower Your AI Environmental Footprint

It is important to find a good balance where AI can add value, but without neglecting concerns about the environment. Therefore, being well-informed about the energy consumption of AI helps you to find a middle ground between these conflicting values. For this reason, we have outlined various methods here to guide you on what you can personally do to reduce the external environmental costs of AI.

5 Practical Tips to Reduce Your AI Environmental Footprint

Reflect on Whether You Need AI for Each Task

The most straightforward way to reduce the environmental impact of AI is to stop AI usage.

This option overlooks the added value that AI can provide. Therefore, it is important to evaluate whether AI usage is truly beneficial carefully. This means being selective and critical about when and where to use AI solutions like OpenAI‘s models. Before calling the API, consider some reflective example questions we have created. These questions will help you think about which types of calls are beneficial for using AI and which ones can be handled by other methods, such as a simple Google web search.

If you’re concerned about the CO2 emissions from AI, learn how to track and reduce your AI’s carbon footprint with our guide on CodeCarbon right here.

Reflective Questions whether AI Usage is Necessary
  • Is the question simple and factual?

    • Yes: Consider using a simple web search or checking course materials. For example, “What year did World War II start?” or “What is the formula for calculating the area of a circle?” can often be quickly found in textbooks, lecture notes, or a quick web search, like Bing and Google.

    • No: AI usage might be beneficial if the question requires more depth or synthesis of information.

  • Is the answer to my question already available in an existing course document, textbook, or syllabus?
    • Yes: Check those resources first. If the answer is available in your lecture notes, course textbook, or class slides, there’s no need to use AI.
    • No: AI usage can be beneficial if the information is not readily accessible.
  • Does the problem require deep contextual understanding or creative problem-solving?
    • Yes: AI might be helpful if the assignment involves tasks like summarizing complex readings or analyzing different perspectives in a research paper.
    • No: Consider simpler alternatives like asking classmates, joining a study group, or consulting your professor during office hours.
  • Is the question time-sensitive or needs constant updates?
    • Yes: If your question is about something that changes often, like news, weather, or current events: Use a web search. AI might not have the latest information.
    • No: If your question is about something that doesn’t change much, like historical facts, definitions, or general knowledge: Ask AI. It’s great for answering questions based on well-known, reliable information.
  • Is the answer you are seeking prone to opinions or other subjective content?
    • Yes: Engaging with peers in discussion forums, reading course reviews, or participating in study groups may provide more diverse and relatable insights.
    • No: If objective information or a structured explanation is needed (e.g., understanding a specific concept), AI could be of use. Keep in mind that AI responses may vary in accuracy.
  • Is the computational cost justified by the expected benefit?
    • Yes: If the task’s benefit significantly outweighs the energy cost (like complex data analysis), using AI can be justified.
    • No: Opt for a simpler, less energy-intensive solution.
  • Can a less powerful AI model be used instead of the AI model you intend to use?
    • Yes: For example, if you’re considering using GPT-4 for a simple query, consider whether a smaller model like GPT-3.5 could handle the task just as effectively. For straightforward or less complex questions, a smaller model will suffice, conserving both time and computational resources.
    • No: If a task is highly complex or requires advanced reasoning, using a more powerful model might be justified.

Use the Right AI Models for Your Needs

Larger AI models, such as GPT-4, offer advanced capabilities, and can handle a wide range of complex tasks. However, they also consume significantly more computational resources, leading to higher CO2 emissions. To minimize environmental impact, it’s important to use these models only when their advanced features truly add value. For simpler tasks, like generating short texts, captions, or headlines, consider using smaller models like GPT-3.5 or even lighter AI tools, which are more energy-efficient and still sufficient for basic needs.

Available AI Models and Their Suggested Uses
  • gpt-3.5-turbo: This model is efficient and budget-friendly.
    • Best for: General tasks that don’t need the most advanced features. It’s great for simple to moderately complex text, casual conversations, and straightforward questions.
    • Examples: Basic factual questions, explaining terminology, drafting emails, summarizing short articles, quick Q&A.
  • gpt-4o (o stands for optimized): A version of GPT-4 designed to save costs and increase efficiency.
    • Best for: General tasks that need GPT-4’s robustness while keeping energy use and cost low. Ideal for complex tasks that don’t need the full power of GPT-4.
    • Examples: Writing sophisticated articles, generating high-quality summaries, and handling in-depth prompt iterations.
  • gpt-4: The most advanced model with the highest capabilities.
    • Best for: Very complex and creative tasks that need deep understanding, complex reasoning, and lots of context.
    • Examples: Complex research, advanced programming help, detailed terminology explanations.
Python
from openai import OpenAI
client = OpenAI()

completion = client.chat.completions.create(
  model="gpt-3.5-turbo",  # Here you can change the model of use
  messages=[
    {"role": "user", "content": "ENTER YOUR PROMPT HERE"}
  ],
  max_tokens= 150  
)

print(completion.choices[0].message)

Reuse and Recycle AI Responses

When interacting with AI models, generating a new response for every repeated question can lead to unnecessary computational costs, increased energy consumption, and higher expenses. The solution is simple: instead of having the AI model generate a fresh response each time, we can store responses for future use. When a repeated prompt is detected, the system can check for an existing response and reuse it. This method reduces redundant API calls, cutting both energy usage and financial costs.

A Simple Template for Storing AI Responses

Here’s a Python code template that demonstrates this concept:

Python
from openai import OpenAI  # Import the OpenAI library
import os  # Import the os library to access environment variables

# Initialize the OpenAI client using your API key stored in an environment variable
openai.api_key = os.getenv('OPENAI_API_KEY')

def get_response(prompt):
    # Attempt to find and reuse a saved response
    try:
        with open("saved_responses.txt", "r") as file:
            saved_responses = file.readlines()
            for line in saved_responses:
                if prompt in line:
                    print("Reusing saved response to reduce computation.")
                    response_index = line.find("Response: ")
                    if response_index != -1:
                        return line[response_index + len("Response: "):].strip()  
    except FileNotFoundError:
        # If the file doesn't exist, proceed to generate a new response
        pass  

    # Generate a new response if none was found
    print("Generating new response from AI model.")
    response = openai.ChatCompletion.create(
        model="gpt-4",  
        messages=[{"role": "user", "content": prompt}],
        temperature=0  
    )
    response_text = response.choices[0].message.content

    # Save the new response for future use
    with open("saved_responses.txt", "a") as file:
        file.write(f"Prompt: {prompt}\nResponse: {response_text}\n---\n")

    return response_text

# Example usage
prompt = "ENTER YOUR PROMPT HERE"
response = get_response(prompt)
print("AI Response:", response)

By storing previously generated responses, this approach avoids the need to call the AI model repeatedly for identical queries. Reusing responses saves computational resources and reduces the environmental footprint associated with energy-intensive AI operations. This method is particularly effective when certain prompts are likely to be repeated across multiple users or over time.

Opportunities for Improvement

While this template may seem basic and not fully ready for large-scale deployment, its purpose is to engage you in higher-level thinking and ideas for further development and optimization. In practice, users often ask a wide range of unique prompts, but this approach still provides valuable opportunities for improvement:

  • Leveraging Similarity Matching: Instead of just matching identical prompts, consider a more advanced approach like embeddings. This would let the system detect and link prompts that are, say, 85% similar, providing a consistent response even with slight variations. This would reduce the number of times the AI model needs to generate a new answer, improving both efficiency and consistency.
  • Scaling with Databases for Chatbots: For chatbots with a large user base, many prompts are likely to be repeated across different users. By storing these prompts and their corresponding responses in a database, you can efficiently track commonly asked questions and recycle the answers. This approach is not only computationally efficient but also environmentally friendly, as it reduces the need for repeated processing and conserves resources.

Limit Output Length of AI Models

A key advantage of using AI models like ChatGPT via an API is the ability to control the length of the generated responses by setting a maximum token limit. Tokens represent chunks of text (like words or phrases), and by restricting the number of tokens, you can keep the responses brief.

Environmentally, this would result in:

  • Reduced Computational Load: Limiting the token count decreases the processing power required, resulting in quicker responses. This can be helpful when you need rapid results, such as last-minute revision summaries before exams.
  • Energy Efficiency: Shorter outputs consume less computational energy, contributing to greener practices.

When retrieving short responses can be beneficial: to generate brief summaries of lengthy research articles, chapters, or study notes to quickly grasp the main points. Produce short explanations or answers for quick study sessions, ideal for flashcards or self-quizzing. or get concise, relevant examples or definitions to support lectures, presentations, or discussions.

Practical Implementation

Here’s how you can implement this concept in Python using the OpenAI API, focusing on limiting the token count:

Python
from openai import OpenAI
client = OpenAI()

completion = client.chat.completions.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "user", "content": "ENTER YOUR PROMPT HERE"}
  ],
  max_tokens= 150  # Limit the maximum tokens for the model's response
)

print(completion.choices[0].message)

Two birds with one stone: Thanks to the pay-for-use system of the API, conserving energy also translates into saving money.

Batch Your Prompts for Efficient AI Usage

When prompting AI models, each prompt consumes computational power, indirectly contributing to energy consumption and environmental impact. One effective way to cut these costs is by batching prompts, grouping multiple questions or tasks into a single request. For students, this can be a practical strategy to reduce the carbon footprint associated with AI usage while staying productive.

Why Batching Prompts Matters for Sustainability

  • Lower Energy Consumption: Every time you make an API call, it requires server resources and energy. By combining multiple queries into a single call, you reduce the number of requests, cutting down on overall energy use.
  • Decreased Carbon Footprint: Fewer API requests mean less computational work, which translates into lower carbon emissions.
  • Cost-Effective Learning: Beyond environmental benefits, batching prompts can also save money by reducing the total number of calls made to an AI service.
How to Implement Batching

Here’s an example of how you can batch multiple prompts into one request using Python and the OpenAI API:

Python
from openai import OpenAI # Import the OpenAI package
import os # Import the os package

client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
            {"role": "user", "content": 
            
            """Please complete the following tasks and clearly restate each
            prompt before providing the answer:

            1. PROMPT 1
            2. PROMPT 2
            3. PROMPT 3
            4. PROMPT 4
            
            Format the responses as follows:
            
            Response 1: 
            Prompt: [Restate the first prompt]
            Answer: [Your answer here]
            
            Response 2:
            Prompt: [Restate the second prompt]
            Answer: [Your answer here]
            
            Response 3:
            Prompt: [Restate the third prompt]
            Answer: [Your answer here]
            
            Response 4:
            Prompt: [Restate the fourth prompt]
            Answer: [Your answer here]
            """}
        ],
)

print(response.choices[0].message.content)

Plan Your Queries: Before making an API call, list all the questions or tasks you need help with. Group them logically so the AI’s response is clear and well-organized.

Conclusion: Embrace Mindful AI Use

You have probably used the benefits of AI technology in academic settings, offering advantages like efficient data analysis, quick access to information, and creative problem-solving support. However, these benefits come with environmental costs, such as high energy consumption and CO2 emissions. We at tilburg.ai believe that finding a balance between leveraging AI’s capabilities and minimizing its ecological footprint is important.

That’s why this article has provided practical strategies to help you reduce your AI environmental footprint:

  • Reflect on the Necessity of AI Usage: Evaluate whether using AI is essential for each task. If simpler alternatives like web searches, textbooks, or discussions with peers suffice, opt for those options to reduce unnecessary energy consumption.
  • Use Appropriate AI Models: Choose models that are best suited for your specific needs. For example, use smaller, less resource-intensive models like GPT-3.5 for simpler queries, and reserve advanced models like GPT-4 for complex tasks that truly require their capabilities.
  • Reuse and Recycle AI Responses: Store and reuse AI-generated responses whenever possible, especially for repetitive questions. This reduces the number of API calls and lowers the associated energy consumption.
  • Limit Output Length: Control the length of AI-generated responses by setting token limits. Shorter responses require less computational power, thus reducing both energy usage and response time.
  • Batch Your Prompts: Combine multiple questions or tasks into a single request to minimize the number of API calls. This not only reduces computational resources and energy consumption but also saves time and money.

By adopting these strategies, you can reduce the environmental impact of AI while still benefiting from its innovative capabilities. Remember, every conscious decision you make regarding AI usage contributes to more sustainable technology practices. Embrace mindful AI use.