NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Exploring Length Generalization in Large Language Models

Cem Anil, Yuhuai Wu, Anders Johan Andreassen, Aitor Lewkowycz, Vedant Misra, Vinay Venkatesh Ramasesh, Ambrose Slone, Guy Gur-Ari, Ethan Dyer, Behnam Neyshabur

Large Language Models (LLMs) are notorious for struggling when asked to solve problems longer or more complex than those seen during training. While scaling model size or applying standard finetuning often fails to bridge this "length generalization" gap, recent research demonstrates that combining few-shot prompting with scratchpad reasoning (Chain-of-Thought) allows models to successfully extrapolate to much longer problem instances.

The Challenge of Length Generalization

In many reasoning domains—such as theorem proving, coding, or mathematics—the difficulty of a problem is intrinsically tied to its length. However, real-world datasets are typically "heavy-tailed": they contain an abundance of short, simple examples and very few long, complex ones.

Distribution of proof lengths in the Archive of Formal Proofs

Ideally, a model should learn the underlying algorithm from these short examples and apply it seamlessly to longer ones. Currently, LLMs struggle with this extrapolation. A model might perfectly solve 5-digit addition but fail entirely on 50-digit addition. This suggests that models often rely on surface-level memorization or "shortcut" heuristics rather than learning robust, generalizable logic.

Experimental Methods

To systematically test which training and prompting strategies enable a model to generalize to unseen lengths, researchers evaluated several learning modalities using decoder-only transformer models (specifically LaMDA, ranging from 244M to 64B parameters):

  1. Vanilla Finetuning: Standard supervised learning on the task.
  2. Scratchpad Finetuning: Finetuning the model to output intermediate "thought" steps before providing the final answer.
  3. Few-shot Prompting: Providing a few examples in the context window without updating the model's weights.
  4. Few-shot Scratchpad Prompting: Combining in-context examples with intermediate reasoning steps.

The study also included a detailed failure analysis, investigating whether positional encodings, "distractor" tokens, or end-of-sequence (EOS) predictions were the primary culprits behind generalization failures.

Datasets and Tasks

The study focuses on two synthetic algorithmic tasks designed to isolate the mechanics of length generalization:

  • Parity: Determining if a bitstring has an even or odd number of '1's. This was also tested in a natural language "coin flip" format (e.g., "A coin is heads up. It flips. It flips again. Is it heads or tails?").
  • Variable Assignment: Executing a series of Python-like boolean assignments. This requires the model to track state over multiple steps.
Variable Assignment task showing in-distribution vs. out-of-distribution lengths

By training on short instances (e.g., lengths 3–5) and testing on much longer ones (e.g., length 20), the researchers could precisely measure the models' extrapolation capabilities.

Key Insights and Results

  • Scale is Not a Silver Bullet: Increasing model size from 244M to 64B parameters does almost nothing to improve length generalization in the standard finetuning regime. All models, regardless of size, fail abruptly once they hit lengths just slightly beyond their training data. For example, models finetuned on parity strings of length 40 achieved near 100% accuracy on those lengths but dropped to random chance (~50%) on strings of length 45.
  • Transformers Prefer Parallelism over Sequences: When finetuned on parity, Transformers tend to learn a "counting" shortcut—using self-attention to pool all tokens at once—rather than a sequential bit-by-bit algorithm. This shortcut breaks down as soon as the number of tokens changes.
  • The "In-Context" Advantage: Surprisingly, models can "learn" to generalize via few-shot prompting even when they fail to do so via finetuning. In-context learning appears to allow the model to perform "variable length template matching," mapping the structure of a short example onto a much longer query.
  • Scratchpad Success: Using Few-shot Scratchpad Prompting, the largest LaMDA model (128B) successfully generalized from a 3-step example to correctly solve 20-step problems with high accuracy.
  • Distractors are the Enemy: In scratchpad finetuning, the main reason models fail on longer sequences isn't the sequence length itself, but the accumulation of "distractor" information that the model hasn't learned to ignore at scale.
  • The Hybrid Winner: The combination of Finetuning + Prompting + Scratchpad showed the most robust performance. Finetuning helps the model learn the basic task mechanics, while prompting provides the structural "template" needed for length extrapolation.

Limitations and Future Work

While the parity and variable assignment tasks are synthetic and relatively simple compared to real-world software engineering or mathematical research, they clearly isolate the mechanics of length generalization. The identification of "distractors" as a major failure mode for finetuning is a significant step forward. However, more work is needed to understand why a model's internal weights struggle to learn the same generalization patterns that its in-context mechanism handles with ease.

Future research may explore architectural changes, such as alternative attention mechanisms, that encourage more sequential, algorithmic learning during the initial training phase, ultimately bridging the gap between short-context training and long-context application.

Summary updated

BibTeX
@inproceedings{anil2022exploring,
  author = {Cem Anil and Yuhuai Wu and Anders Johan Andreassen and Aitor Lewkowycz and Vedant Misra and Vinay Venkatesh Ramasesh and Ambrose Slone and Guy Gur-Ari and Ethan Dyer and Behnam Neyshabur},
  title = {{Exploring Length Generalization in Large Language Models}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2022},
  url = {https://openreview.net/forum?id=zSkYVeX7bC4},
}
Figure