greenai.guide

Home › How do I…? › How do I get AI to write energy-efficient code?

How do I…?

How do I get AI to write energy-efficient code?

Updated · 1 min read

In short

Ask explicitly for efficient code and name the constraint, such as memory, allocations or algorithmic complexity, then run the tests and measure the code's energy before accepting it. Research found prompt changes could make generated code up to 91.9% more energy-efficient, but often at the cost of correctness. Give the assistant energy principles to follow, and keep a person checking the result.

Steps

  1. Ask for efficient code and name the constraint that matters, such as memory or database round trips.
  2. Give the assistant the full problem description, not a trimmed one.
  3. Run the test suite on everything it produces.
  4. Measure energy or at least runtime and memory before and after on the same machine.
  5. Give the assistant a written set of energy rules or anti-patterns to check against.

Ask for efficiency explicitly, then verify it. AI assistants write code that passes tests but can be wasteful: loops with database calls inside, needless copies, repeated work.

What does the evidence show?

Ionescu, Jensen and Thomsen's EnergyBench scores AI-written code for both correctness and energy. They found that changing the prompt could make generated code up to 91.9% more energy-efficient, but often at the cost of accuracy. In some cases, removing half the problem description made code over four times more efficient while correctness dropped to zero. Efficient but wrong is worth nothing.

How do I prompt for it?

  • Name the constraint. "Avoid a database query inside the loop", "stream the file rather than loading it into memory", "keep this O(n log n)".
  • Keep the full specification. Don't trim the problem to save tokens; that is where correctness goes.
  • Give it rules. GreenCode researchers published the GreenCode Constitution, a set of energy principles and anti-patterns an AI coding agent can follow: profile the code, fix ranked anti-patterns, measure again and keep only changes that save energy.

Common mistakes

  • Trusting "optimised" in the reply. Measure on the same machine under the same load.
  • Treating faster as greener. The GreenCode work found energy and speed can diverge; measure both. See how to measure the energy AI uses on your own computer.
  • Skipping review of generated code you didn't write.

For measurement methods, read how to measure the energy and carbon of AI.

Sources

  1. EnergyBench: holistic benchmark for correct and energy-efficient LLM-generated code, Ionescu, Jensen and Thomsen, FLTA 2025 (2025)
  2. GreenCode Constitution: agent principles for optimizing code and LLM inference energy use, Kapenekakis, Kim, Ionescu, Thomsen and Albano, GreenAI workshop, ECML PKDD 2026 (2026)
  3. Web Sustainability Guidelines, W3C (2025)

Join us

Help cut AI's impact on the planet

Get new guides, funding calls and directory listings by email, about once a month. Or add your product, course, PhD or research to the directories.

No tracking. Unsubscribe in one click. Other ways to join · Privacy