On LLMs' Internal Representation of Code Correctness

Fuente: arXiv
Gespeichert in:
Bibliographische Detailangaben
Hauptverfasser: Ribeiro, Francisco, Spiess, Claudio, Devanbu, Prem, Nadi, Sarah
Format: Preprint
Veröffentlicht: 2025
Schlagworte:
Online-Zugang:
Tags: Tag hinzufügen
Keine Tags, Fügen Sie den ersten Tag hinzu!
_version_ 1866908778230710272
author Ribeiro, Francisco
Spiess, Claudio
Devanbu, Prem
Nadi, Sarah
author_facet Ribeiro, Francisco
Spiess, Claudio
Devanbu, Prem
Nadi, Sarah
contents 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.
format Preprint
id arxiv_https___arxiv_org_abs_2512_07404
institution arXiv
publishDate 2025
record_format arxiv
spellingShingle On LLMs' Internal Representation of Code Correctness
Ribeiro, Francisco
Spiess, Claudio
Devanbu, Prem
Nadi, Sarah
Software Engineering
Artificial Intelligence
Machine Learning
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.
title On LLMs' Internal Representation of Code Correctness
topic Software Engineering
Artificial Intelligence
Machine Learning
url https://arxiv.org/abs/2512.07404