Interpreting and Steering for Safe and Correct Code Generation
Not on the map. Why?
Abstract (arXiv)
Large language models (LLMs) frequently generate source code containing vulnerabilities, yet little work studies the internal mechanisms that distinguish safe from vulnerable generation in them. In this work, we systematically perform a mechanistic interpretation of LLMs, aiming at both understanding how code safety-vs-vulnerability is represented or driven by components in an LM and turning the insights into actionable steering strategies to encourage safer code generation. To this end, we introduce CodeSec-Pairs, a dataset of 9,342 Python safe-and-vulnerable contrastive code pairs, sampled from Llama-3.1-8B-Instruct. Utilizing the dataset, we explore approaches to localize layers and attention heads that relate to code safety, and further experiment with different steering strategies for inference-time vulnerability reduction. In particular, we propose DuoSteer, a double-steering approach that simultaneously applies safety and code-correctness steering to attention heads. In experiments over five vulnerability types, DuoSteer leads to an average of -26.9% vulnerability rate reduction and +7.5% functional correctness improvement, which outperforms not only other steering variants but also prompting and supervised fine-tuning baselines. The advantage also replicates on Qwen-2.5-Coder-7B-Instruct with another 2,500 contrastive pairs sampled from that model.
Key facts. No citing papers in the map yet (data through 14 Oct 2026). Builds on 23 of its 39 references that are in the map. First posted on arXiv 30 Aug 2026.
Builds on
23 of 39 references.
References · 23
- 2022Locating and Editing Factual Associations in GPTcited by 2,691
- 2023Inference-Time Intervention: Eliciting Truthful Answers from a Language Modelcited by 1,302
- 2022Interpretability in the Wild: a Circuit for Indirect Object Identification in GPT-2 smallcited by 1,165
- 2023Steering Llama 2 via Contrastive Activation Additioncited by 1,085
- 2019BERT Rediscovers the Classical NLP Pipelinecited by 1,059
- 2024Refusal in Language Models Is Mediated by a Single Directioncited by 1,032
- 2023Steering Language Models With Activation Engineeringcited by 922
- 2021Probing Classifiers: Promises, Shortcomings, and Advancescited by 876
- 2023Towards Automated Circuit Discovery for Mechanistic Interpretabilitycited by 724
- 2024Sparse Feature Circuits: Discovering and Editing Interpretable Causal Graphs in Language Modelscited by 435
- 2021Asleep at the Keyboard? Assessing the Security of GitHub Copilot's Code Contributionscited by 431
- 2024Mechanistic Interpretability for AI Safety -- A Reviewcited by 422
11 more
- 2023Towards Best Practices of Activation Patching in Language Models: Metrics and Methodscited by 359
- 2023Large Language Models for Code: Security Hardening and Adversarial Testingcited by 155
- 2024Instruction Tuning for Secure Code Generationcited by 68
- 2023LLMSecEval: A Dataset of Natural Language Prompts for Security Evaluationscited by 62
- 2023CodeLMSec Benchmark: Systematically Evaluating and Finding Security Vulnerabilities in Black-Box Code Language Modelscited by 55
- 2024CodeJudge: Evaluating Code Generation with Large Language Modelscited by 44
- 2025Give LLMs a Security Course: Securing Retrieval-Augmented Code Generation via Knowledge Injectioncited by 16
- 2025Guiding AI to Fix Its Own Flaws: An Empirical Study on LLM-Driven Secure Code Generationcited by 13
- 2026Activation Steering via Generative Causal Mediationcited by 6
- 2025Failure by Interference: Language Models Make Balanced Parentheses Errors When Faulty Mechanisms Overshadow Sound Onescited by 4
- 2026CodeCircuit: Toward Inferring LLM-Generated Code Correctness via Attribution Graphscited by 3
Built on by
Not cited yet.