Abstract
Large Language Models (LLMs) are known to be vulnerable to jailbreaking attacks, wherein adversaries exploit carefully engineered prompts to induce harmful or unethical responses. Such threats have raised critical concerns about the safety and reliability of LLMs in real-world deployment. While existing defense mechanisms partially mitigate such risks, subsequent advancements in adversarial techniques have enabled novel jailbreaking methods to circumvent these protections, exposing the limitations of static defense frameworks. In this work, we explore defending against evolving jailbreaking threats through the lens of context retrieval. First, we conduct a preliminary study demonstrating that even a minimal set of safety-aligned examples against a particular jailbreak can significantly enhance robustness against this attack pattern. Building on this insight, we further leverage the retrieval-augmented generation (RAG) techniques and propose Safety Context Retrieval (SCR), a scalable and robust safeguarding paradigm for LLMs against jailbreaking. Our comprehensive experiments demonstrate how SCR achieves superior defensive performance against both established and emerging jailbreaking tactics, contributing a new paradigm to LLM safety.
The Challenge of Evolving Jailbreaks
Large Language Models (LLMs) are typically aligned using techniques like Reinforcement Learning from Human Feedback (RLHF) to refuse harmful requests. However, attackers constantly develop "jailbreaks"—cleverly engineered prompts using ciphers, roleplay, or logic puzzles that bypass these safety guards.
Current defenses are often static, relying on fixed system prompts or pre-training. When a new, "in-the-wild" attack pattern emerges, such as the "Skeleton Key" or "Renellm" attacks, these static defenses often fail. While fine-tuning the model on new attacks is an option, it is computationally expensive, slow to deploy, and can lead to "catastrophic forgetting" of general intelligence. This research introduces a defense that is as adaptive and scalable as the attacks themselves.
Safety Context Retrieval (SCR)
The proposed framework, Safety Context Retrieval (SCR), treats LLM safety as a retrieval problem rather than just a training one. Unlike static modules that struggle with novel attack patterns, SCR dynamically adapts to the input query.
The framework operates in two main stages:
- Initialization: A "Safety Context Base" is created, containing thousands of vanilla harmful prompts paired with safe, helpful refusals (e.g., using the WildJailbreak dataset).
- The Identify-and-Defend Loop:
- When a new jailbreak pattern is discovered, developers add a small batch (10–100) of examples of that specific attack and its safe refusal into the retrieval database. No retraining is required.
- At Inference Time: For every user query, SCR uses an embedding model to find the $K$ most similar examples from the safety base. These examples are prepended to the user's prompt as "in-context" demonstrations.
- Generation: The LLM leverages its in-context learning capabilities, seeing examples of how to refuse similar-looking attacks, which significantly increases the likelihood of refusing the current jailbreak query.
Experimental Evaluation
The study utilized several key datasets for evaluation, including WildJailbreak for the safety base, HarmBench for common attacks, and RapidResponseBench to simulate "in-the-wild" attacks like Skeleton Key and Renellm. To ensure the defense did not degrade general model performance, the authors tested on HumanEval (coding), GSM8K (math), and MMLU (general knowledge).
The Power of Few-Shot Safety
A core insight of this work is the effectiveness of few-shot safety demonstrations. A preliminary study revealed that providing just 4 to 16 relevant safety examples can drop the success rate of a novel attack from over 50% to under 10%.
As shown in the results above, SCR demonstrated superior performance across multiple LLMs:
- Against Common Attacks: For the GCG-T attack, SCR reduced the average Attack Success Rate (ASR) from 34.9% to 2.5%.
- Against "In-the-Wild" Attacks: On the difficult "Skeleton Key" attack, SCR achieved an ASR of 20.4%, significantly outperforming standard In-Context Defense (40.8%).
- Zero Impact on Intelligence: Crucially, the models maintained their scores on general benchmarks like HumanEval and MMLU. The defense only triggers a refusal when the retrieved context suggests the input is harmful, leaving benign queries unaffected.
Conclusion and Future Directions
SCR offers a scalable and robust safeguarding paradigm by prioritizing context over weights. Instead of attempting to bake every possible defense into the model's parameters, SCR provides the model with a relevant "safety manual" for each conversation.
While the approach adds some latency due to retrieval and increased input length, it provides a manageable trade-off for high-security applications. Future research will focus on optimizing retrieval speed and exploring automated generation of diverse safety contexts for obscure attack vectors.
Summary updated