Auditing and Adapting kNNGuard for Long-Context Prompts
Abstract
Training-free activation guardrails such as kNNGuard score prompts by comparing last-token LLM hidden states (the Layer Ensemble branch, kNNGuard LE) and sentence embeddings (all-MiniLM-L6-v2, Embedding-kNN) against a small reference bank (50 examples per class), combining the two branches into a Fused Ensemble (kNNGuard FE). While effective on short inputs (up to 256 tokens), standard kNNGuard degrades substantially on long inputs (over 1000 tokens): averaged across Qwen3.5-4B and Gemma-4-E4B-it, kNNGuard LE AUROC drops from 0.742 (0.777 and 0.706) to 0.554 (0.587 and 0.521) on Content Harm Moderation (Aegis-2.0), and from 0.994 (0.995 and 0.992) to 0.556 (0.598 and 0.513) on RAG Hallucination Detection (HaluBench). We trace this degradation to three causes: (1) hidden-state drift as sequence length increases, (2) hard 256-token truncation in MiniLM, and (3) signal dilution when a long document is compressed into a single final-token vector. We then evaluate three training-free adaptations: a Length-Routed Dual Bank, a long-context embedding model (Qwen3-Embedding-0.6B), and Strided Window Pooling across intermediate token positions. On Content Harm Moderation, dual-bank routing and strided window pooling raise long-context kNNGuard LE AUROC from 0.554 to 0.828 (+0.274 AUROC) and fused kNNGuard FE AUROC from 0.721 to 0.886 (+0.164 AUROC) with no latency overhead from bank routing. On RAG Hallucination Detection, where the guardrail must verify logical consistency between a generated answer and a retrieved passage, sentence-embedding kNN remains near random chance across all lengths, while length-matched LLM hidden states (kNNGuard LE) recover medium-length detection (AUROC = 0.853) but remain limited on over-1000-token passages (AUROC = 0.607) due to final-token dilution over long contexts.
Figures & tables
| Backbone | Benchmark | Branch | Short ( ) | Medium ( – ) | Long ( ) | (Long Short) |
|---|---|---|---|---|---|---|
| Qwen3.5-4B | Aegis-2.0 (Safety) | kNNGuard LE | 0.777 | 0.736 | 0.587 | -0.190 |
| Qwen3.5-4B | Aegis-2.0 (Safety) | kNNGuard FE | 0.810 | 0.806 | 0.755 | -0.055 |
| Qwen3.5-4B | HaluBench (RAG) | kNNGuard LE | 0.995 | 0.725 | 0.598 | -0.397 |
| Qwen3.5-4B | HaluBench (RAG) | kNNGuard FE | 0.995 | 0.724 | 0.598 | -0.398 |
| Gemma-4-E4B-it | Aegis-2.0 (Safety) | kNNGuard LE | 0.706 | 0.628 | 0.521 | -0.186 |
| Gemma-4-E4B-it | Aegis-2.0 (Safety) | kNNGuard FE | 0.746 | 0.741 | 0.687 | -0.059 |
| Benchmark | Configuration | LE AUROC (95% CI) | Emb AUROC | FE AUROC (95% CI) | LE / FE |
|---|---|---|---|---|---|
| Aegis-2.0 (Safety) | Baseline (Short Bank) | 0.554 [0.501, 0.601] | 0.785 | 0.721 [0.679, 0.763] | +0.000 / +0.000 |
| Aegis-2.0 (Safety) | Mixed-Length Bank | 0.672 [0.622, 0.720] | 0.895 | 0.876 [0.843, 0.906] | +0.118 / +0.155 |
| Aegis-2.0 (Safety) | Length-Routed Dual Bank | 0.722 [0.673, 0.765] | 0.894 | 0.886 [0.855, 0.914] | +0.168 / +0.164 |
| Aegis-2.0 (Safety) | + Qwen3-Embedding-0.6B | 0.722 [0.673, 0.765] | 0.847 | 0.828 [0.790, 0.864] | +0.168 / +0.107 |
| Aegis-2.0 (Safety) | + Strided Window Pooling | 0.828 [0.790, 0.861] | 0.853 | 0.853 [0.817, 0.885] | +0.274 / +0.132 |
| HaluBench (RAG) | Baseline (Short Bank) | 0.556 [0.510, 0.600] | 0.479 | 0.557 [0.512, 0.600] | +0.000 / +0.000 |
| Benchmark | Configuration | LE AUROC (95% CI) | Emb AUROC | FE AUROC (95% CI) | LE / FE |
|---|---|---|---|---|---|
| Aegis-2.0 (Safety) | Baseline (Short Bank) | 0.682 [0.634, 0.732] | 0.809 | 0.773 [0.733, 0.812] | +0.000 / +0.000 |
| Aegis-2.0 (Safety) | Mixed-Length Bank | 0.716 [0.675, 0.760] | 0.871 | 0.862 [0.830, 0.896] | +0.034 / +0.088 |
| Aegis-2.0 (Safety) | Length-Routed Dual Bank | 0.782 [0.742, 0.820] | 0.874 | 0.870 [0.836, 0.901] | +0.099 / +0.096 |
| Aegis-2.0 (Safety) | + Qwen3-Embedding-0.6B | 0.782 [0.742, 0.820] | 0.818 | 0.829 [0.795, 0.863] | +0.099 / +0.056 |
| Aegis-2.0 (Safety) | + Strided Window Pooling | 0.808 [0.771, 0.850] | 0.823 | 0.823 [0.788, 0.858] | +0.126 / +0.050 |
| HaluBench (RAG) | Baseline (Short Bank) | 0.710 [0.669, 0.749] | 0.538 | 0.708 [0.667, 0.747] | +0.000 / +0.000 |
LLM Guardrails
Large language model guardrails detect or constrain harmful outputs, prompt attacks, and unsafe agent actions before they cause harm.
See all 218 papers