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
- Ask for efficient code and name the constraint that matters, such as memory or database round trips.
- Give the assistant the full problem description, not a trimmed one.
- Run the test suite on everything it produces.
- Measure energy or at least runtime and memory before and after on the same machine.
- 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
- EnergyBench: holistic benchmark for correct and energy-efficient LLM-generated code, Ionescu, Jensen and Thomsen, FLTA 2025 (2025)
- GreenCode Constitution: agent principles for optimizing code and LLM inference energy use, Kapenekakis, Kim, Ionescu, Thomsen and Albano, GreenAI workshop, ECML PKDD 2026 (2026)
- Web Sustainability Guidelines, W3C (2025)
Related
How do I use an AI coding assistant without a big footprint?
How developers can keep the benefit of AI coding assistants while cutting wasted tokens: right-size the model, focus the context, break tasks down.
How do I measure the energy AI uses on my own computer or GPU?
Measure local AI energy with hardware counters, GPU telemetry or a plug-in meter, sample over the whole job, and subtract the idle baseline.
How to measure the energy and carbon of AI
A practical guide to metering or estimating the energy and carbon of AI work: tools, system boundaries, per-task metrics and honest uncertainty.
Agentic AI and token waste
Why AI agents can use many times the tokens and energy of a chat, how loops waste them, and the guardrails that keep agents right-sized.