RAG Prompt Engineering: Context Placement & Citation Strategies › The Context Placement Problem
Language AI HandbookAll chapters RAG Prompt Engineering: Context Placement & Citation Strategies › The Context Placement ProblemOne section · Sections in this chapter2 / 11 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
When you retrieve documents for RAG, you face an immediate architectural question: where do these documents go in the prompt? Unlike the fixed instructions of a system prompt or the specific query from a user, retrieved context is variable in length, potentially noisy, and important to the final output. The answer is not as simple as "put them before the question" because large language models do not process all positions in their context window with equal attention. Position measurably affects whether the model uses the information you provide.
Think of the context window as a theater where the seats at the front and back are premium. Information placed in those positions gets the most attention from the audience (the model), while information buried in the middle rows often goes unnoticed despite being present. The challenge for RAG systems is that you typically retrieve between 5 and 20 documents, some of which are only marginally relevant, and you need to pack them into a finite window in a way that maximizes the chance the truly important passages get read carefully.
The placement decision interacts with everything else in your prompt. A strong system instruction placed before the context competes for primacy with the documents themselves. A long list of retrieved passages placed immediately before the user query exploits recency bias but risks truncation if the concatenated context is too long. Understanding the mechanics of position bias, and then making deliberate choices about how to fight or exploit it, is the first major skill of RAG prompt engineering.
Recency Bias and the Lost in the Middle Phenomenon
Language models exhibit position bias when processing long contexts. Studies of large language models reveal a U-shaped performance curve: models excel at using information at the very beginning and very end of their context window, but struggle with information buried in the middle. This "lost in the middle" problem means that your most relevant document, if placed poorly, might be effectively ignored.

The bias stems from how attention mechanisms process long sequences. As we explored in the chapter on attention complexity, attention costs grow quadratically with sequence length, and in practice, models employ various approximations and soft attention decay that favor proximal tokens. Even with techniques like rotary position embeddings, the practical reality is that not all positions in a long context receive equal processing fidelity. The model's internal representations are shaped by what is in the context and where each piece of information appears.
To understand this phenomenon mathematically, consider the attention mechanism as a function of position. In a transformer architecture, the attention weight between a query at position and a key at position typically incorporates positional information through either absolute embeddings or relative position biases. As the distance increases, the effective attention capacity diminishes due to several interacting factors.
The first factor is softmax saturation. The attention scores undergo softmax normalization across all positions. In long sequences, the probability mass dilutes across many tokens, reducing the gradient flow to distant positions. Each token in a short context receives a meaningful share of the attention budget; each token in a long context competes with hundreds of others. The second factor is positional embedding degradation. Whether using sinusoidal encodings or learned embeddings, the distinctiveness of positional representations tends to blur for middle positions in very long contexts, creating an information bottleneck where the model struggles to distinguish "token at position 512" from "token at position 513" in a meaningful way. The third factor is context window pressure. Modern large language models often employ attention approximations, such as sparse attention patterns or sliding window attention, which explicitly limit the receptive field of each token to its local neighborhood.
The U-shaped performance curve emerges from the interaction between these architectural constraints and the sequential processing biases inherent in autoregressive generation. Information at the beginning of the context establishes the foundational semantic framework for the model's processing, while information at the end benefits from recency effects in the attention mechanism and proximity to the query tokens. Middle positions suffer from both diminished attention weights and cumulative context drift, where the model's internal state has been transformed by processing preceding tokens, potentially obscuring the relevance of middle content.
The key insight is that the U-shape is not uniform across all models or all context lengths. Smaller models with shorter training context windows show steeper drops in the middle. Newer models trained with techniques specifically designed to improve long-context utilization show shallower drops, but the effect persists even in the most advanced models. You should design your RAG prompt as if the middle will be partially ignored, because even if your specific model handles it better than average, the cost of that assumption being wrong is a hallucinated or grounded-only-on-edge-documents response.
For RAG systems, this creates a strategic dilemma. Beginning placement guarantees the model sees the context early, but may be overshadowed by the final instruction or query when the model generates its response. End placement, placing context just before the query, takes advantage of recency bias but risks truncation if the context is too long. Interleaved placement, weaving context throughout the conversation history, complicates attribution and is rarely worth the implementation complexity. Most production systems use a form of strategic boundary placement that we will explore in the next section.
Strategic Ordering of Retrieved Documents
When you have multiple retrieved passages, their order matters beyond simple position bias. The goal is to arrange documents so that the most important ones occupy the high-attention positions at the beginning and end of the context block, while accepting that documents in the middle will receive less careful processing.
Consider these strategies and their tradeoffs:
-
Relevance-based ordering (Sandwich pattern): Places the highest-scoring document first and the second-highest document last, with lower-confidence documents in the middle. This exploits both primacy and recency effects, sacrificing the marginal documents to ensure the best ones are processed fully. This is the default recommended strategy for most RAG applications.
-
Chronological ordering: Arranges documents by publication date or version, useful when recency matters more than retrieval score. A query about current regulations or recent events may be better served by a slightly less semantically similar but more recent document than by a highly similar but outdated one.
-
Cluster-based grouping: Groups related documents together, reducing the cognitive load on the model to connect dispersed information. This works particularly well when you have retrieved documents that collectively address different facets of a complex question, and you want the model to process each facet as a coherent unit before moving to the next.
-
Authority-based ordering: For domains with hierarchical information authority (legal, medical, regulatory), placing authoritative sources first and supplementary sources later mirrors how human experts read these materials. A Supreme Court ruling before a law review article is not just polite convention; it signals to the model which source to treat as controlling.



The sandwich pattern deserves a closer look because it is the most widely adopted strategy in production systems. The intuition is simple: if you have documents ranked by relevance , you place document 1 at position 1 (primacy), document 2 at position (recency), and fill the middle with documents 3 through in any order. The two most important documents are guaranteed to land in the high-attention zones, and the less important documents absorb the attention deficit of the middle positions. This is a heuristic rather than a formally optimal strategy, but it is well-motivated by the empirical evidence and easy to implement.
The sandwich pattern assumes your relevance ranking is reliable. If your retrieval system returns documents with scores clustered tightly together, say all between 0.75 and 0.80, then the distinction between first and second place is more noise than signal, and you might prefer a different strategy such as random ordering within a relevance tier, or clustering by subtopic so that the model processes related passages together. The best ordering strategy depends on your retrieval quality as much as on model architecture.