MAGE: Safeguarding LLM Agents against Long-Horizon Threats via Shadow Memory
Yuhui Wang, Tanqiu Jiang, Jiacheng Liang, Charles Fleming, Ting Wang · 2026
Read MAGE: Safeguarding LLM Agents against Long-Horizon Threats via Shadow Memory by Yuhui Wang, Tanqiu Jiang, Jiacheng Liang, Charles Fleming, Ting Wang (2026) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2605-03228.txt.
Introduction
Yuhui Wang, Tanqiu Jiang, Jiacheng Liang, Charles Fleming, and Ting Wang present ShadowMem, a defense for language model agents against attacks that unfold over many turns. Such long-horizon threats build through extended interaction and are nearly impossible in single exchanges, yet they endanger agents in critical settings.
Borrowing the shadow stack idea from systems security, ShadowMem keeps a separate safety memory that distills key context across the whole run and checks pending actions before they execute. In tests it beat existing defenses on detection, caught most attacks early, and cost little in agent usefulness.
Catalog record
| CREATOR | Yuhui Wang, Tanqiu Jiang, Jiacheng Liang, Charles Fleming, Ting Wang |
|---|---|
| YEAR | 2026 |
| COLLECTION | PAPERS |
| FORMAT | TXT |
| FILE SIZE | 104 KB |
| LICENSE | http://creativecommons.org/licenses/by/4.0/ |
| RIGHTS | CC BY |
| SOURCE | arXiv |
Source: https://arxiv.org/abs/2605.03228
Browse the full library · About the Tower of Babel
Full text
The opening of the work, from the library's hosted text:
MAGE: Safeguarding LLM Agents against Long-Horizon Threats via Shadow Memory Yuhui Wang, Tanqiu Jiang, Jiacheng Liang, Charles Fleming, Ting Wang Abstract . As large language model (LLM)-powered agents are increasingly deployed to perform complex, real-world tasks, they face a growing class of attacks that exploit extended user–agent–environment interactions to pursue malicious objectives improbable in single-turn settings. Such long-horizon threats pose significant risks to the safe deployment of LLM agents in critical domains. In this paper, we present MAGE , 1 1 1 MAGE : M emory A s G uardrail E nforcement. a novel defensive framework designed to counter a wide range of long-horizon threats. Inspired by the “shadow stack” abstraction in systems security, MAGE maintains a dedicated, safety-focused agentic memory that distills and retains safety-critical context across the agent’s full execution trajectory, leveraging this shadow memory to proactively assess the risk of pending actions prior to their execution. Extensive evaluation demonstrates that MAGE substantially outperforms existing defenses across diverse long-horizon threats in detection accuracy, achieves early-stage detection for the majority of attacks, and introduces only negligible overhead to agent utility. To our best knowledge, MAGE represents the first framework to detect and mitigate long-horizon threats using an agentic memory approach, establishing a new paradigm for this critical challenge and opening promising directions for future research. 1. Introduction Large language model (LLM)-powered agents are increasingly deployed in settings that demand multi-turn, extended interactions ( 48 ; 49 ) . Yet, this very capability introduces a new class of security vulnerabilities: adversaries can exploit long-horizon contexts to mount attacks that are improbable in single-turn settings, including multi-turn poisoning ( 14 ) , tool-chaining attacks ( 31 ) , and persistent prompt injection ( 25 ) . For instance, as illustrated in Figure 1 , a tool-chaining attack orchestrates a sequence of individually innocuous tool calls whose cumulative effect is destructive, triggering compliance violations, financial losses, or data exfiltration. Such long-horizon attacks share a defining property: malicious intent often emerges only at the trajectory level, while remaining latent within individual turns. Most existing defenses, such as keyword filters, per-turn classifiers, and safety prompting, are engineered for short contexts and degrade substantially under the distributional shift to long-horizon interactions ( 2 ) . A handful of recent studies attempt to extend single-turn defenses to multi-turn settings ( 20 ; 82 ; 51 ) ; MTSA ( 17 ) , for instance, improves multi-turn alignment through dialogue-level training. Yet these extensions break down as interaction length exceeds the regimes seen during training, and nearly all focus narrowly on multi-turn jailbreak attacks, leaving the broader landscape of long-horizon threats without principled defensive measures. Figure 1 . (a) Tool-chaining attack; (b) MAGE defends against long-horizon attacks by distilling and retaining safety-relevant context in shadow memory when evaluating the risks of potential actions. Our work. In this paper, we explore a fundamentally different approach. Motivated by the observation that long-horizon attacks often succeed by manipulating the agent’s context across extended interactions, we repurpose agentic memory ( 86 ; 75 ) , originally designed to enhance the agent’s context awareness, as the foundation for our defensive measure. Specifically, drawing on the “shadow stack” abstraction ( 4 ) from systems security research, we introduce a dedicated, security-focused agentic memory that distills and retains security-critical context throughout the agent’s execution trajectory. This shadow memory intercepts pending actions prior to execution, assessing their risk against the accumulated trajectory context. For instance, as illustrated in Figure 1 , the shadow memory identifies that prior interactions have established intent to exfiltrate confidential data, and blocks the offending action before it executes. To realize this idea, we develop MAGE , the first framework to leverage an agent’s memory mechanism for detecting and mitigating long-horizon threats. At its core, MAGE comprises two components: a memory manager that distills and maintains the shadow memory, and a judge that consults this memory to perform risk assessment. Both are trained via reinforcement learning (e.g., GRPO ( 58 ) ) to jointly optimize detection accuracy, utility preservation, and computational efficiency. MAGE offers three key advantages over alternative approaches: Versatile — it addresses a broad spectrum of long-horizon threats, from adversarial prompting to indirect prompt injection; Plug-and-Play — it integrates readily with existing LLM agents with minimal modification to the core inference pipeline; and Interpretable — it produces human-readable explanations grounded in the shadow memory, making detection decisions transparent to operators. Extensive empirical evaluation demonstrates that MAGE provides strong defense across two complementary threat models. Under the user-as-adversary setting, using a small local model ( Qwen3-4B ) as the backbone for both the memory manager and judge, MAGE reduces the attack success rate (ASR) of sequential tool-attack chaining (STAC) ( 31 ) from 100.0% to 8.3%, while preserving 94.4% benign utility (BU) at a cost of only 7.0K extra tokens per task. Under the environment-as-adversary setting, MAGE reduces the ASR of persistent indirect prompt injection (PI2) ( 25 ) to 0.0%, while preserving 73.0% benign utility, on par with the no-defense upper bound. Notably, in both settings, MAGE detects the majority of attacks in their early stages (at or near the first attack turn), affording operators sufficient time to intervene. Taken together, these results establish MAGE as a strong solution across the dimensions of security, utility, and overhead. Our contributions. To the best of our knowledge, this work is the first to leverage agentic memory for detecting and mitigating long-horizon threats. Our contributions are as follows. \bullet We identify that the key to defending LLM agents against long-horizon threats lies in maintaining security-critical context across the agent’s execution trajectory. \bullet We develop MAGE , the first framework that exploits agentic memory to distill and retain security-critical context and perform retrospective, trajectory-level risk assessment. \bullet We conduct extensive empirical evaluation demonstrating that MAGE outperforms existing defenses across diverse attacks, achieves early detection for the majority of attacks, and introduces negligible overhead. \bullet We discuss the limitations of MAGE ’s current design and outline promising directions for future research. 2. Related Work 2.1. Adversarial Prompting LLM agents face a variety of threats in realistic deployments. One class assumes adversarial users and extends jailbreak techniques ( 88 ; 40 ; 6 ) , aiming to trigger the agent into misbehavior, including unauthorized risky actions ( 53 ) , memory manipulation ( 10 ; 14 ) , and unsafe code generation ( 16 ) . Recent work extends such attacks to multi-turn settings. For instance, STAC ( 31 ) maintains a stateful, context-aware attack chain to progressively steer the agent toward policy violations. MINJA ( 14 ) compromises the agent’s memory through query-only interaction, injecting malicious records that elicit harmful responses in subsequent turns. In response, a handful of defenses have been proposed to prevent agents from executing harmful actions, drawing on established jailbreak mitigation ( 83 ; 85 ; 46 ; 20 ; 82 ; 51 ) . For instance, MTSA ( 17 ) enhances multi-turn alignment through dialogue-level training. However, these defenses tend to degrade as interaction length exceeds the conditions encountered during training. 2.2. Indirect Prompt Injection Indirect prompt injection attacks assume adversarial environments, embedding malicious instructions into external content to manipulate agent behavior. Proposed attacks span manually crafted templates (