EcoCoderAgentCore: A Green Software Engineering AI Agent

What Inspired Us

The world is increasingly aware of the environmental impact of technology. While much of the focus has been on hardware and data center efficiency, the software that runs on this hardware also has a significant carbon footprint. Inefficient code consumes more CPU cycles, which in turn consumes more energy. As developers, we have a responsibility to write code that is not only functional and efficient but also sustainable. With AI and LLMs based software, this becomes even more relevant for now.

This realization was the primary inspiration for EcoCoderAgentCore. We wanted to create a tool that would empower developers to make environmentally conscious decisions directly within their existing workflows. The idea was to build an AI agent that could automatically analyze code for its environmental impact, providing actionable insights to reduce its carbon footprint. We envisioned a future where "green code" is a standard part of software quality, just like bug-free, security first or well-documented code.

What It Does

EcoCoderAgentCore is an intelligent AI agent that integrates directly into the GitHub pull request workflow. When a developer creates or updates a pull request, our agent begins a comprehensive analysis and delivers a "Green Code Report" as a comment.

The key features include:

  • Carbon Footprint Analysis: Using the CodeCarbon library, the agent estimates the CO2 emissions of the code changes based on performance profiling. It takes into account the energy consumption of the underlying hardware and the carbon intensity of the AWS region where the analysis is run.

  • LLM-Powered Code Review: The agent uses a Large Language Model to perform a deep static analysis of the code, identifying potential bugs, style issues, and areas for improvement that go beyond traditional linters.

  • Enhanced Performance Profiling: This is where EcoCoderAgentCore is particularly powerful. Instead of just static analysis, our agent:

    1. Discovers Tests: It intelligently identifies relevant test files associated with the code changes in the pull request.
    2. Executes in a Real Environment: It runs these tests in an isolated AWS CodeBuild environment with the AWS CodeGuru Profiler attached.
    3. Analyzes Performance: It analyzes the profiler output to identify CPU bottlenecks and memory usage patterns, providing a real-world performance grade.
  • Automated Reporting: All these insights are compiled into a single, easy-to-read Markdown report posted directly to the pull request, giving developers immediate feedback.

How We Built It

Building EcoCoderAgentCore was a project that involved modern cloud and AI technologies. Our architecture is built for scalability, security, and robustness.

  • Core Logic: The core of our agent is built using the Strands SDK and is deployed on the AWS Bedrock AgentCore Runtime. This allowed us to focus on the agent's logic and tools while AWS handles the orchestration, scaling, and container management.

  • Webhook Entrypoint: We used AWS SAM (Serverless Application Model) to define and deploy our public-facing infrastructure. An API Gateway receives webhooks from GitHub, which triggers a Lambda function (ecocoder-core-entry). This Lambda function acts as a bridge, validating the webhook and invoking our agent running in the Bedrock AgentCore Runtime.

  • Agent Tools: The agent's capabilities are modularized into four distinct Python tools:

    • llm_code_reviewer.py: Interacts with Bedrock's LLMs for code analysis.
    • codeguru_profiler.py: Manages the CodeBuild jobs and analyzes CodeGuru Profiler data.
    • codecarbon_estimator.py: Calculates the carbon footprint.
    • github_poster.py: Posts the final report back to GitHub.
  • Infrastructure as Code: The entire project is defined as code. We have a Dockerfile for our agent's container, a template.yaml for our SAM application, and various shell scripts for easy deployment and setup.

Challenges We Faced

  1. IAM Permissions: As with any non-trivial AWS project, getting the IAM permissions just right was a significant challenge. The agent's role needed permissions for Bedrock, CodeBuild, CodeGuru, S3, Secrets Manager, and Systems Manager. The setup-permissions.sh script was born out of the necessity to automate this complex setup.

  2. AI Test Discovery: Creating an algorithm that could reliably discover the correct tests for a given set of changed files was complex. We had to account for different naming conventions (test_*.py, *_test.py), project structures, and test types (unit, integration).

  3. Asynchronous Operations: The performance profiling is a long-running process. The agent invokes a CodeBuild job and then has to wait for it to complete. Managing this asynchronous flow and providing timely feedback was a puzzle we had to solve.

  4. Making Sense of Data: The CodeGuru Profiler provides a wealth of data. The challenge was to distill this information into simple, actionable insights for the developer. We had to process the raw profiling data and combine it with our own heuristics to generate a meaningful performance grade and recommendations.

What We Learned

This project was a great learning experience.

  • Green Software is Complex: We learned that estimating the carbon footprint of code is not a simple task. It involves understanding hardware power consumption, CPU utilization, and regional carbon intensity.
  • The Power of Hybrid Analysis: Combining the deep pattern recognition of LLMs with the real-world execution data from a profiler yields results that are far more powerful than either tool on its own.
  • Serverless AI Agents are the Future: Using Bedrock AgentCore Runtime demonstrated how powerful and scalable it is to deploy containerized AI agents. The separation of the agent's logic from the underlying infrastructure is a game-changer.
  • The Importance of a Good Developer Experience: We realized that for a tool like this to be adopted, it has to be seamless. That's why we focused on integrating directly into the GitHub workflow and providing clear, concise reports.

What's Next

We are just beginning to explore what's possible with EcoCoderAgentCore. Roadmap may include:

  • Support for More Languages: Expanding beyond Python to include Java, JavaScript, and more.
  • Deeper Integrations: Integrating with other static analysis tools and security scanners.
  • Real-time Carbon Data: Using real-time carbon intensity data for even more accurate footprint calculations.
  • IDE Plugins: Bringing the green code analysis directly into the developer's IDE for even faster feedback.

I believe that EcoCoderAgentCore is a step towards a future of more sustainable software development.

Built With

  • agentcore
  • apigateway
  • codebuild
  • codecarbon
  • lambda
  • python
  • ssmparameter
  • strands
  • webhook
Share this project:

Updates