Inspiration

Modern software development moves fast — but efficiency is often overlooked. Developers unintentionally write code that consumes more CPU cycles, memory, and energy than necessary.

As AI begins to assist developers in writing code, we saw an opportunity to go further: What if AI could not only write code, but actively improve it for performance and sustainability?


What It Does

LeafyAI is an AI-powered GitLab agent that:

  • Analyzes code in Merge Requests
  • Automatically fixes inefficient code
  • Generates test cases
  • Assigns a Green Score based on efficiency
  • Posts structured feedback directly in GitLab

Instead of just suggesting improvements, the agent takes action — modifying code and improving quality in real time.


How We Built It

🧠AI Layer

We used Anthropic (Claude) to:

  • Detect inefficient patterns
  • Generate optimized code
  • Provide explanations
  • Create test cases

GitLab Integration

We integrated directly with GitLab using:

  • Merge Request triggers (CI/CD pipeline)
  • GitLab REST API for:

    • Reading files
    • Updating code (auto-fix)
    • Creating new files (tests)
    • Posting MR comments

Automation Flow

Merge Request Created
        ↓
Agent triggers via GitLab CI
        ↓
Code sent to Claude for analysis
        ↓
Returns:
- issues
- optimized code
- test cases
        ↓
Agent:
✔ updates code
✔ generates tests
✔ calculates Green Score
✔ posts MR comment

Green Score Logic

We designed a simple scoring system to reflect code efficiency:

$$ \text{Green Score} = 100 - \sum (\text{inefficiency penalties}) $$

Where:

  • Inefficient loops → −10
  • Redundant operations → −10
  • Poor structure → −5

This provides a clear, interpretable metric for developers to track improvements.


Example

Before

for i in range(len(arr)):
    print(arr[i])

After

for item in arr:
    print(item)

Fewer operations Better performance Higher Green Score


Challenges We Faced

1. Parsing AI Responses

Ensuring Claude returned clean JSON consistently was tricky. We had to design structured prompts and fallback parsing.


2. GitLab API Integration

Handling:

  • file encoding
  • branch-specific updates
  • authentication

required careful debugging.


3. Auto-Fix Safety

Automatically modifying code is powerful — but risky. We ensured:

  • changes are minimal
  • explanations are provided
  • context is preserved

4. Defining “Green Score”

Measuring real energy usage is complex. We simplified it by mapping efficiency → energy impact, making it practical for developers.


What We Learned

  • How to build event-driven AI agents
  • Deep integration with GitLab CI/CD pipelines
  • Using AI beyond chat — as an autonomous actor
  • Designing systems that balance automation + control

Impact

LeafyAI transforms AI from a passive assistant into an active development partner.

It helps developers:

  • write better code
  • reduce inefficiencies
  • improve performance
  • and contribute to more sustainable software

Future Improvements

  • Real-time energy estimation models
  • Multi-file optimization
  • Historical Green Score tracking
  • Integration with cloud cost metrics

Conclusion

LaefyAI demonstrates that the future of development is not just faster — but smarter and more responsible.

“The best code isn’t just correct — it’s efficient, sustainable, and continuously improving.”


Built With

Share this project:

Updates