INDICT: Code Generation with Internal Dialogues of Critiques for Both Security and Helpfulness

Hung Le, Yingbo Zhou, Caiming Xiong, Silvio Savarese, Doyen Sahoo

TL;DR

INDICT introduces a dual-critic framework that significantly improves Large Language Model (LLM) code generation by balancing the often-conflicting goals of safety and helpfulness. By orchestrating an autonomous internal dialogue between a safety-driven critic and a helpfulness-driven critic—both empowered with external search and execution tools—the framework achieves over a 10% absolute improvement in code quality across various model sizes without requiring expensive fine-tuning.

What Problem Does This Paper Address?

While LLMs have become remarkably proficient at generating code, they struggle to navigate the intricate boundary between helpfulness and security. Code itself is rarely inherently malicious; for instance, an encryption script can be used to build a secure personal file system (helpful) or to launch a ransomware attack (malicious).

Standard alignment techniques like fine-tuning (RLHF) are expensive, require scarce expert cybersecurity data, and often fail against sophisticated, optimized "jailbreak" attacks. Furthermore, forcing a single model to optimize for both functional correctness and safety at inference time often leads to biased or diluted critiques. INDICT addresses this gap by creating a scalable, inference-time multi-agent framework that simultaneously optimizes both security and helpfulness.

Methods

The core innovation of INDICT (Internal Dialogues of Critiques) is its dual cooperative critic system. Instead of relying on a single critic, the framework splits the responsibility into two distinct agents with orthogonal objectives:

  1. Safety-Driven Critic ($\sigma$): Focuses entirely on identifying security risks, vulnerabilities, and malicious intents.
  2. Helpfulness-Driven Critic ($\omega$): Focuses on whether the code fully satisfies the user's prompt requirements and corner cases.
Figure 2: INDICT (Internal Dialogues of Critiques) is a framework to generate code by both safety and helpfulness. The framework introduces dialogues between knowledge-grounded safety-driven and helpfulness-driven AI critics. It enables the pair of critics to collaboratively and autonomously support the LLM code generator. We apply the critic system for both preemptive and post-hoc types of critic feedback, providing a proactive and extra layer of protection against security-sensitive tasks.

These critics engage in an autonomous internal dialogue. To prevent context bloat for the main "actor" LLM, their conversation is dynamically summarized by an LLM-based summarizer.

Crucially, the critics are knowledge-grounded using external tools through two primary actions:

  • Code Search: Queries the web, Wikipedia, or OpenAI using text and code snippets to find secure alternatives or best practices.
  • Code Review: Executes code snippets within a secure code interpreter to observe runtime behavior and errors.
Figure 3: We define two types of tool-enabled actions the critics can perform: (1) “code search” queries external tools by a generated text query and optionally a corresponding code snippet. (2) “code review” uses the execution result of the code snip- pet (through a code interpreter) as additional input to complement the query. Both action types query tools like web search, Wikipedia, and OpenAI as the knowledge base.

INDICT deploys these critics in two distinct phases: preemptive feedback (during the initial code generation stage to prevent dangerous code from ever being executed) and post-hoc feedback (after the code runs in an execution environment).

Datasets

The authors evaluated INDICT across 8 programming languages using 5 rigorous benchmarks:

  • CyberSecEval-1: Used to test insecure coding practices (Autocomplete and Instruction splits) and cyberattack helpfulness.
  • CyberSecEval-2: Used to evaluate advanced threats like interpreter abuse and prompt injection.
  • CVS (Code Vulnerability and Security): A dataset providing pairs of ground-truth secure and insecure code for specific problems.
  • HarmBench: An open-ended generation benchmark used to test the framework against automated red-teaming optimization methods.
  • CAMEL: A dataset containing complex, open-ended problems across diverse conversational domains.

Key Insights and Ideas

  • Decoupling Objectives Prevents Bias: Trying to prompt a single model to be both perfectly safe and perfectly helpful usually results in a compromise. Separating these roles into two adversarial yet collaborative agents allows each to advocate fully for its objective.
  • Preemptive Protection is Mandatory for Security: Relying solely on post-hoc execution feedback (testing if code works after running it) is highly risky in cybersecurity. If an LLM generates code that deletes a directory, running it to get an error message is too late. Preemptive critique acts as a vital firewall.
  • External Grounding Cures Hallucinations: LLMs frequently hallucinate security vulnerabilities or miss obscure ones. Giving critics access to web searches and code interpreters drastically improves the accuracy and reliability of their feedback.

Key Results

The framework was validated using models ranging from 7B to 70B parameters (including CommandR, Llama, and CodeLlama families).

  • Universal Improvement: INDICT delivered a consistent +10% absolute improvement in overall code quality across all tested models.
  • State-of-the-Art Safety: When paired with CommandR and Llama3, INDICT achieved state-of-the-art safety scores, rendering over 80% of outputs safe on CyberSecEval-1 and over 90% safe on CVS.
  • No Loss in Helpfulness: Unlike traditional safety guards that make models overly conservative, up to 70% of INDICT's outputs were rated as more helpful than baseline or ground-truth models.
Figure 1: INDICT (Internal Dialogues of Critiques) enables two different critics to interact with each other autonomously and collaboratively, improving code generation by both security and helpfulness. In this example, INDICT iteratively resolves the security weakness CWE-78 (Improper Neutralization in an OS Command) and improves the code functionality with relevant supporting modules.

Limitations and Future Work

  • Dependency on Instruction-Following: INDICT relies heavily on the base LLM’s capacity to roleplay effectively. If a model struggles to maintain the persona of a strict safety critic, the quality of the dialogue degrades.
  • Computational Overhead: Running multiple critic loops and external tool queries increases inference latency and API costs, making INDICT roughly 3-4x more time-consuming than direct single-model generation.
  • Narrow Definition of Safety: The paper focuses heavily on technical vulnerabilities (CWEs) and malicious exploits. It does not currently address broader safety concerns such as social biases or offensive language within code comments.

Summary updated

Abstract

Large language models (LLMs) for code are typically trained to align with natural language instructions to closely follow their intentions and requirements. However, in many practical scenarios, it becomes increasingly challenging for these models to navigate the intricate boundary between helpfulness and safety, especially against highly complex yet potentially malicious instructions. In this work, we introduce INDICT: a new framework that empowers LLMs with Internal Dialogues of Critiques for both safety and helpfulness guidance. The internal dialogue is a dual cooperative system between a safety-driven critic and a helpfulness-driven critic. Each critic provides analysis against the given task and corresponding generated response, equipped with external knowledge queried through relevant code snippets and tools like web search and code interpreter. We engage the dual critic system in both code generation stage as well as code execution stage, providing preemptive and post-hoc guidance respectively to LLMs. We evaluated INDICT on 8 diverse tasks across 8 programming languages from 5 benchmarks, using LLMs from 7B to 70B parameters. We observed that our approach can provide an advanced level of critiques of both safety and helpfulness analysis, significantly improving the quality of output codes ($+10\%$ absolute improvements in all models).
BibTeX
@inproceedings{le2024indict,
  author = {Hung Le and Yingbo Zhou and Caiming Xiong and Silvio Savarese and Doyen Sahoo},
  title = {{INDICT: Code Generation with Internal Dialogues of Critiques for Both Security and Helpfulness}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=jCMYIUwprx},
}
Figure