TLDR: A new research paper evaluates the security of code generated by large language models (LLMs) and introduces ‘Reflexion,’ an iterative self-correction method. The study found that 25-33% of initial LLM-generated code contains vulnerabilities, with Python yielding the most secure code and C/C# the least. Reflexion prompting significantly improves security, increasing average accuracy from 70.74% to 79.43% over three rounds, with most gains in the first round. This suggests that LLMs can learn to identify and fix their own security flaws through iterative feedback.
Large language models (LLMs) have become indispensable tools for developers, assisting in everything from drafting new code to refactoring existing projects. However, a critical question arises: is the code generated by these powerful AI models truly secure? A recent research paper, Secure Code Generation at Scale with Reflexion, delves into this very challenge, evaluating the security of LLM-generated code and proposing an iterative self-correction method called ‘Reflexion’ to enhance security.
The Challenge of Insecure Code
The core issue is that while LLMs are trained on vast codebases, these datasets often contain design flaws and vulnerabilities. Consequently, LLMs can inadvertently carry over these insecurities into the code they generate. Early studies highlighted this problem, showing that a significant portion of LLM-generated code could be insecure. This led to the development of various security benchmarks to evaluate code quality, but many were limited in scope, focusing on a single programming language or a narrow set of vulnerability types.
To address these limitations, benchmarks like CyberSecEval and its refined version, Instruct Prime, were introduced. Instruct Prime, used in this study, offers a comprehensive dataset of 1,404 instruction-style prompts across eight programming languages (C, C++, C#, Java, JavaScript, Python, PHP, and Rust) and covers 50 distinct Common Weakness Enumeration (CWE) types, providing a more robust foundation for evaluation.
Introducing Reflexion: A Path to Self-Correction
Most prior evaluations of secure code generation relied on single-turn prompting, where the LLM gets only one chance to produce secure code. This paper introduces and evaluates a ‘Reflexion’ prompting approach. Inspired by how humans learn from mistakes, Reflexion allows LLMs to iteratively review and revise their own code. In this process, the model receives feedback from an Insecure Code Detector (ICD) – a static analysis tool that flags vulnerabilities and their associated CWEs. This feedback is then incorporated into a revised prompt, guiding the LLM to correct insecure practices over multiple rounds.
The researchers compared a zero-shot baseline (no feedback) with a three-round Reflexion approach, measuring improvements (Repairs), new vulnerabilities introduced (Regressions), and the overall NetGain in security.
Key Findings: Insecurity and Improvement
The study’s findings reveal several important trends. Firstly, insecurity remains a common problem in initial code generations. At the zero-shot baseline, roughly 25-33% of programs generated by the five evaluated LLMs (Qwen2.5-Coder-32B-Instruct, Codestral-22B-v0.1, CodeLlama-34B-Instruct, DeepSeek-Coder-33B-Instruct, and OpenCoder-8B-Instruct) contained vulnerabilities. Weak cryptography and configuration-dependent bugs were particularly difficult for models to avoid, while more templated vulnerabilities like Cross-Site Scripting (XSS), code injection, and hard-coded secrets were handled more reliably.
Regarding programming languages, Python consistently yielded the highest secure code rates, while C and C# had the lowest. Java, JavaScript, PHP, and C++ fell in the middle, suggesting that dynamic, higher-level languages are easier for LLMs to handle securely than lower-level or type-sensitive ones.
Secondly, the Reflexion prompting approach significantly improved code security across all models. On average, secure-generation accuracy increased from 70.74% at the baseline to 79.43% after three rounds of Reflexion. The most substantial gains occurred in the first round of feedback, with diminishing returns in subsequent rounds. This indicates that early feedback is highly effective in helping models correct the majority of issues. QwenCoder and Codestral showed the largest improvements, demonstrating that Reflexion helps models make more consistent use of feedback.
Also Read:
- Unlocking Trust: How to Improve Large Language Models’ Self-Confidence in Code Reasoning
- Balancing Code Generation: How SELF-REDRAFT Helps AI Explore and Refine Solutions
Implications for Secure AI-Assisted Coding
This research underscores that while LLMs are powerful coding assistants, their outputs require careful scrutiny for security vulnerabilities. The Reflexion strategy offers a practical, training-free mechanism to enhance the security of generated code. The findings suggest that a short, focused Reflexion loop (one to two rounds) can capture most of the benefits, providing meaningful security improvements without excessive computational overhead.
The study’s detailed breakdown by CWE type and programming language also highlights areas where further investment is needed, particularly in improving secure defaults and curating high-quality training data for cryptographic and memory safety vulnerabilities. Integrating targeted Reflexion with existing static and dynamic security checks could further elevate the security performance of AI-generated code, moving us closer to a future where AI not only writes functional code but also secure code by default.


