Context Length Challenges › A Worked Example: Context Length Stress Test

Michael BrenndoerferUpdated July 4, 20253 min read
Language AI HandbookAll chapters
Part 18 of 67Long Context
Chapter 126Context Length Challenges › A Worked Example: Context Length Stress Test
Reading section 7 of 11 · A Worked Example: Context Length Stress Test · 3 minSection 7 of 11 · 3 min

Book contents

Section 7 of 11 · 3 min read

Context Length Challenges › A Worked Example: Context Length Stress TestOne section · Sections in this chapter7 / 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

Let's bring together the challenges with a concrete example. We'll simulate how a model's performance degrades as we push beyond training context.

In[31]:
Code
def context_stress_test(trained_length, test_lengths, model_quality=0.95):
    """
    Simulate model performance at different context lengths.

    Combines multiple degradation factors:
    1. Position encoding extrapolation
    2. Attention dilution
    3. Lost-in-middle effect
    4. Memory limitations (simulated by truncation)
    """
    results = []

    for test_len in test_lengths:
        # Factor 1: Position encoding degradation
        if test_len <= trained_length:
            position_factor = 1.0
        else:
            extrapolation_ratio = test_len / trained_length
            # Exponential decay beyond training length
            position_factor = np.exp(-0.5 * (extrapolation_ratio - 1))

        # Factor 2: Attention dilution
        # Relevant info becomes harder to find in longer contexts
        dilution_factor = min(1.0, trained_length / test_len)

        # Factor 3: Lost-in-middle (affects middle 50% of context)
        # More severe at longer lengths
        middle_penalty = 0.1 * (
            1 - trained_length / max(test_len, trained_length)
        )
        middle_factor = 1.0 - middle_penalty

        # Combined performance
        combined = (
            model_quality * position_factor * dilution_factor * middle_factor
        )

        results.append(
            {
                "test_length": test_len,
                "position_factor": position_factor,
                "dilution_factor": dilution_factor,
                "middle_factor": middle_factor,
                "overall_performance": combined,
                "within_training": test_len <= trained_length,
            }
        )

    return results


# Model trained on 4K tokens, tested on various lengths
trained = 4096
test_lens = [512, 1024, 2048, 4096, 8192, 16384, 32768, 65536]
stress_results = context_stress_test(trained, test_lens)
Out[32]:
Console
Context Stress Test (trained on 4096 tokens):

 Test Length   Position   Dilution     Middle    Overall       Status
--------------------------------------------------------------------
         512       1.00       1.00       1.00       0.95           OK
          1K       1.00       1.00       1.00       0.95           OK
          2K       1.00       1.00       1.00       0.95           OK
          4K       1.00       1.00       1.00       0.95           OK
          8K       0.61       0.50       0.95       0.27       EXTRAP
         16K       0.22       0.25       0.93       0.05       EXTRAP
         32K       0.03       0.12       0.91       0.00       EXTRAP
         65K       0.00       0.06       0.91       0.00       EXTRAP
Out[33]:
Visualization
Line plot with shaded components showing performance degradation factors beyond training context length.
Simulated model performance across context lengths. Performance remains stable within training range (left of dashed line), then degrades due to position encoding failures, attention dilution, and lost-in-middle effects.

The stress test visualizes how multiple factors compound to degrade performance beyond training length. Position encoding breaks first and most severely. Attention dilution provides a gentler decay. Together, they explain why naive extrapolation fails and motivate the techniques covered in subsequent chapters.