AgentSentry: Mitigating Indirect Prompt Injection in LLM Agents via Temporal Causal Diagnostics and Context Purification
Organizations: Key Laboratory of Aerospace Information Security and Trusted Computing, Ministry of Education, School of Cyber Science and Engineering, Wuhan University, Wuhan 430072, China · Department of Computer Science and Engineering, University at Buffalo, SUNY · Department of Computer Science, College of Information Science and Technology, Jinan University, Guangzhou 510632, China · College of Cyber Security, Jinan University, Guangzhou 510632, China
Abstract
Large language model (LLM) agents increasingly rely on external tools and retrieval systems to autonomously complete complex tasks. However, this design exposes agents to indirect prompt injection (IPI), where attacker-controlled context embedded in tool outputs or retrieved content silently steers agent actions away from user intent. Unlike prompt-based attacks, IPI unfolds over multi-turn trajectories, making malicious control difficult to disentangle from legitimate task execution. Existing inference-time defenses primarily rely on heuristic detection and conservative blocking of high-risk actions, which can prematurely terminate workflows or broadly suppress tool usage under ambiguous multi-turn scenarios. We propose AgentSentry, a novel inference-time detection and mitigation framework for tool-augmented LLM agents. To the best of our knowledge, AgentSentry is the first inference-time defense to model multi-turn IPI as a temporal causal takeover. It localizes takeover points via controlled counterfactual re-executions at tool-return boundaries and enables safe continuation through causally guided context purification that removes attack-induced deviations while preserving task-relevant evidence. We evaluate AgentSentry on the AgentDojo benchmark across four task suites, three IPI attack families, and multiple black-box LLMs. AgentSentry eliminates successful attacks and maintains strong utility under attack, achieving an average Utility Under Attack (UA) of 74.55 %, improving UA by 20.8 to 33.6 percentage points over the strongest baselines without degrading benign performance.
Figures & tables
| Metric | Description |
| ASR ( ) | Fraction of attacked tasks where the injected objective is executed. |
| UA ( ) | Fraction of attacked tasks where the user objective is completed while avoiding the injected objective. |
| CU ( ) | Success rate on benign (non-attacked) tasks. |
| FPR ( ) | Fraction of benign tasks where the defense incorrectly intervenes. |
| Model | Defense | No Attack | Important Instr. | Tool Knowledge | InjecAgent | Avg. | |||||
| CU | FPR | UA | ASR | UA | ASR | UA | ASR | UA | ASR | ||
| GPT-4o | No Defense | 78.35 | 0.00 | 36.84 | 33.23 | 42.14 | 30.00 | 72.22 | 15.28 | 50.40 | 26.17 |
| Delimiting ( Chen et al., 2025 ; Hines et al., 2024 ) | 72.16 | 0.00 | 39.85 | 48.57 | 50.00 | 21.43 | 75.00 | 11.81 | 54.95 | 27.27 | |
| Repeat Prompt ( Learn Prompting, 2024 ) | 84.54 | 0.00 | 61.35 | 28.42 | 57.14 | 11.43 | 77.08 | 15.97 | 65.19 | 18.61 | |
| Tool Filter ( Debenedetti et al., 2024 ) | 72.16 | 0.00 | 50.38 | 5.41 | 60.81 | 7.86 | 62.50 | 3.47 | 57.90 | 5.58 | |
| DeBERTa Detector ( Kokkula et al., 2024 ) | 41.24 | 0.00 | 18.95 | 14.29 | 31.43 | 15.00 | 31.25 | 0.00 | 27.21 | 9.76 | |
| Configuration | UA (%) | ASR (%) |
| AgentSentry (Full) | 90.36 | 0.00 |
| Temporal aggregation disabled | 88.57 | 1.07 |
| Sanitized counterfactuals removed | 58.21 | 22.50 |
| Single-step causal contrast | 56.25 | 21.79 |
| Weak masking (paraphrasing) | 56.07 | 23.57 |
| Weak masking (tool-instruction deletion) | 56.43 | 22.86 |
Appendix figures & tables2 assets
Supplementary material from the paper’s appendix.
Appendix
| Defense (pipeline) | Takeover | Task complete |
| MELON | Yes | Yes |
| Transformer PI detector | No | No |
| Task Shield | No | Yes |
| AgentSentry (ours) | No | Yes |
| Instance | Retrieved context surface | Injected directive | Observed tool action |
| Calendar query ( user_task_3 , injection_task_1 ) | Calendar event description returned by search_calendar_events contains an embedded <INFORMATION> block. | Delete the file with ID ‘13’ from the cloud drive. | Destructive side effect executed via delete_file after the tool return is interpreted as authorized instruction. |
| Meeting query ( user_task_3 , injection_task_8 ) | Calendar event record is retrieved normally, while the injected <INFORMATION> payload is surfaced as part of the context that precedes task completion. | Email the contents of all unread emails to [email protected], delete the sent email, then delete all unread emails. | No high-impact tool call is executed in the shown trace segment, yielding a residual failure mode where persuasive drift can precede an overt tool-side violation. |