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

> How to prompt AI coding tools for efficient code, why efficient-but-wrong is worthless, and how to test and measure generated code before you accept it.

Source: https://greenai.guide/how-do-i/get-ai-to-write-energy-efficient-code/  
Updated: 2026-11-19

## 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](/how-do-i/measure-energy-of-ai-on-my-own-computer/).
- **Skipping review** of generated code you didn't write.

For measurement methods, read [how to measure the energy and carbon of AI](/practice/measure-ai-energy/).

## Sources

- [EnergyBench: holistic benchmark for correct and energy-efficient LLM-generated code](https://doi.org/10.1109/flta67013.2025.11336593), Ionescu, Jensen and Thomsen, FLTA 2025 (2025)
- [GreenCode Constitution: agent principles for optimizing code and LLM inference energy use](https://greencode.ai/publications/greencode-constitution-agent-principles-for-optimizing-code-and-llm-inference-en/), Kapenekakis, Kim, Ionescu, Thomsen and Albano, GreenAI workshop, ECML PKDD 2026 (2026)
- [Web Sustainability Guidelines](https://www.w3.org/TR/web-sustainability-guidelines/), W3C (2025)
