Context Length Challenges › Evaluation Challenges
Language AI HandbookAll chapters Context Length Challenges › Evaluation ChallengesOne section · Sections in this chapter6 / 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
How do we know if a model uses long context? Perplexity on long documents tells us the model predicts well overall, but not whether it's using information from 10K tokens ago. This evaluation gap has led to the development of specialized benchmarks.
The Needle in a Haystack Test
The most intuitive test: hide a specific fact early in a long document, then ask about it at the end. If the model can retrieve the fact, it's using long context.
def create_needle_haystack_prompt(
needle_position, total_length, context_tokens=100
):
"""
Create a needle-in-a-haystack test prompt.
The 'needle' is a specific fact. The 'haystack' is filler text.
We place the needle at a specific position and ask about it at the end.
"""
# The needle: a specific retrievable fact
needle = "The secret code is ALPHA-7392."
# Filler text (in practice, this would be coherent text)
filler_per_segment = "This is background text that provides context but is not relevant to the specific question. "
# Build the document
segments_before = needle_position // context_tokens
segments_after = (
total_length - needle_position - len(needle.split())
) // context_tokens
document = filler_per_segment * segments_before
document += needle + " "
document += filler_per_segment * segments_after
question = "\nQuestion: What is the secret code mentioned in the document?"
return {
"document": document[
: total_length * 5
], # Approximate token to char ratio
"question": question,
"answer": "ALPHA-7392",
"needle_position": needle_position,
"total_length": total_length,
}
# Test at different positions
positions = [100, 1000, 5000, 10000, 50000]
total_length = 100000
test_cases = [
create_needle_haystack_prompt(pos, total_length) for pos in positions
]Needle-in-a-Haystack Test Cases:
Needle Position % Into Document Context Distance
--------------------------------------------------------
100 0.1% 99,900
1,000 1.0% 99,000
5,000 5.0% 95,000
10,000 10.0% 90,000
50,000 50.0% 50,000Real benchmarks like RULER and LongBench extend this idea with multiple needles, multi-hop reasoning, and varying distractor difficulty. Performance typically degrades as the needle moves earlier in the document, revealing the model's effective context utilization.
Multi-Hop Reasoning Over Long Context
A harder test: require the model to combine information from multiple distant locations.
def multi_hop_test_design(num_hops, positions, total_length):
"""
Design a multi-hop reasoning test over long context.
Each hop provides a clue that points to the next location.
The model must chain together all clues to answer.
"""
clues = [
("Alice lives in", "France"),
("The capital of France is", "Paris"),
("The main airport in Paris is", "Charles de Gaulle"),
("The airline code for Charles de Gaulle is", "CDG"),
]
test = {
"num_hops": num_hops,
"positions": positions[:num_hops],
"total_length": total_length,
"clues": clues[:num_hops],
"question": "What is the airline code for the main airport in the capital of the country where Alice lives?",
"answer": clues[num_hops - 1][1] if num_hops <= len(clues) else "N/A",
}
# Compute the total "distance" the model must span
if len(positions) >= 2:
test["max_span"] = max(positions) - min(positions)
else:
test["max_span"] = 0
return test
# Different multi-hop configurations
configs = [
(2, [1000, 50000]),
(3, [1000, 25000, 50000]),
(4, [1000, 20000, 40000, 60000]),
]
multi_hop_tests = [multi_hop_test_design(n, pos, 100000) for n, pos in configs]Multi-Hop Reasoning Test Configurations:
Hops Positions Max Span
------------------------------------------------------------
2 1K, 50K 49,000
3 1K, 25K, 50K 49,000
4 1K, 20K, 40K, 60K 59,000
Example question: What is the airline code for the main airport in the capital of the country where Alice lives?
Required reasoning chain:
Hop 1: Alice lives in → France
Hop 2: The capital of France is → Paris
Hop 3: The main airport in Paris is → Charles de Gaulle
Hop 4: The airline code for Charles de Gaulle is → CDGMulti-hop tests reveal whether models can maintain and combine information across distances. Many models that pass single-needle tests fail multi-hop tests, suggesting they struggle with reasoning over long context rather than just retrieval.
The Lost in the Middle Phenomenon
A striking empirical finding: models often perform best when relevant information is at the beginning or end of the context, and worst when it's in the middle. This "lost in the middle" effect suggests attention doesn't uniformly cover the context. Liu et al. (2023) documented this phenomenon systematically across multiple models and tasks. The effect is not a quirk of a particular architecture but a consistent pattern, suggesting it reflects something fundamental about how transformers process long sequences.
The primacy effect (strong performance at the start of context) likely reflects the model's training dynamics: early tokens in a document are referenced by every subsequent token's attention computation, giving them more gradient signal and making them more salient by the end of training. The recency effect (strong performance at the end) reflects a similar mechanism: late tokens appear more recently relative to the model's generation position, so they receive stronger attention weight in causal (decoder-only) models. The tokens in the middle receive neither of these advantages.

The "lost in the middle" effect has significant practical implications. When building RAG systems or structuring long prompts, placing critical information at the beginning or end improves retrieval. Understanding this phenomenon is essential for effective use of long-context models. It also motivates architectural improvements like position-aware attention biases, which can help models attend more uniformly across the full context rather than clustering at the boundaries. In practice, if you must include many retrieved documents in a prompt and want the model to use them all effectively, ordering them with the most critical documents at the start or end rather than scattered through the middle can noticeably improve output quality.