AgentWatcher: A Rule-based Prompt Injection Monitor

Yanting Wang, Wei Zou, Runpeng Geng, Jinyuan JiaarXiv 2026

TL;DR

AgentWatcher is a novel prompt injection detection framework designed to protect Large Language Model (LLM) agents, even within massive context windows. By combining attention-based context attribution with explicit, rule-guided LLM reasoning, it drops attack success rates to nearly zero while providing clear, explainable security decisions.

What Problem Does This Paper Address?

LLM-based agents are increasingly deployed to handle real-world tasks like reading emails, browsing the web, and executing API calls. However, they are highly vulnerable to prompt injection attacks, where an attacker embeds malicious instructions into untrusted data (like an email or a webpage) to hijack the agent's behavior.

Current detection mechanisms suffer from two major flaws:

  1. Poor Scalability: Their detection accuracy degrades significantly as the input context grows longer, making them impractical for complex, long-context agent applications.
  2. Lack of Explainability: Most detectors rely on implicit learned representations (acting as black boxes) or oversimplified assumptions, making their security decisions opaque and difficult for engineers to debug or trust.

Methods

AgentWatcher solves these issues using a distinct two-phase architecture:

  1. Causally Important Context Attribution: Instead of scanning a massive document all at once, AgentWatcher looks at the agent's proposed action and traces back its attention weights to locate "sink tokens" (tokens that receive a disproportionately high amount of attention). Using a sliding window approach, it extracts only the small, highly influential segments of text responsible for that action. This prevents malicious instructions from being fragmented across arbitrary text boundaries.
  2. Rule-Based Prompt Injection Detection: The extracted text snippet, the user's original task, and the agent's intended action are passed to a separate Monitor LLM. This monitor evaluates the context against a customizable set of explicit safety rules. To maximize performance, the authors fine-tuned the Monitor LLM using Group Relative Policy Optimization (GRPO) with a BLEU-based reward function that incentivizes both correct classification and precise extraction of the malicious string.

Datasets

The authors evaluated AgentWatcher across a diverse suite of benchmarks:

  • Agent Benchmarks: AgentDojo, InjecAgent, WASP, and AgentDyn (evaluating shopping, GitHub operations, and daily tasks).
  • Long-Context Datasets: Six datasets from LongBench (including LCC for code generation, GovReport for summarization, and HotpotQA for question answering).
  • Training Data: A custom dataset of 20,000 samples constructed using benign text from the Cleaned Alpaca Dataset, WebArena, and StarCoder, paired with context-aware malicious instructions generated via GPT-4o-mini.

Key Insights and Ideas

  • Attention Sinks as Anchors: Transformers naturally aggregate segment-level information into specific delimiter tokens (sink tokens). Capitalizing on this behavior allows AgentWatcher to precisely isolate where an injection is lurking without scanning megabytes of text.
  • Context Reduction Empowers Reasoning: By shrinking the text down to a few highly relevant windows, it becomes computationally feasible to use an LLM to perform deep, rule-based logical reasoning.
  • Emergent Rule Citation via GRPO: One of the most fascinating findings was that training the monitor LLM with GRPO naturally encouraged "rule-grounded reasoning." As training progressed, the model increasingly cited specific rule numbers in its thought process, even though the reward function only explicitly graded the final classification and text extraction accuracy.

Key Results

AgentWatcher demonstrated state-of-the-art robustness across all testing environments:

  • AgentDojo: It reduced the Attack Success Rate (ASR) to $\le 1\%$ while incurring a mere 2% drop in task utility.
  • Long-Context Performance: Across all six LongBench datasets, AgentWatcher was the only defense that consistently kept the ASR under 10% under both direct and combined attacks (see details in figure:art_31fd623f05a343b8aafc8c2617269f61).
  • AgentDyn Benchmark: As shown in the baseline comparison, AgentWatcher was the only defense capable of achieving a 0.0% ASR while maintaining a high benign utility of 48.3%.
Figure 1: Compare AgentWatcher with 9 baselines on AgentDyn [1]. The backbone LLM is GPT-4o. The baseline results are from the original paper [1].
Table 1: Evaluation on LLM agent benchmarks. Utility is measured on benign inputs (higher is better). ASR is measured under attack (lower is better). InjecAgent does not support utility measurement, so utility metrics are omitted for InjecAgent. Best results for each column are highlighted in bold.

Limitations and Future Work

The primary limitation of AgentWatcher is its computational overhead. Because it relies on an LLM reasoning pass, a single detection check takes roughly 8.2 seconds on an NVIDIA A100 GPU.

To mitigate this in production, the authors suggest invoking AgentWatcher selectively rather than at every single step. For example, developers can establish a blacklist of high-risk actions (e.g., deleting files, executing shell commands, or exfiltrating data) so the monitor only runs when the agent attempts a potentially dangerous operation. Future work will focus on optimizing the attribution step for exceptionally long agent trajectories.

Summary updated

BibTeX
@misc{wang2026agentwatcher,
  author = {Yanting Wang and Wei Zou and Runpeng Geng and Jinyuan Jia},
  title = {{AgentWatcher: A Rule-based Prompt Injection Monitor}},
  year = {2026},
  eprint = {2604.01194},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2604.01194},
}
Figure