Online Learning Defense against Iterative Jailbreak Attacks via Prompt Optimization
Masahiro Kaneko, Zeerak Talat, Timothy Baldwin
TL;DR
This work addresses the vulnerability of LLMs to iterative jailbreaking by introducing an online-learning defense that continuously rewrites prompts to reject harmful prompts while preserving useful responses for harmless prompts. The defense combines supervised pretraining and reinforcement learning to optimize a prompt-optimization model, with online updates and a novel Past-Direction Gradient Damping (PDGD) mechanism to mitigate overfitting in non-IID input streams. Empirical results across GPT-4, OLMo 2, and Llama 3 against five iterative jailbreak methods show significant reductions in attack success and improved harmless-task perplexity, including multilingual settings and robustness to non-iterative attacks. The approach demonstrates that prompt optimization, when performed online and with PDGD regularization, can achieve defense effectiveness without retraining the target LLM, offering a practically deployable external safeguard for real-world systems.
Abstract
Iterative jailbreak methods that repeatedly rewrite and input prompts into large language models (LLMs) to induce harmful outputs -- using the model's previous responses to guide each new iteration -- have been found to be a highly effective attack strategy. Despite being an effective attack strategy against LLMs and their safety mechanisms, existing defenses do not proactively disrupt this dynamic trial-and-error cycle. In this study, we propose a novel framework that dynamically updates its defense strategy through online learning in response to each new prompt from iterative jailbreak methods. Leveraging the distinctions between harmful jailbreak-generated prompts and typical harmless prompts, we introduce a reinforcement learning-based approach that optimizes prompts to ensure appropriate responses for harmless tasks while explicitly rejecting harmful prompts. Additionally, to curb overfitting to the narrow band of partial input rewrites explored during an attack, we introduce Past-Direction Gradient Damping (PDGD). Experiments conducted on three LLMs show that our approach significantly outperforms five existing defense methods against five iterative jailbreak methods. Moreover, our results indicate that our prompt optimization strategy simultaneously enhances response quality for harmless tasks.
