Manuscript Offprint

arXiv:2609.37737

Where Do LLMs Decide to Break the Rules? Mechanistic Localization of Prompt Injection Compliance

Rui Wen, Jiayang Liu, Zeyu Yang, Jun Sakuma, Lu Sun

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

ABSTRACT

When a prompt injection attack succeeds, a Large Language Model (LLM) abandons its assigned system role to comply with an adversarial instruction. While prior work has extensively quantified how often this occurs, we ask a more fundamental question: where inside the network does the model actually decide to break the rules? Using layer-by-layer causal activation patching across five models (4B to 32B parameters), we find a clear dissociation: attack information is linearly decodable from the first layer, yet causal leverage over the model's behavior is negligible until a late-layer bottleneck in the final third of the network. Patching this bottleneck reverses compliance in 77–92% of cases. We show that the compliance mechanism occupies a compact linear subspace (rank-8 in 4B and 14B models, scaling to rank-64 at 32B) and is architecturally stable across varying model families. Finally, we validate our mechanistic account by showing that this causal peak layer is also the representationally optimal site for detecting attacks, outperforming early-layer classifiers that degrade under surface-level obfuscation such as leetspeak substitution. This alignment between causal leverage and detection performance provides converging evidence that the late-layer bottleneck captures decision-relevant computation rather than merely reflecting an artifact of the intervention.

1 INTRODUCTION

Prompt injection exploits a fundamental vulnerability in LLMs: the absence of a hard security boundary between developer instructions and user inputs. An attacker who controls any part of the context window can override the system prompt simply by inserting new instructions into the prompt (Perez & Ribeiro, 2022). As LLMs are deployed as autonomous agents with access to tools, databases, and external APIs, this vulnerability escalates from an inconvenience to a direct privilege-escalation vector: injections embedded in retrieved documents, emails, or tool outputs can silently redirect an agent's actions in the real world (Greshake et al., 2023; Zhan et al., 2024).

A growing literature measures how often attacks succeed (Liu et al., 2024; Yi et al., 2025; Debenedetti et al., 2024) and proposes defenses ranging from input filtering (Chen et al., 2025; Hines et al., 2024) to system-prompt hardening (Wallace et al., 2024). Yet all of these approaches treat the model as a black box and offer no principled account of why compliance happens. Without knowing where inside the network the decision is made, defenses remain heuristic: an adversary who understands the mechanism has a structural advantage over a defender who does not.

Why mechanistic localization matters. Knowing where causal leverage concentrates has three concrete consequences. First, it enables surgical intervention. If the decision to comply with an attack is bottlenecked in a specific region of the network, defenders can focus their monitoring and steering efforts exactly there. This isolates the defense, avoiding the need to retrain or modify the entire model. Second, it dictates the feasibility of lightweight runtime defenses. If leverage is concentrated, a probe on one layer may suffice; if leverage is broadly distributed, intervention must be systemic. Third, it reveals a structural vulnerability that we can directly exploit for detection. While prior work shows that models often encode concept layers before actually acting on them (Belrose et al., 2023), we

Figure

Figure 1: Mechanistic localization and defense framework. Left: resistant, compliant, and stealthy/obfuscated attacks define the contrast used for causal intervention. Middle: attack presence is decodable from early layers, yet causal leverage remains near zero until a late-layer bottleneck. The underlying mechanism is compact and low-rank (empirically rank ≈\approx 8 in 4B–14B models). Right: the causal peak captures decision-level semantics rather than surface form, making it the most effective and obfuscation-robust site for detection.

demonstrate how extreme this gap is for prompt injection: the attack is recognizable by the first layer, yet causal leverage over whether the model complies does not appear until late in the network. We leverage this exact split to build a robust defense. Because early layers primarily process surface-level text, detectors placed there are easily blinded by simple obfuscations like leetspeak. By contrast, a detector placed at the true causal peak targets the model's underlying intent, making it substantially more robust against evasion.

Our approach. We apply causal activation patching (Meng et al., 2022; Geiger et al., 2025): when a model complies with an injection, we intercept its forward pass at layer ℓ\ell and replace the hidden state with the average state of resistant runs. If the model then refuses, that layer has causal leverage. Sweeping every layer produces a precise causal map. The result is consistent across different models and attack types: attack information is linearly decodable from the first layer, yet causal leverage over the model's behavior is negligible until a late-layer bottleneck. Figure 1 presents an overview of our framework, illustrating how prompt injection behaviors are mechanistically localized to a late-layer causal bottleneck and governed by a compact low-dimensional subspace, enabling robust detection and intervention.

In summary, we make four main contributions:

• Causal localization of prompt injection compliance. We provide the first systematic layer-wise causal analysis of prompt injection. Across models from 4B to 32B, causal influence is negligible in early layers and peaks in a late-layer bottleneck. This reveals a clear separation between representation and action.

• Mechanistic characterization of the compliance process. We demonstrate that the compliance mechanism operates within a low-dimensional linear subspace, suggesting it is a structured circuit rather than a diffuse property of the network.

• Robustness across settings and attack types. The location of this late-layer window is stable across the dense architectures we test, across decoding temperatures, and under adversarial suffixes and multi-turn escalations; stealthy attacks, which travel different representational pathways in early layers, converge on the same late block.

• Detection as validation of the mechanism. Our causal analysis predicts that the optimal detection layer should align with the causal bottleneck. We confirm this empirically: the causal peak layer yields the best detection performance among the tested layers, and its detector is robust to obfuscation. This provides external validation of the mechanistic account.

References (42)

  • Refusal in Language Models Is Mediated by a Single Direction
  • Understanding Jailbreak Success: A Study of Latent Space Dynamics in Large Language Models
  • Eliciting Latent Predictions from Transformers with the Tuned Lens (opens arXiv in a new tab)
  • StruQ: Defending Against Prompt Injection with Structured Queries
  • AgentDojo: A Dynamic Environment to Evaluate Prompt Injection Attacks and Defenses for LLM Agents
  • Causal Abstraction: A Theoretical Foundation for Mechanistic Interpretability
  • Localizing Model Behavior with Path Patching (opens arXiv in a new tab)
  • Not What You've Signed Up For: Compromising Real-World LLM-Integrated Applications with Indirect Prompt Injection
  • Do LLMs "know" internally when they follow instructions? In International Conference on Learning Representations (ICLR), 2025
  • Defending Against Indirect Prompt Injection Attacks With Spotlighting
  • Mistral 7B
  • Safety Layers in Aligned Large Language Models: The Key to LLM Security
  • Formalizing and Benchmarking Prompt Injection Attacks and Defenses
  • DataSentinel: A Game-Theoretic Detection of Prompt Injection Attacks
  • Locating and Editing Factual Associations in GPT. In Annual Conference on Neural Information Processing Systems (NeurIPS)
  • Ignore Previous Prompt: Attack Techniques For Language Models (opens arXiv in a new tab)
  • Steering Llama 2 via Contrastive Activation Addition
  • Steering Language Models With Activation Engineering
  • The Instruction Hierarchy: Training LLMs to Prioritize Privileged Instructions (opens arXiv in a new tab)
  • Defending against Indirect Prompt Injection by Instruction Detection
  • Qwen3 Technical Report
  • Benchmarking and Defending against Indirect Prompt Injection Attacks on Large Language Models
  • InjecAgent: Benchmarking Indirect Prompt Injections in Tool-Integrated Large Language Model Agents
  • Understanding and Enhancing Safety Mechanisms of LLMs via Safety-Specific Neuron
  • How Alignment and Jailbreak Work: Explain LLM Safety through Intermediate Hidden States
  • Representation Engineering: A Top-Down Approach to AI Transparency
  • You help customers with their orders, returns, and general questions about AcmeCorp products
  • If the mechanism is low-rank, flip rate should saturate well before k reaches the full hidden dimension (2560 for Qwen3-4B)
  • To determine which component is responsible for the causal signal, we run three patching variants at each of seven layers: (a) full, replacing the entire
  • The 80% threshold is crossed at $k = 8$ for Qwen3-4B and Qwen3-14B and at $k = 64$ for Qwen3-32B (2560, 5120, and 5120 hidden dimensions)
  • The result (Table 6) is clear: at L24, neither sublayer alone accounts for more than $24\%$ of the full $90.5\%$ effect
  • The compliance representation is built up progressively across the preceding blocks and arrives encoded in the residual stream entering L24; L24 itself does not freshly compute the compliance outcome
  • We constructed 600 stimuli (300 topics × 2 prefix variants) and measured a 25.0% natural compliance rate (150/600), compared with 28% on the multi-template direct corpus
  • The raw rise is late, but at L36 the norm-matched random control also flips 0.433
  • The dilution above is a property of the indirect-own donor, not of the target layer
  • Table 9 shows that the causal profile remains late
  • The raw flip rates at L24 (0.66–
  • This is consistent with our central claim that the late-layer location is distribution-robust while the absolute magnitude tracks phrasing diversity
  • Both peak at L24 with the largest gap over random
  • InjecAgent, with poisoned tool outputs targeting data theft or direct harm, and AgentDojo, with attacker goals across banking, Slack, travel, and workspace tasks
  • A linear probe separating injected from benign tool results (matched pairs differing only in the tool output) reaches attack-presence AUROC = 1.00 from the first layer on Qwen3-4B, Qwen3-14B, and Mistral-7B
  • The suffix targets L24 activations and does not transfer to the text classifier