kNNGuard: Turning LLM Hidden Activations into a Training-Free Configurable Guardrail

Mahmoud Abdelfattah, Hamid Nasiri, Peter Garraghan
7/2/2026
cs.LGcs.AIcs.CR

No confident code implementation found for this paper. 1 possible match can be shown below.

Abstract

Large language models (LLMs) are increasingly deployed in domains requiring guardrails to detect unsafe, off-topic, or adversarial prompts. Existing guardrails predominately rely on fine-tuning to build classifiers, which often suffer from low generalization and high inference latency. We present kNNGuard, a training-free guardrail that utilizes the activation space of an off-the-shelf LLM. Given a small bank of 50 safe and unsafe prompts, kNNGuard extracts hidden activations and performs multi-layer kNN fusing activation-space and embedding-space scores for classification. Across six domains spanning topical and security prompts, kNNGuard achieves competitive or superior F1 compared to fine-tuned state-of-the-art guardrails while running 2.7x faster than the best comparable guardrail, and 10x faster than a fine-tuned safety classifier without gradient updates or fine-tuning. Domain adaptation requires only updating the labeled bank, which can be constructed in under 10 seconds and several orders of magnitude faster than established guardrails. We also analyze the impact of system prompts, layer selection, and integration into production LLM pipelines as a configurable, low-latency guardrail.

View on arXivView PDF

Code Implementations

No confident code match yet

We couldn't find an author-owned or strongly-evidenced community implementation for this paper. 1 weaker match is hidden by default — verify before relying on them.

No code implementations found yet.

Know of an implementation? Let us know in the comments below!

Similar Papers

Cite this paper

@article{abdelfattah2026knnguard,
  title  = {kNNGuard: Turning LLM Hidden Activations into a Training-Free Configurable Guardrail},
  author = {Mahmoud Abdelfattah and Hamid Nasiri and Peter Garraghan},
  year   = {2026},
  eprint = {2607.02072},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2607.02072v1}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2607.02072.svg)](https://codeofpaper.com/paper/2607.02072)

Discussion