When Does Context Help? Error Dynamics of Contextual Information in Large Language Models
Dingzirui Wang, Xuanliang Zhang, Keyan Xu, Qingfu Zhu, Wanxiang Che, Yang Deng
TL;DR
The paper introduces a unified error-dynamics framework to explain how arbitrary contextual information affects inference in Transformer LLMs. It proves that, in a single-layer setting, the context-induced error equals the sum of the baseline error and a contextual correction, with explicit norm and direction conditions for error reduction and an upper bound tied to context relevance and complementarity. These results extend to multi-context and multi-layer architectures, preserving the core conditions, and are validated across ICL, RAG, and ME with multiple models and datasets. Empirical findings show that misalignment between context and baseline error, as well as insufficient correction magnitude, are the primary failure modes, motivating a principled context-selection strategy that achieves about 0.6% relative improvement. The work provides a practical theory-to-implementation bridge for designing effective context-enhancement methods in LLM inference, including a vector-direction predictor and a norm-based ranking scheme for context selection.
Abstract
Contextual information at inference time, such as demonstrations, retrieved knowledge, or interaction history, can substantially improve large language models (LLMs) without parameter updates, yet its theoretical role remains poorly understood beyond specific settings such as in-context learning (ICL). We present a unified theoretical framework for analyzing the effect of arbitrary contextual information in Transformer-based LLMs. Our analysis characterizes contextual influence through output error dynamics. In a single-layer Transformer, we prove that the context-conditioned error vector decomposes additively into the baseline error vector and a contextual correction vector. This yields necessary geometric conditions for error reduction: the contextual correction must align with the negative baseline error and satisfy a norm constraint. We further show that the contextual correction norm admits an explicit upper bound determined by context-query relevance and complementarity. These results extend to multi-context and multi-layer Transformers. Experiments across ICL, retrieval-augmented generation, and memory evolution validate our theory and motivate a principled context selection strategy that improves performance by $0.6\%$.
