Manuscript Offprint

arXiv:2608.27580

LongGuard: Mechanistic Analysis and Training-Free Mitigation of Long-Context Failure in Safety Guardrails

Ziyang Chen, Xing Wu, Songlin Hu

Abstract

Safety guardrails serve as the last line of defense against harmful inputs and outputs of large language models (LLMs), yet they are trained and evaluated almost exclusively on short text. We present LongGuard, a framework that evaluates, mechanistically analyzes, and mitigates long-context guardrail failure. We formulate the task as Safety Needle-in-a-Haystack (SafetyNIAH) over a 0.25k–32k length grid; across 15 mainstream guardrails, unsafe recall drops monotonically by more than 50% on average, and a paired Benign-Fill vs. Needle-Repeat design attributes the failure to proportional dilution of the unsafe needle rather than to absolute length. A three-layer attention-logit-behavior analysis on six guardrails locates the mechanism: attention mass on the unsafe needle is diluted, the unsafe-over-safe logit margin is compressed in lockstep, and the detection decision collapses accordingly, with this attention→logit→behavior chain remaining consistent after partialling out length. We further isolate a sparse set of guard-specialized retrieval heads that exhibit partial specificity relative to their base models. Building on the analysis, we propose two training-free mitigations – Chunked Detection (CD) and Attention-Head Sharpening (AHS) – and a deployment protocol, Context-Aware Hyperparameter Routing (CAHR), that selects configurations by context length and audit side. Across five benchmarks spanning synthetic data, long-context attacks, and reasoning-model outputs, CAHR-CD and CAHR-AHS improve the six-guardrail average by 22% and 13%, respectively. Code and data are available online. 1^{1}

1 Introduction

Safety guardrails serve as real-time moderators of LLM inputs and outputs and have become indis-

Figure

Figure 1: Long-context attacks (on the input side) and long reasoning outputs (on the output side) pose new challenges for safety guardrails trained on short text.

pensable for LLM safety, with multiple generations (Inan et al., 2023; Han et al., 2024; Zeng et al., 2024; Kumar et al., 2025; Zhao et al., 2025; Lin et al., 2026) widely deployed in short-text scenarios. The sequences they moderate, however, are growing longer (Figure 1): Many-shot Jailbreaking (Anil et al., 2024) and NINJA Attack (Shah et al., 2025) distribute harmful instructions across long prompts to bypass safety alignment, and reasoning models such as DeepSeek-R1 (Guo et al., 2025) and Qwen3 (Yang et al., 2025) produce thousands of intermediate tokens that may carry latent risks. Together, they shift the deployment distribution of guardrails from short text to long context. While long-context safety of LLMs themselves has begun to receive attention (Lu et al., 2025; Huang et al., 2025), whether guardrails still play the role of the last line of defense under long context remains open.

We organize the paper around three research questions. (RQ1) Does guardrail detection degrade systematically with context length? (RQ2) If so, what is the internal mechanism? (RQ3) Can the mechanism guide training-free mitigations? We answer them in LongGuard, a unified evaluation, analysis, and mitigation framework with three contributions (Figure 2).

∗^{*}Corresponding author.

1^{1}https://github.com/caskcsg/LongGuard

Figure

Figure 2: Overview of LongGuard: evaluation, mechanistic analysis, and training-free mitigation.

Evaluation. We formulate the task as Safety Needle-in-a-Haystack (SafetyNIAH) and synthesize a benchmark over a 0.25k–32k length grid with controlled needle type, position, and haystack type; a paired Benign-Fill / Needle-Repeat design separates proportional dilution from absolute length. Across 15 guardrails, unsafe recall declines monotonically, while safe recall remains flat—an asymmetric degradation driven mainly by dilution.

Analysis. On six guardrails, a three-layer attention–logit–behavior framework localizes the failure to dilution of unsafe evidence by the haystack. It isolates a sparse set of guard-specialized retrieval heads that exhibit partial specificity relative to their base models.

Mitigation. Two training-free methods follow: Chunked Detection (CD) chunks the input to raise local evidence density, and Attention-Head Sharpening (AHS) sharpens the softmax temperature on the retrieval heads; Context-Aware Hyperparameter Routing (CAHR) selects hyperparameters by context length and audit side. Across five benchmarks, they restore long-context detection and form a performance-cost trade-off.

References (33)