Uncovering LLM-Generated Code: A Zero-Shot Synthetic Code Detector via Code Rewriting

Tong Ye, Yangkai Du, Tengfei Ma, Lingfei Wu, Xuhong Zhang, Shouling Ji, Wenhai Wang

Introduction

The rapid adoption of Large Language Models (LLMs) like GPT-4 and GitHub Copilot has transformed software development. However, this shift brings new challenges in academic integrity and software provenance. Distinguishing between human-written and AI-generated (synthetic) code is now a critical necessity. While many detectors exist for natural language, they often falter when applied to the rigid, structured world of programming languages.

The Challenge of Low Token Entropy

A primary reason traditional AI detectors fail on code is the nature of programming syntax. In natural language, a single concept can be expressed in countless ways, leading to high token entropy. In contrast, programming languages are governed by strict rules. Keywords like def, if, and return are highly predictable, resulting in "low-entropy" tokens.

Figure 1: Predictive entropy distribution for code versus general texts, highlighting the high concentration of low-entropy tokens in programming languages.

As shown in Figure 1, over 70% of code tokens fall into the lowest entropy bracket (0.0-1.0). This creates a significant overlap in the statistical signatures of human and AI-written code, making "white-box" methods—which rely on log-probabilities—unreliable for code detection.

Methodology: Detection through Rewriting

To overcome the entropy hurdle, researchers have proposed a novel approach: Zero-Shot Detection via Code Rewriting. Instead of analyzing individual token probabilities, this method examines the "global style" and structural consistency of a code snippet.

The process involves three main steps:

  1. Code Rewriting: A "Detector LLM" is tasked with explaining and then rewriting the input code. This "Chain-of-Thought" prompting ensures the model captures the underlying logic before generating a variant. To maintain efficiency, the method achieves high accuracy with as few as 4 to 8 rewrites.
  2. Similarity Modeling: The original code and its rewritten version are compared using a specialized similarity model. The researchers fine-tuned GraphCodeBERT using SimCSE (Simple Contrastive Learning of Sentence Embeddings) to capture semantic similarity, even when formatting or variable names differ.
  3. Classification: The core hypothesis is that LLMs exhibit a "self-consistency bias." When an LLM rewrites its own generated code, the output remains structurally similar. Conversely, when it rewrites human code, it tends to "translate" the human style into its own preferred patterns, leading to a significant divergence.
Figure 5: Examples comparing original human-written code with LLM-rewritten versions, illustrating the structural transformations that occur during the rewriting process.

Experimental Evaluation

The method was evaluated across three major benchmarks: APPS (complex prompt-to-code tasks), MBPP (entry-level programming), and Code Contest (C++ testing for cross-language generalizability). Synthetic samples were generated using a diverse set of models, including CodeLlama-13B, StarChat-Alpha, GPT-3.5-Turbo, and GPT-4.

Key Results

The rewriting-based detector significantly outperformed state-of-the-art baselines like DetectGPT and LogRank:

  • Performance Gains: The method achieved a 20.5% improvement in AUROC on the APPS benchmark and a 29.1% improvement on MBPP.
  • Robustness: The detector remained effective even when the Detector LLM differed from the Generator LLM.
  • Language Agnostic: High performance was maintained in C++ (AUROC ~89%), suggesting the underlying logic of stylistic consistency holds across different programming paradigms.

Discussion and Limitations

The success of this method lies in its ability to exploit the inherent stylistic biases of LLMs. While humans write code with diverse structural patterns, LLMs tend to converge on specific "comfort zones."

However, some limitations remain. The method is computationally more expensive than simple statistical checks due to the need for LLM inference. Additionally, while the similarity model is robust, simple adversarial attacks like aggressive identifier renaming can still impact performance. Interestingly, the study found that "correct" synthetic code is harder to detect, likely because the constraints of a working solution force the AI to mimic human logic more closely.

Conclusion

By shifting the focus from token-level statistics to global structural consistency, this rewriting-based approach provides a powerful new tool for identifying synthetic code. Its zero-shot, black-box nature makes it highly adaptable to the evolving landscape of large language models.

Summary updated

BibTeX
@inproceedings{ye2025uncovering,
  author = {Tong Ye and Yangkai Du and Tengfei Ma and Lingfei Wu and Xuhong Zhang and Shouling Ji and Wenhai Wang},
  title = {{Uncovering LLM-Generated Code: A Zero-Shot Synthetic Code Detector via Code Rewriting}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2025},
  doi = {10.1609/aaai.v39i1.32082},
}
Figure