Automatic Detection of LLM-Generated Code: A Comparative Case Study of Contemporary Models Across Function and Class Granularities
Organizations: Concordia University, Montréal, QC, Canada · University of Calgary, Calgary, AB, Canada
Abstract
The adoption of Large Language Models (LLMs) for code generation risks incorporating vulnerable code into software systems. Existing detectors face two critical limitations: a lack of systematic cross-model validation and opaque "black box" operation. We address this through a comparative study of code generated by four distinct LLMs: GPT-3.5, Claude 3 Haiku, Claude Haiku 4.5, and GPT-OSS. Analyzing 14,485 Python functions and 11,913 classes from the CodeSearchNet dataset, we generated corresponding code with all four LLMs. Using interpretable software metrics, we trained CatBoost classifiers for each configuration. Our analysis reveals that granularity effects dominate model differences by a factor of 8.6, with negligible feature overlap, indicating that function-level and class-level detection rely on fundamentally disjoint structural signatures. We discover critical granularity-dependent inversions: while modern models (Claude, GPT-OSS) are more detectable at the class level, GPT-3.5 is an anomaly that uniquely excels at the function level. SHAP analysis identifies the Comment-to-Code Ratio as the sole universal discriminator. However, its predictive magnitude varies drastically across models, explaining why detectors trained on specific LLMs fail to generalize. Our findings demonstrate that GPT-3.5's exceptional detectability (AUC-ROC 0.96) is unrepresentative of contemporary models (AUC-ROC approximately between 0.68 and 0.80). Robust detection requires moving beyond single-model studies to account for substantial diversity in structural fingerprints across architectures and granularities.
Figures & tables
| Stage | Function-Level | Class-Level |
| 1. Data Source | CodeSearchNet Dataset | Classes from same OSS Projects |
| 2. Initial Sample | 20,000 standalone functions | 20,000 standalone classes |
| 3. Code Generation | Generate with 4 LLMs: GPT-3.5, GPT-OSS, | |
| Claude 3 Haiku, Claude Haiku 4.5 | ||
| 4. Validation | Verify valid Python syntax using ast parsing | |
| 5. Intersection Filter | Keep only snippets successfully generated by ALL 4 LLMs | |
| Category | Metrics | Supporting literature |
| Comment-related stylometry | Comment Lines, Average Comment Lines, Comment to Code Ratio | Reported as discriminative in prior work ( Idialu et al., 2024 ; Shi et al., 2025 ) . |
| Complexity | Cyclomatic, Modified, Strict, and Strict Modified Cyclomatic Complexity, Essential Complexity, Maximum Nesting Depth, Paths, Logarithmic Paths | Reported as a measure of control-flow complexity ( McCabe, 1976 ) and as a project-dependent predictor of post-release defects ( Nagappan et al., 2006 ) . |
| Structural-count stylometry | Lines, Code Lines, Blank Lines, Declarative and Executable Code Lines, Statements, Declarative and Executable Statements | Reported as discriminative in prior work ( Idialu et al., 2024 ) ; RLHF reported to increase output length outside code ( Singhal et al., 2023 ) . |
| Granularity | Total Features | Model | p 0.01 | & Non-Negligible |
| Function | 18 | Claude 3 Haiku | 18 (100%) | 3 (17%) |
| Claude Haiku 4.5 | 17 (94%) | 13 (72%) | ||
| GPT-3.5 | 18 (100%) | 17 (94%) | ||
| GPT-OSS | 18 (100%) | 18 (100%) | ||
| Class | 39 | Claude 3 Haiku | 36 (92%) | 23 (59%) |
| Claude Haiku 4.5 | 21 (54%) | 2 (5%) |
| GPT-3.5 | GPT-OSS | Claude 3 Haiku | Claude Haiku 4.5 | ||
| Precision | GPTZero | 0.71 | 0.85 | 0.75 | 0.78 |
| CatBoost | 0.94 [0.93, 0.95] | 0.73 [0.71, 0.75] | 0.63 [0.61, 0.64] | 0.66 [0.64, 0.68] | |
| Recall | GPTZero | 0.29 | 0.64 | 0.34 | 0.42 |
| CatBoost | 0.87 [0.86, 0.88] | 0.71 [0.69, 0.73] | 0.64 [0.63, 0.66] | 0.66 [0.65, 0.68] | |
| F1-Score | GPTZero | 0.41 | 0.73 | 0.47 | 0.55 |
| CatBoost | 0.90 [0.89, 0.91] | 0.72 [0.71, 0.73] | 0.64 [0.62, 0.65] | 0.66 [0.65, 0.68] |
| GPT-3.5 | GPT-OSS | Claude 3 Haiku | Claude Haiku 4.5 | ||
| Precision | GPTZero | 0.65 | 0.86 | 0.71 | 0.72 |
| CatBoost | 0.78 [0.76, 0.79] | 0.72 [0.70, 0.74] | 0.72 [0.70, 0.74] | 0.78 [0.77, 0.80] | |
| Recall | GPTZero | 0.15 | 0.51 | 0.20 | 0.21 |
| CatBoost | 0.83 [0.81, 0.84] | 0.75 [0.73, 0.76] | 0.78 [0.76, 0.79] | 0.61 [0.59, 0.63] | |
| F1-Score | GPTZero | 0.25 | 0.64 | 0.31 | 0.32 |
| CatBoost | 0.80 [0.79, 0.81] | 0.73 [0.72, 0.75] | 0.75 [0.73, 0.76] | 0.68 [0.67, 0.70] |
| Granularity | Comparison (A vs. B) | AUC A | AUC B | AUC | Significant |
| Function | GPT-3.5 vs Claude 3 Haiku | 0.961 | 0.681 | +0.280 | Yes |
| GPT-3.5 vs Claude Haiku 4.5 | 0.961 | 0.714 | +0.247 | Yes | |
| GPT-3.5 vs GPT-OSS | 0.961 | 0.795 | +0.166 | Yes | |
| GPT-OSS vs Claude Haiku 4.5 | 0.795 | 0.714 | +0.081 | Yes | |
| GPT-OSS vs Claude 3 Haiku | 0.795 | 0.681 | +0.114 | Yes | |
| Claude Haiku 4.5 vs Claude 3 Haiku | 0.714 | 0.681 | +0.033 | Yes |
| Model | Function AUC | Class AUC | |
| Claude 3 Haiku | 0.681 | 0.830 | +0.149 |
| Claude Haiku 4.5 | 0.714 | 0.806 | +0.092 |
| GPT-3.5 | 0.961 | 0.887 | -0.074 |
| GPT-OSS | 0.795 | 0.808 | +0.013 |
| Comparison Scope | Fixed Factor | Avg. Jaccard | Stability |
| Cross-Model | Class Level | 0.855 | High |
| Cross-Model | Function Level | 0.690 | Moderate |
| Cross-Granularity | Model Architecture | 0.099 | Negligible |
| Model | Granularity | Comment | Complexity | Structural-count |
| Claude 3 Haiku | Function | 29.5% | 28.6% | 41.9% |
| Claude Haiku 4.5 | Function | 37.2% | 21.7% | 41.1% |
| GPT-3.5 | Function | 64.5% | 18.9% | 16.5% |
| GPT-OSS | Function | 48.5% | 41.3% | 10.2% |
| Claude 3 Haiku | Class | 19.9% | 34.5% | 45.6% |
| Claude Haiku 4.5 | Class | 26.9% | 14.5% | 58.6% |
| Feature | Frequency | Avg. SHAP | Max. SHAP | Top 3 |
| Comment to Code Ratio | 8/8 | 0.777 | 3.795 | 7 |
| Average Blank Lines | 4/8 | 0.703 | 1.807 | 2 |
| Declarative Code Lines | 1/8 | 0.631 | 0.631 | 1 |
| Average Code Lines | 1/8 | 0.603 | 0.603 | 1 |
| Average Comment Lines | 3/8 | 0.594 | 0.740 | 3 |
| Maximum Nesting Depth | 6/8 | 0.401 | 1.034 | 2 |
| GPT-3.5 | GPT-OSS | Claude 3 Haiku | Claude Haiku 4.5 | ||
| Rank 1 | Feature | Comment-to- Code Ratio | Comment Lines | Comment-to- Code Ratio | Declarative Statements |
| SHAP | 3.80 | 0.57 | 0.34 | 0.62 | |
| Rank 2 | Feature | Maximum Nesting | Essential Complexity | Blank Lines | Comment Lines |
| SHAP | 0.70 | 0.42 | 0.29 | 0.38 | |
| Rank 3 | Feature | Declarative Code Lines | Comment-to- Code Ratio | Declarative Statements | Comment-to- Code Ratio |
| SHAP | 0.63 | 0.32 | 0.19 | 0.18 |
| GPT-3.5 | GPT-OSS | Claude 3 Haiku | Claude Haiku 4.5 | ||
| Rank 1 | Feature | Maximum Nesting | Comment-to- Code Ratio | Average Code Lines | Average Blank Lines |
| SHAP | 1.03 | 0.55 | 0.60 | 1.81 | |
| Rank 2 | Feature | Average Comment Lines | Modified Classes | Modified Classes | Average Comment Lines |
| SHAP | 0.74 | 0.47 | 0.34 | 0.73 | |
| Rank 3 | Feature | Comment-to- Code Ratio | Average Blank Lines | Average Comment Lines | Comment-to- Code Ratio |
| SHAP | 0.54 | 0.36 | 0.32 | 0.28 |
| Model | Granularity | Baseline AUC | Adversarial AUC | AUC |
| Claude 3 Haiku | Function | 0.681 [ 0.667, 0.694 ] | 0.985 [0.981, 0.988] | +0.304 |
| Claude 3 Haiku | Class | 0.830 [0.818, 0.841] | 0.858 [0.848, 0.868] | +0.028 |
| Claude Haiku 4.5 | Function | 0.714 [0.700, 0.726] | 0.994 [0.993, 0.996] | +0.280 |
| Claude Haiku 4.5 | Class | 0.806 [0.795, 0.817] | 0.788 [0.775, 0.801] | –0.018 |
| GPT-3.5 | Function | 0.961 [0.956, 0.965] | 1.000 [0.999, 1.000] | +0.039 |
| GPT-3.5 | Class | 0.887 [0.879, 0.896] | 0.903 [0.894, 0.911] | +0.015 |
| Rank | Feature | Baseline SHAP | Adversarial SHAP | % | |
| 1 | Comment to Code Ratio | 0.55 | 1.04 | +0.49 | +90% |
| 2 | Coupled Classes (Modified) | 0.47 | 0.41 | -0.06 | -13% |
| 3 | Average Blank Lines | 0.36 | 0.58 | +0.22 | +62% |
| Rank | Feature | Baseline SHAP | Adversarial SHAP | % | |
| 1 | Declarative Statements | 0.62 | 0.54 | -0.08 | -13% |
| 2 | Comment Lines | 0.38 | 1.04 | +0.66 | +172% |
| 3 | Comment to Code Ratio | 0.18 | 0.57 | +0.39 | +220% |
Appendix figures & tables13 assets
Supplementary material from the paper’s appendix.
Appendix
| Metric | Definition |
| Code Stylometry Metrics | |
| All Methods | Total number of methods in a class, including inherited methods |
| Average Blank Lines | Mean number of blank lines per method in a class |
| Average Code Lines | Mean number of code lines per method in a class |
| Average Comment Lines | Mean number of comment lines per method in a class |
| Average Lines | Mean total number of lines per method in a class |
| Granularity | Model A | Model B | AUC A | AUC B | AUC | -value | Significant | |
| Function | GPT-3.5 | GPT-OSS | 0.961 | 0.795 | 0.166 | 25.52 | Yes | |
| GPT-3.5 | Claude Haiku 4.5 | 0.961 | 0.714 | 0.247 | 34.99 | Yes | ||
| GPT-3.5 | Claude 3 Haiku | 0.961 | 0.681 | 0.280 | 40.34 | Yes | ||
| GPT-OSS | Claude Haiku 4.5 | 0.795 | 0.714 | 0.081 | 10.16 | Yes | ||
| GPT-OSS | Claude 3 Haiku | 0.795 | 0.681 | 0.114 | 11.60 | Yes | ||
| Claude Haiku 4.5 | Claude 3 Haiku | 0.714 | 0.681 | 0.033 | 3.53 | Yes |
| Rank | Feature | Baseline SHAP | Adversarial SHAP | % | |
| 1 | Comment to Code Ratio | 0.34 | 2.01 | +1.67 | +495% |
| 2 | Blank Lines | 0.29 | 0.33 | +0.04 | +14% |
| 3 | Declarative Statements | 0.19 | 0.21 | +0.02 | +12% |
| Rank | Feature | Baseline SHAP | Adversarial SHAP | % | |
| 1 | Comment to Code Ratio | 3.79 | 4.40 | +0.61 | +16% |
| 2 | Maximum Nesting | 0.70 | 0.56 | -0.14 | -21% |
| 3 | Declarative Code Lines | 0.63 | 0.51 | -0.12 | -19% |
| Rank | Feature | Baseline SHAP | Adversarial SHAP | % | |
| 1 | Comment Lines | 0.57 | 0.95 | +0.38 | +68% |
| 2 | Essential Complexity | 0.42 | 0.33 | -0.09 | -22% |
| 3 | Comment to Code Ratio | 0.32 | 0.49 | +0.17 | +52% |
| Rank | Feature | Baseline SHAP | Adversarial SHAP | % | |
| 1 | Average Code Lines | 0.60 | 0.63 | +0.03 | +5% |
| 2 | Coupled Classes (Modified) | 0.34 | 0.32 | -0.02 | -4% |
| 3 | Average Comment Lines | 0.32 | 0.33 | +0.02 | +5% |
| Rank | Feature | Baseline SHAP | Adversarial SHAP | % | |
| 1 | Average Blank Lines | 1.81 | 2.21 | +0.40 | +22% |
| 2 | Average Comment Lines | 0.73 | 0.81 | +0.09 | +12% |
| 3 | Comment to Code Ratio | 0.28 | 0.29 | +0.01 | +4% |
| Rank | Feature | Baseline SHAP | Adversarial SHAP | % | |
| 1 | Maximum Nesting | 1.03 | 1.03 | -0.01 | -1% |
| 2 | Average Comment Lines | 0.74 | 0.78 | +0.04 | +5% |
| 3 | Comment to Code Ratio | 0.54 | 0.25 | -0.29 | -54% |