Analyzing Latent Concepts in Code Language Models

Arushi Sharma, Vedant Pungliya, Christopher J. Quinn, Ali JannesariarXiv 2025 · DOI

Introduction

Large Language Models (LLMs) for code, such as CodeBERT and DeepSeekCoder, have revolutionized software engineering tasks. However, these models remain "black boxes," making it difficult to understand why they make specific predictions. Traditional interpretability methods often provide shallow, token-level highlights (local methods) or rely on human-predefined labels that may not align with the model's internal logic (global methods). To bridge this gap, researchers have introduced Code Concept Analysis (CoCoA), a framework designed to uncover and label the latent abstractions these models build autonomously.

The CoCoA Framework

CoCoA employs a three-stage pipeline to transform opaque internal states into human-readable explanations.

Overview of the CoCoA pipeline, showing concept discovery, alignment, and application.
  1. Concept Discovery: The framework extracts contextualized activations from various model layers and applies K-Means clustering. Each cluster represents a "latent concept"—a recurring pattern or abstraction the model has learned.
  2. Concept Alignment and Annotation: These clusters are then aligned with known syntactic structures (using Tree-sitter) and lexical patterns. For more abstract concepts, GPT-4o acts as an automated annotator, generating descriptive labels for each cluster.
  3. Latent Concept Attribution (LCA): Finally, CoCoA maps salient tokens from traditional attribution methods to these discovered concepts. This allows the system to explain a prediction by describing the functional role of a token rather than just highlighting its presence.

Key Insights into Model Behavior

The study reveals several critical insights into how code LLMs organize information:

  • Layer-wise Evolution: Lower layers primarily capture lexical features (e.g., casing, prefixes), while deeper layers synthesize these into complex semantic abstractions.
  • Impact of Fine-tuning: Fine-tuning reshapes the model's internal "map." For instance, tasks like language classification encourage the development of cross-lingual concepts, such as grouping Python's def and JavaScript's function into a single "Function Definition" category.
  • Stability and Disambiguation: Latent concepts are more robust than raw token highlights. They remain stable even when variable names are changed and can effectively disambiguate identical tokens used in different contexts (e.g., distinguishing a < operator from a structural tag).

Results and Human Impact

CoCoA significantly enhances the interpretability of model outputs for developers. In user studies, concept-augmented explanations improved human understanding of model predictions by 37 percentage points compared to standard token-level attributions.

An example of a CoCoA-generated explanation, mapping code to high-level concepts like "Data structures."

Furthermore, the framework demonstrated high robustness, with a Cluster Sensitivity Index (CSI) of 0.288, indicating that the discovered concepts are resistant to minor code perturbations. The quality of automated annotations was also high, with 95.8% of GPT-4o's labels rated as acceptable by human experts.

Conclusion and Future Directions

While CoCoA provides a powerful lens into code LLMs, limitations remain. The reliance on K-Means assumes spherical cluster shapes, and the attribution process still depends on base methods like Integrated Gradients. Future work aims to explore "concept-aware training," where models are explicitly incentivized to organize their internal space into interpretable concepts from the start, rather than relying solely on post-hoc analysis.

Summary updated

BibTeX
@misc{sharma2025analyzing,
  author = {Arushi Sharma and Vedant Pungliya and Christopher J. Quinn and Ali Jannesari},
  title = {{Analyzing Latent Concepts in Code Language Models}},
  year = {2025},
  eprint = {2510.00476},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2510.00476},
}
Figure