LevDetectCode: Zero-Shot Detection of AI-Generated Code Using Levenshtein Distance
AI-generated code is spreading rapidly, making it harder to trace the true origins of code. Text-based tools such as DetectGPT perform well on texts but struggle with code. We propose a new approach: under masked reconstruction, code produced by LLMs exhibits a much wider spread in Levenshtein distance than human-written code. Our key innovation is to treat this Levenshtein distance dispersion as an indicator for token-probability differences, leading us to develop LevDetectCode. Our zero-shot detector relies solely on string-level Levenshtein distance, requiring neither GPUs nor access to model internals. Experiments on Python snippets from MBPP-train and HumanEval, as well as Java data from CodeContest_Java_test, using GPT-3.5, GPT-4, and Deepseek-V3, show a 5–10 point AUROC improvement over other zero-shot baselines, with an additional case study for the C[Formula: see text] dataset, which also demonstrates strong effectiveness. Furthermore, it runs nearly 3000 times faster and significantly reduces memory usage. These results demonstrate that Levenshtein distance can outperform other zero-shot methods that rely on model-based computations, offering a practical and portable solution for detecting AI-generated code.
Authors
- Guohua Shen (ORCID: https://orcid.org/0000-0003-2182-0019)
- Zhiqiu Huang (ORCID: https://orcid.org/0000-0001-6843-1892)
- Yaoshen Yu (ORCID: https://orcid.org/0000-0003-0609-7985)
- Yu Zhao (ORCID: https://orcid.org/0000-0001-7776-3178)
- J. L. Fu (ORCID: https://orcid.org/0009-0000-1860-6092)
Institutions
- Nanjing University of Aeronautics and Astronautics (CN)
Publication Details
- Journal
- International Journal of Software Engineering and Knowledge Engineering
- Published
- 2025-09-04
- DOI
- https://doi.org/10.1142/s0218194025500524
- Primary Topic
- Digital Media Forensic Detection
- Type
- article
- Field-Weighted Citation Impact
- 0.00