AIResearch AIResearch
Back to articles
AI

AI Now Writes Safer Code on Its Own

AI now writes secure code that fixes its own vulnerabilities automatically. See how this innovation prevents software breaches without human intervention.

AI Research
November 14, 2025
2 min read
AI Now Writes Safer Code on Its Own

A new method called RefleXGen enables artificial intelligence systems to write more secure computer code without requiring extensive human intervention or specialized training data. This breakthrough addresses a critical challenge in software development, where AI-generated code often contains vulnerabilities that could lead to system failures or security breaches.

Researchers discovered that by guiding large language models through self-reflection cycles combined with retrieval-augmented generation techniques, AI systems can significantly improve the security of their code outputs. The approach allows models to identify and fix potential vulnerabilities in their own generated code through iterative optimization processes.

RefleXGen operates through a three-stage workflow. First, the AI generates initial code based on user requirements. Then, it performs self-reflection to identify potential defects or security issues. If problems are detected, the system enters an optimization phase where it retrieves relevant security knowledge and applies repairs. The improved code is then stored for future reference, creating a growing knowledge base that enhances subsequent generations.

Experimental results across four major AI models demonstrated substantial security improvements. GPT-3.5 Turbo showed a 13.6% increase in security rate, GPT-4o improved by 6.7%, CodeQwen-1.5 increased by 4.5%, and Gemini-1.0 Pro achieved a 5.8% security enhancement. The testing used challenging scenarios based on Common Weakness Enumerations, featuring nine of MITRE's most dangerous software vulnerabilities across both C/C++ and Python programming environments.

Figure 2 in the paper illustrates how RefleXGen particularly benefited models with stronger dialogue capabilities, showing significant improvements in handling high-risk vulnerability scenarios. The method proved most effective for CodeQwen, which demonstrated substantial enhancements in scenarios prone to triggering severe security risks.

This advancement matters because AI code generation tools are becoming increasingly prevalent in software development. According to the 2023 GitHub Annual Report cited in the paper, nearly 92% of developers are already using or experimenting with AI programming assistants. The ability to generate more secure code automatically could reduce the time developers spend on code review and vulnerability patching while making software development more accessible to non-expert programmers.

The approach has limitations, particularly for models with weaker dialogue handling capabilities. The introduction of restrictive conditions and added complexity in some scenarios led to decreased success rates for certain models, as noted in the paper's conclusion. Additionally, while RefleXGen significantly improves security rates, it doesn't eliminate all vulnerabilities, and the method's effectiveness varies depending on the specific AI model and programming context.

About the Author

Guilherme A.

Guilherme A.

Former dentist (MD) from Brazil, 41 years old, husband, and AI enthusiast. In 2020, he transitioned from a decade-long career in dentistry to pursue his passion for technology, entrepreneurship, and helping others grow.

Connect on LinkedIn