Self-Programming AI: Code-Learning Agents for Autonomous Refactoring and Architectural Evolution — part 1/7

Cleaned-text code points [0, 13768). Bibliography omitted here; source.md includes it.

Self-Programming AI: Code-Learning Agents for
Autonomous Refactoring and Architectural
Evolution
Kushal Khemani 
Billabong High International School
Research Article
Keywords: self-programming AI, autonomous code generation, iterative code renement, large language
models, GPT-3.5-turbo, AST patching, LibCST, test-driven development, code quality metrics, cyclomatic
complexity, maintainability index, code coverage, multi-agent systems, explainable AI, software evolution,
Git integration, lifelong learning agents, AI software engineering, automated refactoring, self-improving
code agents
Posted Date: May 20th, 2025
DOI: https://doi.org/10.21203/rs.3.rs-6688473/v1
License:   This work is licensed under a Creative Commons Attribution 4.0 International License.  
Read Full License
Additional Declarations: No competing interests reported.

Self-Programming AI: Code-Learning Agents for 
Autonomous Refactoring and Architectural Evolution 
Abstract 
We present a novel implementation of a Self-Refining Programming Agent (SPA) designed to autonomously 
generate, test, and iteratively optimize Python code directly from natural-language sp ecifications. The SPA leverages 
OpenAI's GPT-3.5-Turbo model through structured function-calling to produce an initial implementation and 
corresponding test suite. It then enters a self-improvement loop that integrates static and d ynamic analysis tools, 
including pytest for correctness, coverage.py for test coverage, Radon f or cyclomatic complexity and maintainability 
metrics, and Pylint for code-quality enforcement. Based on the collected metrics, SPA autonomously suggests AST-
level refinements using LibCST transformations. To address common API limitations such as rate-limiting, we 
implemented an exponential backoff retry strategy, enhancing the robustness of agent interactions. Experimental 
evaluation demonstrates that the SPA consistently and autonomously refines generated code to meet rigorous 
software engineering standards, including high test coverage (>80%), low complexity, zero lint errors, and full 
compliance with specified functional requirements. This implementation represents a significant step toward fully 
autonomous, AI-driven software development workflows. 
Keywords - self-programming AI; autonomous code generation; iterative code refinement; large language models; 
GPT-3.5-turbo; AST patching; LibCST; test-driven development; code quality metrics; cyclomatic complexity; 
maintainability index; code coverage; multi-agent systems; explainable AI; software evolution; Git integration; 
lifelong learning agents; AI software engineering; automated refactoring; self-improving code agents 
The implementation code and simulation setups are publicly available at: https://github.com/Kushalk0677/Self-
Programming-AI-Code-Learning-Agents-for-Autonomous-Refactoring-and-Architectural-Evolution 
 
1. Introduction 
Software development is a resource-intensive process – maintenance and bug fixes alo ne can consume up to 90% of 
a project’s cost (estimated at $70 billion annually in the U.S.). This high cost has spurred interest in AI agents that 
can autonomously generate and improve code. Recent advances in large language models (LLMs) suggest that AI-
driven coding assistants could significantly enhance programmer productivity and make software development more 
accessible. For instance, OpenAI’s Codex model (built on GPT-3) can translate natural-language descriptions into 
code with notable success, solving ~28% of programming problems in a standard benchmark (and up to 70% with 
100 sampled attempts). Likewise, DeepMind’s AlphaCode system produced novel solutions to competitive 
programming challenges, achieving a roughly median human ranking (top 54.3% in Codeforces contests). These 
early successes demonstrate the potential of LLM-based code generation to handle non- trivial coding tasks. 
However, current code generation approaches have significant limitations. LLM-generated programs are typically 
produced in a single pass, without the iterative test-debug-refine cycle that human developers rely on. As a result, 
even advanced models often struggle on complex or unseen problems that require debugging, deeper reasoning, or 
algorithmic innovation beyond direct translation. Critically, today’s AI coding tools lack test-driven refinement – 
they rarely execute the code against test cases or use runtime feedback to improv e correctness. They also do little to 
optimize code quality after generating a working solution; issues of maintainability, efficiency, or architectural 
design are usually ignored. While proprietary systems like GPT-4’s Code Interpreter show the promise of integrating 
code execution and feedback (enabling an LLM to iteratively correct its outputs), such capabilities are not yet 
common in open models. In practice, human intervention is still needed to review, test, and refactor the code that 
LLMs produce.

There is a growing body of work exploring how AI agents might self-improve their code. In software engineering 
research, automated program repair techniques have demonstrated that it is possible to fix bugs automatically by 
leveraging test cases as a feedback signal. For example, GenProg uses a given test suite to evolve program variants 
until a patched version passes all tests, effectively automating bug fixes in legacy code. More generally, the concept 
of a self-programming AI – one that can modify its own code – has been discussed since the dawn o f AI research. 
Until recently this idea remained theoretical, but new evidence shows it is becoming feasible. Sheng  and 
Padmanabhan report the first practical implementation of a self-programming AI: a code generation model that 
successfully rewrites parts of its own source code to improve performance and even generates new helper 
functions autonomously. This progress hints that AI systems can not only write code, but also learn from the code 
they wrote and improve it in a closed loop. 
Still, no existing approach fully realizes an autonomous coding agent that can  continuously refine and re-architect 
its software. Current LLM-based coding systems do not perform holistic architectural evolution of programs over 
time, nor do they integrate metrics of code quality into their generation process. In short, there is a clear gap: we 
lack a general-purpose AI coder that can write a program, test it, evaluate it, and then iteratively improve the 
code based on quantitative feedback (coverage, complexity, style, etc.) as well as evolving requirements. Addressing 
this gap requires combining natural language understanding, program synthesis, software testing, and refactoring 
into a single autonomous workflow. 
In this paper, we propose a novel self-programming AI agent that aims to fill this void by autonomously generating 
and iteratively improving code. Our approach couples an LLM-based code generation pipeline with a closed-loop 
feedback and refactoring mechanism. In particular, the system we develop is characterized by the following key 
features: 
• Natural language-to-code pipeline – The agent uses OpenAI GPT-3.5 Turbo (with the new function-
calling API) to translate high-level problem descriptions into Python code. This LLM-driven translation 
provides the initial code implementation from a given natural-language specification, with  function-calling 
ensuring reliable, structured outputs for downstream processing. 
• Iterative, metric-driven refinement – Rather than one-shot generation, the agent improves the code 
through multiple iterations. After generating an initial solution, it executes the code and gathers feedback: 
unit test results (pass/fail and errors), test coverage data, and static analysis metrics. Code q uality metrics 
such as cyclomatic complexity and maintainability index are computed (via  Radon), and coding style issues 
are assessed (via Pylint scoring). Guided by these metrics and test outcomes, the agent instructs the LLM to 
revise the code, addressing bugs and reducing complexity or lint warnings. This loop repeats, using 
quantitative criteria to drive code refinement in a test-driven manner. 
• AST-level code patching – Code edits are applied through abstract syntax tree transformations 
using LibCST. LibCST preserves formatting and syntax details of the original code, enabling the agent to 
insert or modify code snippets safely without breaking the code structure or style. By operating on the AST, 
the agent can perform fine-grained refactorings (such as adding error-handling, renaming variables, or 
restructuring logic) programmatically. This approach yields controlled code changes that cumulatively 
evolve the program’s architecture. 
• Automated evaluation and logging – The framework integrates seamlessly with developer tools for 
continuous testing and evaluation. Pytest is invoked to run the test suite and generate coverage reports for 
each iteration. Radon computes static metrics (complexity, maintainability) and Pylint checks for adherence 
to coding standards, providing a score out of 10 for code quality. All results and metrics from each iteration 
are automatically logged using MLflow, an open-source experiment tracking system. This allows

monitoring the agent’s progress over time and provides a history of code versions, test outcomes, and 
metric improvements across iterations. 
• Robustness to API limits – To operate reliably in an autonomous fashion, the agent is designed to handle 
external API constraints gracefully. In particular, when calling the OpenAI code-generation API, it employs 
retry and exponential backoff strategies to mitigate rate limits or transient failures. This ensures that 
iterative refinement can proceed steadily even under stringent API usage conditions, making the system 
resilient in long-running sessions. 
By combining these components, our self-programming agent is able to incrementally enhance its own code in a 
way that imitates a human developer’s workflow: it writes an initial solution, tests it, then debugs and refactors the 
code based on the test results and quality metrics. The experimental results (detailed in later sections) show that this 
approach can autonomously boost code correctness (fixing failing tests) and improve code quality metrics over 
successive iterations. Importantly, the agent’s ability to integrate new feedback in each cycle points toward a system 
that learns from its own outputs. We argue that this work is a step toward general-purpose AI developers that can 
engage in lifelong code improvement. Such an AI would not only solve a given programming task, but continue 
to evolve the software architecture as requirements change or as better solutions emerge – akin to an engineer who 
never stops optimizing and learning. This vision aligns with the concept of lifelong learning in AI, which 
emphasizes an agent’s ability to continuously learn and adapt over time. In the long term, we foresee self-
programming AI agents that possess an ever-growing code base of their own creation, continuously refactoring and 
extending it to meet new challenges. The system presented here demonstrates the feasibility of this paradigm, laying 
groundwork for future research toward truly autonomous, self-evolving software. 
2. Related Work 
Code Generation via Language Models 
Pre-trained language models have rapidly advanced the state of code generation. OpenAI’s Codex (GPT-3 fine-
tuned on GitHub code) was among the first large-scale examples, solving 28.8% of problems in the HumanEval 
benchmark with a single sample and 70.2% with 100 samples. This dramatically outperformed earlier models (GPT-
3 solved 0%) and illustrated the value of sampling-based code synthesis. However, Codex still struggles with 
complex specifications (e.g. multi-step reasoning in docstrings) and variable binding, revealing limitations in deep 
code understanding. DeepMind’s AlphaCode pushed further by generating massive numbers of candidate programs 
and filtering them via testing. AlphaCode’s transformer-based system achieved a Codeforces contest rank in the top 
54%, marking the first competitive-level code generation result. It reaches this performa nce by synthesizing 
thousands of C++/Python solutions per problem and then clustering and selecting a small set of correct programs via 
test-based verification. This approach improves reliability but at the cost of extremely large sampling and execution 
overhead. 
Open-source code LMs have also emerged. PolyCoder (2.7B GPT-2 model trained on 249 GB of code across 12 
languages) demonstrates that smaller open models can approach proprietary systems; in fact, PolyCoder was 
reported to generate C code with higher accuracy than Codex. Similarly, CodeParrot (a 1.5B parameter GPT-2 
model for Python) and SantaCoder (a 1.1B model for Python, Java, JavaScript) were trained on massive GitHub 
datasets to democratize code generation research. CodeT5 takes a slightly different approach with an encoder–
decoder architecture that is identifier-aware. By leveraging the structure of code (e.g. distinguishing and predicting 
masked identifier names) and incorporating code comments for bimodal learning, CodeT5 achieved state-of-the-art 
results on several code understanding and generation tasks. These models conf irm that language models can learn 
syntax and even semantics of code to a remarkable degree. Limitations: Despite their successes, code LMs typically 
generate programs in a single pass without iterative refinement. They often require brute-force sampling to obtain 
correct solutions (as seen with Codex and AlphaCode) and cannot verify or improve their outputs on their own. This