On LLMs' Internal Representation of Code Correctness
Not on the map. Why?
Abstract (arXiv)
Despite the effectiveness of large language models (LLMs) for code generation, they often output incorrect code. One reason is that model output probabilities are often not well-correlated with correctness, and reflect only the final output of the generation process. Inspired by findings that LLMs internally encode concepts like truthfulness, this paper explores if LLMs similarly represent code correctness. Specifically, we identify a correctness representation inside LLMs by contrasting the hidden states between pairs of correct and incorrect code for the same programming tasks. By experimenting on four LLMs, we show that exploiting this extracted correctness representation outperforms standard log-likelihood ranking, as well as verbalized model confidence. Furthermore, we explore how this internal correctness signal can be used to select higher-quality code samples, without requiring test execution. Ultimately, this work demonstrates how leveraging internal representations can enhance code generation systems and make LLMs more reliable, thus improving confidence in automatically generated code.
Key facts. Cited by 5 papers in the map (data through 7 Oct 2026). Builds on 63 of its 93 references that are in the map. First posted on arXiv 8 Dec 2025.
Builds on
63 of 93 references.
References · 63
- 2017Attention Is All You Needcited by 59,932
- 2021Evaluating Large Language Models Trained on Codecited by 8,053
- 2020Measuring Massive Multitask Language Understandingcited by 7,768
- 2023DINOv2: Learning Robust Visual Features without Supervisioncited by 7,764
- 2017On Calibration of Modern Neural Networkscited by 4,860
- 2018Think you have Solved Question Answering? Try ARC, the AI2 Reasoning Challengecited by 4,710
- 2016SQuAD: 100,000+ Questions for Machine Comprehension of Textcited by 4,115
- 2021Program Synthesis with Large Language Modelscited by 3,509
- 2023SWE-bench: Can Language Models Resolve Real-World GitHub Issues?cited by 3,193
- 2021TruthfulQA: Measuring How Models Mimic Human Falsehoodscited by 2,919
- 2018Can a Suit of Armor Conduct Electricity? A New Dataset for Open Book Question Answeringcited by 2,114
- 2018CommonsenseQA: A Question Answering Challenge Targeting Commonsense Knowledgecited by 1,793
38 more
- 2022Language Models (Mostly) Know What They Knowcited by 1,783
- 2022Competition-Level Code Generation with AlphaCodecited by 1,427
- 2023Is Your Code Generated by ChatGPT Really Correct? Rigorous Evaluation of Large Language Models for Code Generationcited by 1,332
- 2016Man is to Computer Programmer as Woman is to Homemaker? Debiasing Word Embeddingscited by 1,304
- 2023Representation Engineering: A Top-Down Approach to AI Transparencycited by 1,239
- 2021Measuring Coding Challenge Competence With APPScited by 879
- 2023Just Ask for Calibration: Strategies for Eliciting Calibrated Confidence Scores from Language Models Fine-Tuned with Human Feedbackcited by 788
- 2022Teaching Models to Express Their Uncertainty in Wordscited by 731
- 2023Towards Automated Circuit Discovery for Mechanistic Interpretabilitycited by 700
- 2018CoQA: A Conversational Question Answering Challengecited by 674
- 2021Asleep at the Keyboard? Assessing the Security of GitHub Copilot's Code Contributionscited by 417
- 2023Large Language Models for Software Engineering: A Systematic Literature Reviewcited by 389
- 2023CodeT5+: Open Code Large Language Models for Code Understanding and Generationcited by 327
- 2020IntelliCode Compose: Code Generation Using Transformercited by 225
- 2022Grounded Copilot: How Programmers Interact with Code-Generating Modelscited by 216
- 2022Leveraging Large Language Models for Multiple Choice Question Answeringcited by 199
- 2023Planning with Large Language Models for Code Generationcited by 179
- 2025Reasoning Models Know When They're Right: Probing Hidden States for Self-Verificationcited by 154
- 2017Learning to Generate Reviews and Discovering Sentimentcited by 147
- 2022Natural Language Descriptions of Deep Visual Featurescited by 136
- 2024Rethinking Interpretability in the Era of Large Language Modelscited by 110
- 2023Navigating the Grey Area: How Expressions of Uncertainty and Overconfidence Affect Language Modelscited by 91
- 2022Coder Reviewer Reranking for Code Generationcited by 83
- 2024LUQ: Long-text Uncertainty Quantification for LLMscited by 79
- 2024Can multiple-choice questions really be useful in detecting the abilities of LLMs?cited by 66
- 2025Which of These Best Describes Multiple Choice Evaluation with LLMs? A) Forced B) Flawed C) Fixable D) All of the Abovecited by 51
- 2024Calibration and Correctness of Language Models for Codecited by 49
- 2023Hot or Cold? Adaptive Temperature Sampling for Code Generation with Large Language Modelscited by 37
- 2023Security Weaknesses of Copilot-Generated Code in GitHub Projects: An Empirical Studycited by 36
- 2022Fault-Aware Neural Code Rankerscited by 33
- 2023Beyond Surface Statistics: Scene Representations in a Latent Diffusion Modelcited by 29
- 2024InternalInspector I2: Robust Confidence Estimation in LLMs through Internal Statescited by 29
- 2024Towards Understanding the Characteristics of Code Generation Errors Made by Large Language Modelscited by 28
- 2025Right Answer, Wrong Score: Uncovering the Inconsistencies of LLM Evaluation in Multiple-Choice Question Answeringcited by 27
- 2023Assessing the Security of GitHub Copilot Generated Code -- A Targeted Replication Studycited by 24
- 2019BERT has a Moral Compass: Improvements of ethical and moral values of machinescited by 21
- 2021A Few More Examples May Be Worth Billions of Parameterscited by 19
- 2023Trustworthy and Synergistic Artificial Intelligence for Software Engineering: Vision and Roadmapscited by 19
50 most cited of 63 All 63 (JSON)
Built on by
Citing papers per year: 2026: 5.
5 papers.
- 2026Code Correctness Is Linearly Decodable from LLM Hidden States Before Generationcited by 2
- 2026On the Robustness of LLMs' Internal Representation of Code Correctnesscited by 1
- 2026Themis: Training Robust Multilingual Code Reward Models for Flexible Multi-Criteria Scoringcited by 0
- 2026From Brewing to Resolution: Tracing the Internal Lifecycle of Code Reasoning in LLMscited by 0
- 2026Latent Programming Horizons in Coding Agentscited by 0
- 2026On the Robustness of LLMs' Internal Representation of Code Correctness8 Aug 2026
- 2026Latent Programming Horizons in Coding Agents6 Jul 2026
- 2026From Brewing to Resolution: Tracing the Internal Lifecycle of Code Reasoning in LLMs16 Jun 2026
- 2026Code Correctness Is Linearly Decodable from LLM Hidden States Before Generation12 Jun 2026
- 2026Themis: Training Robust Multilingual Code Reward Models for Flexible Multi-Criteria Scoring1 May 2026