From Observation to Explanation: Mechanistic Interpretability of LLM-Generated Code for Principled Repair

Musfiqur RahmanarXiv 2026

Imagine you’re a senior engineer reviewing a pull request from an AI agent. The code looks clean, the syntax is perfect, and it passes the initial linter. You merge it. But a week later, that same code is causing strange production bugs—specifically, it’s trying to call methods that don't exist.

This is the "uncanny valley" of AI-generated code: it looks right, but it feels fundamentally fragile. In his research program, Musfiqur Rahman moves beyond just observing these quirks to asking the ultimate question: Why does the AI make these specific mistakes, and can we fix the "brain" of the model rather than just patching its output?

TL;DR

LLMs are transforming coding, but they produce "syntactically plausible" code that harbors deep structural defects, often failing 84% of the time on real-world tasks despite acing synthetic benchmarks. This research uses mechanistic interpretability—essentially "brain-mapping" the transformer—to identify the specific internal circuits responsible for these errors, paving the way for AI that repairs its own causal flaws rather than just masking symptoms.

What Problem Does This Paper Address?

We are currently in an era of "observation without explanation." We know LLMs are good at coding, but we don't know why they fail in such specific, repetitive ways.

Rahman identifies three major gaps:

  1. The Stability Gap: While industry rumors suggest AI code is "disposable" and quickly replaced, data shows it actually sticks around—but autonomous agent code requires significantly more corrective fixes than human code.
  2. The Evaluation Gap: LLMs that look like geniuses on synthetic benchmarks (scoring 85%+) turn out to be mediocre (scoring ~30%) when faced with complex, real-world class-level code.
  3. The Detection Gap: We can detect AI code, but our methods are inconsistent. Why is GPT-3.5 incredibly easy to spot (AUC-ROC 0.96) while newer models are stealthier, even when they use similar structures?

The core problem is that we treat LLMs as "black boxes." If we don't understand the causal reason for a hallucination, we can't build truly reliable software agents.

Methods

The research triangulates evidence across three layers:

  • Longitudinal Analysis: Using survival analysis (Kaplan-Meier and Cox Proportional Hazards) to track how long AI-generated code survives in the wild across 156 open-source repositories.
  • Empirical Benchmarking: Testing models on "unseen" real-world code to rule out memorization.
  • Mechanistic Interpretability: This is the "neurosurgery" part. Rahman uses techniques like:
  • Logit Lens: Watching how a specific word (token) "crystallizes" as it moves through the model's layers.
  • Activation Patching: Swapping internal signals between a "clean" prompt and a "corrupted" one to see which specific circuit causes a mistake.
  • Linear Probing: Training tiny classifiers to see if a model's internal state already "knows" it's about to generate a bug.

Datasets

The study introduces and utilizes several high-scale datasets:

  • AIDev: 223,997 line-level units from 156 repositories to study code stability.
  • RealClassEval: A new benchmark of 400 real-world Python classes, specifically designed to test models on code they haven't seen in their training data.
  • OpenClassGen: A massive corpus of 324,843 real-world Python classes with 27 static metrics.
  • SIEVE: A custom infrastructure for sourcing "contamination-free" data from GitHub, ensuring the models aren't just reciting code they've already memorized.

Key Insights and Ideas

  • The "AttributeError" Paradox: LLMs almost never make syntax errors (they know the "grammar" of Python perfectly), yet they routinely hallucinate object attributes. This suggests that "syntactic knowledge" and "object knowledge" live in entirely different circuits in the model's brain.
  • The GPT-3.5 Anomaly: GPT-3.5 is 27 percentage points more detectable than modern models. The research suggests this is due to extreme brevity and documentation sparsity—a "stylistic signature" that modern models have evolved away from.
  • Granularity Dominance: Detecting AI at the "function" level is a completely different task than at the "class" level. The features that give away a single function don't work for a whole class, indicating that AI "style" changes depending on the scope of the task.
Table 3: Detection AUC-ROC: CatBoost vs. GPTZero. GPTZero recall shown in parentheses; CatBoost recall ranges from 61–87% [13].

Key Results

  • Autonomous Agent Fragility: Code from agents like Devin attracts corrective modifications 49.1% of the time, compared to 37.9% for humans. It looks stable syntactically, but it's structurally "rotten."
Table 1: Code stability by tier, showing that autonomous agents produce code that is statistically as unstable as human code at the structural level.
  • The Performance Collapse: Models that hit 84–89% correctness on synthetic tasks drop to a mere 24–35% on real-world code.
  • Semantic vs. Functional: As shown in Table 2, models achieve high semantic similarity (they "get" the gist) but fail miserably on functional correctness (the code doesn't actually run).
Table 2: OpenClassGen evaluation: three LLMs on 300 strat- ified classes. CB-F3 = CodeBERTScore-F3; TSED = tree-edit similarity [16].

Limitations and Future Work

The author acknowledges that findings on small, open-source models (like Qwen or DeepSeek) might not perfectly translate to giant, proprietary "black boxes" like GPT-4. To mitigate this, the research triangulates across different model families.

The final "boss" of this thesis is Interpretability-Informed Agentic Repair. Instead of a model just guessing how to fix a bug, it will use these "circuit maps" to intervene at the causal source. If the model knows why it hallucinated a variable name, it can steer its own internal activations to produce the correct one.

Summary updated

Abstract

Large language models (LLMs) have transformed software development, yet our understanding of how they generate code remains critically underdeveloped. We address this gap by triangulating evidence across three layers. Longitudinally, autonomous agent-attributed code is statistically indistinguishable from human code under a semantic modification definition (HR = 1.019, p = 0.490), yet attracts disproportionately corrective modifications in production (49.1% vs 37.9% human baseline). This finding demands a causal explanation. Empirically, existing benchmarks overestimate LLM capabilities: models that achieve 84 to 89% correctness on synthetic tasks attain only 24 to 35% on real-world class-level code. Behaviourally, detection systems reveal puzzling asymmetries: GPT-3.5 is far more detectable than modern models (AUC-ROC = 0.96, a 27-point gap), even though modern models produce mutually similar code structures; yet no single feature reliably distinguishes AI-generated from human code at the class level, and the leading commercial detector achieves only 15 to 64% recall. Together, these findings raise a question that empirical studies cannot answer: why do LLMs generate code with these properties? This thesis proposes mechanistic interpretability, a causal token-level circuit tracing, as the bridge between empirical observation and principled correction. By identifying the transformer circuits responsible for detectable stylistic signatures, hallucination patterns, and systematic defects, we aim to drive an agentic repair framework that intervenes at the causal source of defects rather than patching surface symptoms.
BibTeX
@misc{rahman2026from,
  author = {Musfiqur Rahman},
  title = {{From Observation to Explanation: Mechanistic Interpretability of LLM-Generated Code for Principled Repair}},
  year = {2026},
  eprint = {2611.28153},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.28153},
}
Figure