[Paper Review] Token-Level Adversarial Prompt Detection Based on Perplexity Measures and Contextual Information
This paper proposes a token-level adversarial prompt detection method that leverages perplexity and contextual information from neighboring tokens to identify malicious inputs in LLMs. By combining optimization-based and probabilistic graphical model (PGM) approaches, the method achieves high detection accuracy—even with small models like GPT-2-small—demonstrating strong performance in identifying adversarial sequences through visualized heatmap overlays.
In recent years, Large Language Models (LLM) have emerged as pivotal tools in various applications. However, these models are susceptible to adversarial prompt attacks, where attackers can carefully curate input strings that mislead LLMs into generating incorrect or undesired outputs. Previous work has revealed that with relatively simple yet effective attacks based on discrete optimization, it is possible to generate adversarial prompts that bypass moderation and alignment of the models. This vulnerability to adversarial prompts underscores a significant concern regarding the robustness and reliability of LLMs. Our work aims to address this concern by introducing a novel approach to detecting adversarial prompts at a token level, leveraging the LLM's capability to predict the next token's probability. We measure the degree of the model's perplexity, where tokens predicted with high probability are considered normal, and those exhibiting high perplexity are flagged as adversarial. Additionaly, our method also integrates context understanding by incorporating neighboring token information to encourage the detection of contiguous adversarial prompt sequences. To this end, we design two algorithms for adversarial prompt detection: one based on optimization techniques and another on Probabilistic Graphical Models (PGM). Both methods are equipped with efficient solving methods, ensuring efficient adversarial prompt detection. Our token-level detection result can be visualized as heatmap overlays on the text sequence, allowing for a clearer and more intuitive representation of which part of the text may contain adversarial prompts.
Motivation & Objective
- To address the vulnerability of LLMs to adversarial prompts that manipulate model outputs through carefully crafted inputs.
- To detect adversarial prompts at the token level rather than at the sequence level, enabling precise localization of malicious content.
- To improve detection reliability by integrating contextual information from neighboring tokens alongside perplexity scores.
- To develop efficient, lightweight detection methods suitable for integration into real-world LLM systems without high computational cost.
Proposed method
- Leverages the language model’s next-token probability predictions to compute token-level perplexity as a core indicator of adversarial likelihood.
- Integrates contextual information from adjacent tokens to detect contiguous adversarial sequences, improving robustness against fragmented or subtle attacks.
- Employs two distinct detection algorithms: one based on optimization techniques and another using Probabilistic Graphical Models (PGM) for joint probability estimation.
- Visualizes detection results using heatmap overlays on input text, where color intensity reflects the likelihood of each token being adversarial.
- Uses GPT-2-small (124M parameters) as a base model to ensure low computational overhead and broad deployability.
- Applies perplexity aggregation and smoothing techniques to enhance detection stability and reduce false positives.
Experimental results
Research questions
- RQ1Can perplexity at the token level reliably distinguish adversarial prompts from normal inputs in LLMs?
- RQ2How does incorporating neighboring token context improve the detection of contiguous adversarial sequences?
- RQ3Can an efficient, lightweight detection system be built using small language models without sacrificing accuracy?
- RQ4To what extent do optimization-based and PGM-based detection methods outperform sequence-level approaches in precision and localization?
- RQ5How effective is the method in detecting adversarial prompts generated via discrete optimization, a common attack vector?
Key findings
- The proposed method achieves perfect performance in sentence-level adversarial prompt detection using GPT-2-small, indicating high reliability at the sequence level.
- Token-level detection using perplexity and context-aware analysis successfully identifies adversarial tokens with high precision, even in complex, optimized attack patterns.
- The PGM-based method demonstrates strong performance in modeling dependencies between adjacent tokens, improving detection of multi-token adversarial sequences.
- The optimization-based approach enables efficient, scalable detection with low inference overhead, suitable for real-time deployment.
- Heatmap visualizations effectively highlight adversarial regions in input text, offering interpretable and actionable insights for developers and security teams.
- The method maintains high accuracy even when applied to small, resource-efficient models like GPT-2-small, significantly reducing hardware and computational requirements.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.