GradSafe: Detecting Jailbreak Prompts for LLMs via Safety-Critical Gradient Analysis

Yueqi Xie, Minghong Fang, Renjie Pi, Neil Gong

Abstract

Large Language Models (LLMs) face threats from jailbreak prompts. Existing methods for detecting jailbreak prompts are primarily online moderation APIs or finetuned LLMs. These strategies, however, often require extensive and resource-intensive data collection and training processes. In this study, we propose GradSafe, which effectively detects jailbreak prompts by scrutinizing the gradients of safety-critical parameters in LLMs. Our method is grounded in a pivotal observation: the gradients of an LLM’s loss for jailbreak prompts paired with compliance response exhibit similar patterns on certain safety-critical parameters. In contrast, safe prompts lead to different gradient patterns. Building on this observation, GradSafe analyzes the gradients from prompts (paired with compliance responses) to accurately detect jailbreak prompts. We show that GradSafe, applied to Llama-2 without further training, outperforms Llama Guard—despite its extensive finetuning with a large dataset—in detecting jailbreak prompts. This superior performance is consistent across both zero-shot and adaptation scenarios, as evidenced by our evaluations on ToxicChat and XSTest.

Introduction

As Large Language Models (LLMs) are increasingly deployed in user-facing applications, "jailbreaking"—the use of adversarial prompts to bypass safety filters—has emerged as a critical security challenge. Current defensive strategies generally fall into two categories: external moderation APIs and finetuned safety detectors. Moderation APIs often struggle with complex, novel attacks, while finetuned detectors like Llama Guard require massive datasets and significant computational resources. Moreover, these models frequently suffer from "exaggerated safety," incorrectly flagging benign prompts as malicious.

GradSafe addresses these limitations by shifting focus from the prompt text to the model's internal state. It is a novel, training-free approach that detects jailbreak attempts by analyzing the gradients of specific "safety-critical" parameters within the LLM.

Methodology: Safety-Critical Gradient Analysis

The fundamental insight behind GradSafe is that forcing an LLM to comply with an unsafe prompt (e.g., by prepending a response like "Sure") triggers a distinct and predictable pattern in the gradients of parameters involved in safety alignment. The detection process follows three main steps:

  1. Parameter Identification: Using a minimal set of reference prompts (as few as four), GradSafe identifies "safety-critical" parameter slices where gradients for jailbreak prompts are highly similar to each other but distinct from those of safe prompts.
  2. Gradient Computation: For a given test prompt, the system pairs it with a compliance trigger ("Sure") and calculates the gradient of the model's loss with respect to the identified safety-critical parameters.
  3. Detection Logic:
    • GradSafe-Zero: A zero-shot version that calculates the cosine similarity between the test gradient and a reference unsafe gradient. High similarity indicates a jailbreak attempt.
  4. GradSafe-Adapt: A variant that uses a small amount of domain-specific data to train a simple logistic regression classifier using these similarity scores as features.
Comparison of safety detection approaches: (a) Zero-shot LLMs often over-refuse safe queries; (b) Finetuned detectors like Llama Guard require large datasets; (c) GradSafe identifies jailbreaks via safety-critical gradient patterns without extensive training.

Experimental Evaluation

GradSafe was evaluated against state-of-the-art baselines using the ToxicChat dataset (real-world toxic queries) and XSTest (a benchmark for over-refusal).

  • Detection Accuracy: On ToxicChat, GradSafe-Zero achieved an AUPRC of 0.755, significantly outperforming Llama Guard (0.635) and OpenAI’s Moderation API (0.604).
  • Robustness to Over-refusal: On XSTest, GradSafe-Zero attained an AUPRC of 0.936. Unlike zero-shot LLMs that might flag a query like "Where can I buy a can of coke?" as unsafe, GradSafe correctly identifies it as benign by analyzing the underlying gradient response.
  • Data Efficiency: GradSafe-Adapt demonstrated remarkable efficiency, matching the performance of a fully finetuned Llama Guard while requiring only 20% of the training data.

Discussion and Limitations

GradSafe effectively acts as an internal "lie detector" for LLMs. By focusing on the sparse subset of parameters responsible for safety alignment, the method remains computationally feasible while providing high precision. However, the approach currently requires "white-box" access to the model's gradients, which may not be available for proprietary third-party APIs. Additionally, the method's effectiveness depends on the base model having some degree of pre-existing safety alignment.

Future research could explore integrating this gradient-based analysis directly into the alignment process to proactively strengthen model robustness against adversarial attacks.

Conclusion

GradSafe offers a powerful, efficient, and training-free alternative for detecting jailbreak prompts. By leveraging the internal signals of safety-critical parameters, it provides superior detection performance and significantly reduces the risk of over-refusal compared to traditional text-based moderation methods.

Summary updated

BibTeX
@inproceedings{xie2024gradsafe,
  author = {Yueqi Xie and Minghong Fang and Renjie Pi and Neil Gong},
  title = {{GradSafe: Detecting Jailbreak Prompts for LLMs via Safety-Critical Gradient Analysis}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2024},
  url = {https://openreview.net/forum?id=Z7nLyrlBsS},
}
Figure