Abstract
Transformer-based Large Language Models (LLMs) have demonstrated powerful in-context learning capabilities. However, their predictions can be disrupted by factually correct context, a phenomenon known as context hijacking, revealing a significant robustness issue. To understand this phenomenon theoretically, we explore an in-context linear classification problem based on recent advances in linear transformers. In our setup, context tokens are designed as factually correct query-answer pairs, where the queries are similar to the final query but have opposite labels. Then, we develop a general theoretical analysis on the robustness of the linear transformers, which is formulated as a function of the model depth, training context lengths, and number of hijacking context tokens. A key finding is that a well-trained deeper transformer can achieve higher robustness, which aligns with empirical observations. We show that this improvement arises because deeper layers enable more fine-grained optimization steps, effectively mitigating interference from context hijacking. This is also well supported by our numerical experiments. Our findings provide theoretical insights into the benefits of deeper architectures and contribute to enhancing the understanding of transformer architectures.
Introduction: The Context Hijacking Problem
Large Language Models (LLMs) are famously sensitive to their "context window." While this allows them to learn new tasks on the fly (In-Context Learning, or ICL), it also opens a back door for a subtle type of failure called context hijacking. Unlike a "jailbreak" that uses malicious code or social engineering, context hijacking uses harmless, factually correct information to lead the model astray.
Imagine asking an LLM, "What is Rafael Nadal’s best sport?" Normally, it answers "tennis." However, if you first provide context like "Nadal is not good at playing basketball," the model might suddenly answer "basketball." This is context hijacking. The prepended information is factually true, but it tricks the model into focusing on the wrong subject. While researchers have observed that larger, deeper models seem more resistant to this, a rigorous mathematical explanation has been missing.
Theoretical Framework: Transformers as Optimizers
The authors simplify the problem to make it mathematically tractable, focusing on linear transformers (transformers without the softmax bottleneck) performing a linear classification task. The core of their approach is the "Transformers as Optimizers" lens. Recent theory suggests that the forward pass of a transformer layer is mathematically equivalent to a step of gradient descent. Under this view:
- An $L$-layer transformer implements $L$ steps of gradient descent on the provided context.
- Pre-training determines the "initialization" and the "learning rate" of this internal optimizer.
The authors design a "hijacked" test scenario where context tokens are factually correct (they lie on the correct side of a decision boundary) but are specifically chosen to be very close to the boundary and carry the opposite label of the intended query. They then derive the "test robust accuracy" as a function of model depth ($L$), training context length ($n$), and the number of hijacking tokens ($N$).
Key Insights: Depth as Granularity
The most significant finding is that depth equals granularity. In a shallow model, each layer must take a "giant leap" (a high learning rate) to process context. In a deep model, each layer takes a "tiny step" (a low learning rate).
Because deeper models use smaller internal learning rates across more steps, they are less reactive to individual "noisy" or "hijacking" tokens. It is much harder for a few misleading tokens to knock a 48-layer model off its path than a 12-layer model. This "Heavy Ship" effect allows deeper models to better balance memorized knowledge from pre-training with new in-context information.
Empirical Results: The Depth Dividend
To verify their theory, the authors tested the GPT-2 family (Small, Medium, Large, and XL), ranging from 12 to 48 layers, across four specific tasks (Language, Country, Sports, and City).
Empirical tests showed a clear trend: GPT-2 Small (12 layers) could be hijacked by just 2-3 misleading prepends. In contrast, GPT-2 XL (48 layers) required nearly 20 prepends before its output was disrupted. The authors proved that the classification error is bounded by a term that decreases as depth ($L$) increases, specifically proportional to $(1 - \Theta(\frac{Nd}{nL}))^L$. As $L$ grows, this value shrinks, confirming that depth directly boosts robustness.
Conclusion and Future Work
This work provides the first rigorous theoretical framework explaining why deeper models resist context hijacking. The primary limitation is the focus on linear transformers and linear classification. While these are standard simplifications in deep learning theory, real-world LLMs use non-linearities (like Softmax attention) that might introduce more complex dynamics. Future work could explore whether these findings hold for more complex generative tasks, such as long-form reasoning or code generation, where hijacking might manifest as subtle logical redirection.
Summary updated