Prefix Probing: Lightweight Harmful Content Detection for Large Language Models

Jirui Yang, Hengqi Guo, Zhihui Lu, Yi Zhao, Yuansen Zhang, Shijing Hu, Qiang Duan, Yinggui Wang, Tao WeiarXiv 2025 · DOI · arXiv

Abstract

Deploying Large Language Models (LLMs) safely involves navigating the "impossible triangle" of accuracy, latency, and cost. While external guardrails are accurate, they often introduce significant computational overhead. We present Prefix Probing, a black-box safety method that detects harmful prompts by comparing the model's inherent likelihood of generating "agreement" versus "refusal" prefixes. By leveraging KV caching, Prefix Probing achieves high detection accuracy with negligible computational overhead, matching the performance of heavy-duty external classifiers while maintaining near-zero latency.

Introduction: The Safety Trade-off

Current LLM safety mechanisms typically fall into three categories: external guardrail models (e.g., LlamaGuard), response-process methods (e.g., self-auditing), and internal-feature methods (e.g., perplexity analysis). External models are accurate but double infrastructure costs and latency. Response-process methods are slow due to multi-pass requirements. Internal-feature methods are fast but often lack semantic depth, failing to identify sophisticated jailbreaks.

Prefix Probing addresses these limitations by utilizing the model's own generative biases. The core hypothesis is that an LLM naturally "wants" to refuse harmful prompts and agree with benign ones. By quantifying this preference through prefix probabilities, we can detect risk without external dependencies.

Methodology

The Prefix Probing framework consists of two primary stages: offline prefix construction and online detection.

1. Search-Based Prefix Construction (Offline)

Rather than manually selecting prefixes, a beam search algorithm identifies "highly informative" tokens for a specific model. The goal is to find strings that maximize the probability gap between harmful and benign inputs. For instance, a refusal prefix like "I cannot assist" will have a much higher probability for a harmful prompt than for a benign one.

2. Prefix-Based Harmfulness Scoring (Online)

During inference, the system calculates the log-probability of the model generating a set of "Refusal Prefixes" ($P_R$) versus "Agreement Prefixes" ($P_A$). If $P_R \ge P_A$, the query is flagged as harmful.

The Prefix Probing pipeline: (1) Offline search-based prefix construction and (2) Online detection using KV caching during the prefill phase.

Efficiency via KV Caching: To avoid the cost of running multiple probes, the method utilizes Key-Value (KV) caching. The user's prompt is processed once and the state is "frozen." The system then performs incremental calculations for each short probe, reducing overhead to the time required for a single token generation.

Key Insights: LLMs as Their Own Judges

The effectiveness of Prefix Probing stems from the clear separation in the model's output distribution when presented with different prompt types. As shown in the probability density plots, harmful prompts significantly shift the model's bias toward refusal prefixes, while benign prompts favor agreement.

Average log probability distributions for agreement and refusal prefixes across harmful and benign prompts.

This suggests that a model’s raw probability distribution is inherently aware of risk. Prefix Probing effectively "probes" this internal knowledge during the prefill phase, capturing semantic risk for the price of a simple statistical check.

Evaluation and Results

The method was evaluated across five safety benchmarks, including HHI, OpenAIModeration, Aegis 2.0, and WildGuardTest.

  • Accuracy: Prefix Probing achieved F1 scores comparable to state-of-the-art external models like LlamaGuard3-8B and Qwen3Guard.
  • Efficiency: The use of prefix caching resulted in a 5x to 16x speedup compared to non-cached probing.
  • Latency: The additional overhead is roughly equivalent to generating the first token of a response, making it virtually imperceptible to users.
  • Capability Capture: The method captured over 90% of the "theoretical upper bound" of the model's safety knowledge.

Conclusion and Future Work

Prefix Probing offers a high-efficiency alternative to traditional guardrails. However, limitations remain: prefixes are model-specific and do not transfer perfectly across different architectures. Furthermore, reasoning-oriented models (using Chain-of-Thought) may require different probing strategies. Future research will explore using these probes for active steering—dynamically guiding models away from harmful generation paths in real-time.

Summary updated

Abstract

Large language models often face a three-way trade-off among detection accuracy, inference latency, and deployment cost when used in real-world safety-sensitive applications. This paper introduces Prefix Probing, a black-box harmful content detection method that compares the conditional log-probabilities of "agreement/execution" versus "refusal/safety" opening prefixes and leverages prefix caching to reduce detection overhead to near first-token latency. During inference, the method requires only a single log-probability computation over the probe prefixes to produce a harmfulness score and apply a threshold, without invoking any additional models or multi-stage inference. To further enhance the discriminative power of the prefixes, we design an efficient prefix construction algorithm that automatically discovers highly informative prefixes, substantially improving detection performance. Extensive experiments demonstrate that Prefix Probing achieves detection effectiveness comparable to mainstream external safety models while incurring only minimal computational cost and requiring no extra model deployment, highlighting its strong practicality and efficiency.
BibTeX
@misc{yang2025prefix,
  author = {Jirui Yang and Hengqi Guo and Zhihui Lu and Yi Zhao and Yuansen Zhang and Shijing Hu and Qiang Duan and Yinggui Wang and Tao Wei},
  title = {{Prefix Probing: Lightweight Harmful Content Detection for Large Language Models}},
  year = {2025},
  eprint = {2512.16650},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2512.16650},
}
Figure