Abstract: In the realm of software development, providing accurate and personalized code explanations is crucial for both technical professionals and business stakeholders. Technical professionals benefit from enhanced understanding and improved problem-solving skills, while business stakeholders gain insights into project alignments and transparency. Despite the potential, generating such explanations is often time-consuming and challenging. This paper presents an innovative approach that leverages the advanced capabilities of large language models (LLMs) to generate faithful and personalized code explanations. Our methodology integrates prompt enhancement, self-correction mechanisms, personalized content customization, and interaction with external tools, facilitated by collaboration among multiple LLM agents. We evaluate our approach using both automatic and human assessments, demonstrating that our method not only produces accurate explanations but also tailors them to individual user preferences. Our findings suggest that this approach significantly improves the quality and relevance of code explanations, offering a valuable tool for developers and stakeholders alike.
Introduction
In software development, a "good" code explanation is a moving target. For a senior architect, an explanation should focus on system design and edge cases; for a novice, it needs to be a step-by-step walkthrough of syntax and logic.
Current Large Language Models (LLMs) struggle with two main issues in this domain:
- Faithfulness: LLMs often produce "hallucinations"—explanations that sound confident and syntactically correct but are semantically wrong about what the code actually does.
- Genericism: Most models provide "one-size-fits-all" responses that ignore the user's background, leading to information overload for beginners or triviality for experts.
The authors identify a fundamental trade-off: trying to optimize for both accuracy and personalization in a single prompt often leads to compromises in both. This paper proposes a multi-agent framework to decouple these goals.
Methodology: Iterative Explanation Refinement
The researchers proposed an Iterative Explanation Refinement approach. Instead of a single prompt, the system uses a multi-agent "critique-and-refine" workflow consisting of three stages: Reflection, Iterative Explanation, and Verification/Analysis.
The core innovation lies in two independent loops:
- The Faithfulness Loop: To ensure technical accuracy, the system tasks an agent to write a new piece of verification code based solely on the generated explanation. This code is run through a Python executor against test cases. If the code fails, a "Failure Analysis" agent identifies the discrepancy, and the explanation is refined.
- The Personalization Loop: The system builds a "programming profile" for the user based on their historical Stack Overflow inquiries. A "role-playing" LLM agent then acts as a judge, evaluating whether the explanation aligns with that specific profile and suggesting revisions if the tone or complexity is off-target.
Key Results
The "Self-Iteration" method was evaluated against standard baselines and commercial products using the CodeContests dataset and Stack Overflow user personas.
Technical Accuracy
On the CodeContests test set, the method achieved a Pass@1 of 26.52% and Pass@5 of 30.15%, consistently outperforming the baseline and even some commercial products. This demonstrates that the iterative verification loop effectively reduces hallucinations.
User Preference and Alignment
In "Win Rate" evaluations, the iterative method was preferred over 80% of the time compared to standard greedy decoding baselines. Metrics like the Word Overlap Ratio showed significantly higher scores (e.g., 7.35% overlap vs. 4.86% for the baseline), indicating that the generated text much more closely matched the specific technical vocabulary and interests of the target users.
Discussion and Conclusion
The study highlights that execution is the ultimate ground truth. If an LLM cannot write working code based on its own explanation, the explanation is objectively flawed. By separating the faithfulness and personalization loops, the framework ensures that the model doesn't sacrifice technical correctness just to sound "beginner-friendly."
While the framework excels at explaining code, there remains a "reasoning gap" for high-difficulty tasks. Future work will explore using this iterative method to generate high-quality data for fine-tuning smaller, more efficient reasoning models, further bridging the gap between technical precision and user-centric clarity.
Summary updated