How Few-shot Demonstrations Affect Prompt-based Defenses Against LLM Jailbreak Attacks
Yanshu Wang, Shuaishuai Yang, Jingjing He, Tong Yang
TL;DR
The paper investigates how few-shot demonstrations interact with two prominent prompt-based defenses (RoP and ToP) against jailbreak attacks in LLMs. It introduces a Bayesian in-context learning framework and attention-analysis to explain why few-shot strengthens RoP via role reinforcement while diluting ToP through attention shifts and position bias. Empirically, across multiple models, four safety benchmarks, and six jailbreak methods, RoP+FS yields safety gains up to about $+4.5\%$, whereas ToP+FS can incur losses up to about $-21.2\%$, with think-mode models showing heightened vulnerability. The work offers practical deployment guidance (prefer RoP with few-shot; avoid few-shot with ToP) and lays a foundation for further exploration of prompt-based defense mechanisms and their interaction with in-context learning.
Abstract
Large Language Models (LLMs) face increasing threats from jailbreak attacks that bypass safety alignment. While prompt-based defenses such as Role-Oriented Prompts (RoP) and Task-Oriented Prompts (ToP) have shown effectiveness, the role of few-shot demonstrations in these defense strategies remains unclear. Prior work suggests that few-shot examples may compromise safety, but lacks investigation into how few-shot interacts with different system prompt strategies. In this paper, we conduct a comprehensive evaluation on multiple mainstream LLMs across four safety benchmarks (AdvBench, HarmBench, SG-Bench, XSTest) using six jailbreak attack methods. Our key finding reveals that few-shot demonstrations produce opposite effects on RoP and ToP: few-shot enhances RoP's safety rate by up to 4.5% through reinforcing role identity, while it degrades ToP's effectiveness by up to 21.2% through distracting attention from task instructions. Based on these findings, we provide practical recommendations for deploying prompt-based defenses in real-world LLM applications.
