Think Before You Code: Dual Reasoning for the NLSafety-Utility Trade-Off in LLM Code Generation
On the map: LLM reasoning → Reasoning for agents, planning and code
also in 1 more
Abstract (arXiv)
Large language models (LLMs) for code generation are typically evaluated on functional correctness alone, overlooking whether generated code propagates harmful content embedded in the prompt. Prior work has shown that most Code LLMs reproduce offensive identifiers from injected renaming instructions without warning, yet existing approaches focus on detecting harmful content, neglecting functional correctness. Grounded in the Theory of Dual Channel Constraints (which states that code is a dual-channel medium combining an algorithmic (AL) channel for machine execution and a natural language (NL) channel for human communication, creating a unique safety-utility trade-off where a model must balance functional execution with responsible communication), we propose NLSafety-Utility Duality Score (SUDS), a metric that unifies code utility, safety adherence, and warning awareness into a single score across 12 ranked response scenarios, and Dual Reasoning (DR), a structured inference-time technique that requires an explicit safety audit and task-grounded code review before code generation. Evaluated on six LLMs across two benchmarks augmented with harmful keyword injections (820 and 2,135 samples), DR consistently achieves the highest SUDS across all models, improving mean SUDS by 1.32× to 3.42× over the baseline, while chain-of-thought prompting yields negligible safety gains and a safety-aware prompt provides only partial improvement. Further analysis reveals that DR's effectiveness scales with model capacity, that the one-shot exemplar primarily stabilizes output format for smaller models, and that structured reasoning cannot compensate for models with limited safety vocabularies.
Key facts. No citing papers in the map yet (data through 7 Oct 2026). Placed in Reasoning for agents, planning and code, one of 17 directions in LLM reasoning. Builds on 18 of its 46 references that are in the map. First posted on arXiv 13 Apr 2026.
Builds on
18 of 46 references.
Closest ancestors · 2
Same direction, newest first
- 2025STAIR: Improving Safety Alignment with Introspective Reasoning4 Feb
- 2024Deliberative Alignment: Reasoning Enables Safer Language Models20 Dec
Other references · 16
- 2021Evaluating Large Language Models Trained on Codecited by 8,053
- 2022Self-Consistency Improves Chain of Thought Reasoning in Language Modelscited by 5,601
- 2022Training a Helpful and Harmless Assistant with Reinforcement Learning from Human Feedbackcited by 3,634
- 2021Program Synthesis with Large Language Modelscited by 3,509
- 2023Self-Refine: Iterative Refinement with Self-Feedbackcited by 3,317
- 2023Tree of Thoughts: Deliberate Problem Solving with Large Language Modelscited by 3,155
- 2023Code Llama: Open Foundation Models for Codecited by 2,105
- 2022Least-to-Most Prompting Enables Complex Reasoning in Large Language Modelscited by 1,397
- 2023Is Your Code Generated by ChatGPT Really Correct? Rigorous Evaluation of Large Language Models for Code Generationcited by 1,332
- 2024DeepSeek-Coder: When the Large Language Model Meets Programming -- The Rise of Code Intelligencecited by 1,154
- 2023Fine-tuning Aligned Language Models Compromises Safety, Even When Users Do Not Intend To!cited by 1,106
- 2023Safe RLHF: Safe Reinforcement Learning from Human Feedbackcited by 630
4 more
- 2022MultiPL-E: A Scalable and Extensible Approach to Benchmarking Neural Code Generationcited by 132
- 2024OASIS: Open Agent Social Interaction Simulations with One Million Agentscited by 127
- 2023MTTM: Metamorphic Testing for Textual Content Moderation Softwarecited by 20
- 2024Synergy-of-Thoughts: Eliciting Efficient Reasoning in Hybrid Language Modelscited by 13
Built on by
Not cited yet.
Similar, not cited
Close in wording, no citation either way
- 2025DualGauge: Automated Joint Security-Functionality Benchmarking of Specification-Only Code Generation by LLMs and Coding Agents
- 2026Enhancing Reliability in LLM-Based Secure Code Generation
- 2025Probing AI Safety with Source Code
- 2024Assessing, Exploiting, and Mitigating Syntactic Robustness Failures in LLM-Based Code Generation
- 2024Safe to Serve: Aligning Instruction-Tuned Models for Safety and Helpfulness
- 2026Code Is More Than Text: Uncertainty Estimation for Code Generation
- 2026How Robustly do LLMs Understand Execution Semantics?
- 2025CodeCrash: Exposing LLM Fragility to Misleading Natural Language in Code Reasoning