How Do LLMs Perform Two-Hop Reasoning in Context?

Tianyu Guo, Hanlin Zhu, Ruiqi Zhang, Jiantao Jiao, Song Mei, Michael I. Jordan, Stuart Russell

TL;DR

While large language models (LLMs) excel at many tasks, they surprisingly collapse to random guessing on simple two-hop reasoning problems when irrelevant "distractor" facts are introduced into the prompt. By reverse-engineering a minimal 3-layer Transformer, this paper reveals that a sharp phase transition during training shifts the model from random guessing to a highly structured, sequential query mechanism that precisely tracks logical chains.

What Problem Does This Paper Address?

Multi-hop reasoning—the ability to chain multiple pieces of information together to draw a conclusion (e.g., A implies B, B implies C, therefore A implies C)—is foundational to intelligence. While modern LLMs handle complex tasks well, their ability to perform this reasoning purely in-context (without relying on memorized weights) is fragile.

Specifically, the authors address why pre-trained LLMs fail at two-hop reasoning when the context contains "distractors"—unrelated premises that look plausible but are logically irrelevant. In these scenarios, base models frequently resort to guessing randomly among all possible endpoints. The paper investigates the mechanistic internal information flow of Transformers to understand how they learn to overcome these distractors and successfully execute multi-hop reasoning.

Methods

To dissect this behavior, the authors take a two-pronged approach:

  1. LLM Fine-Tuning: They evaluate popular open-weight models (Llama2-7b, Qwen2.5-7b, Llama3.1-8B, OLMo-7B) on two-hop tasks with distractors, then fine-tune them using Low-Rank Adaptation (LoRA) to observe how performance changes.
  2. Mechanistic Interpretability on a Proxy Model: To see exactly how the network computes the answer, they train a minimal 3-layer, single-head, attention-only Transformer from scratch on a simplified, symbolic version of the task. They use attention logits visualization and the logit lens technique to track how information is routed across layers during different stages of training.
  3. Mathematical Modeling: They develop a simplified three-parameter dynamical system that strips away the random-guessing phase to prove that the emergence of a sequential query mechanism is what drives the sudden jump in model performance.

Datasets

The study utilizes two primary synthetic datasets:

  • Natural Language Two-Hop Dataset: Built using 6 standardized logical templates spanning family relations, geography, biology, and arithmetic (e.g., "[A] is the father of [B]..."). It includes a target reasoning chain mixed with a varying number ($K$) of distractor chains.
  • Symbolic Two-Hop Dataset: A stripped-down version where predicates are removed and entities are represented by unique single tokens (e.g., [SRC] for source, [BRG] for bridge, [END] for end). This eliminates semantic prior knowledge and isolates the pure structural reasoning mechanism.

Key Insights and Ideas

  • The Minimal Architecture Bound: A 3-layer Transformer is the absolute minimum architecture required to solve in-context two-hop reasoning. Single-layer and two-layer models fail completely, matching theoretical predictions that $k$-hop reasoning requires $\log k$ or linear-in-$k$ layer scaling.
  • The Sharp Phase Transition: As the 3-layer model trains, it doesn't improve linearly. Instead, it undergoes a dramatic phase transition (around 800 training steps in their setup), shifting abruptly from uniform random guessing to near-perfect accuracy.
Figure 1: The loss and the predicted probabilities. Left (a): The cross entropy loss computed at the query token, with the label being the correct target end token ([END-T]) in the preceding premises. Right (b): The predicted probabilities for different tokens throughout training. The [END-NT] line represents probabilities averaged across [End-NT1], [End-NT2], [End-NT3], and [Brg-NT4]. Before approximately 800 steps, the [END-NT] and [END-T] lines remain close, indicating an almost random guessing behavior during this slow learning phase.
  • The Sequential Query Mechanism: Reverse-engineering the post-transition model reveals a beautiful, highly structured circuit across the three layers:
  • Layer 1 (Token Copying): Every child token copies the identity of its paired parent token (e.g., the bridge token learns who its source token is).
  • Layer 2 (Bridge Retrieval): The final query token attends specifically to the target bridge token ([BRG-T]).
  • Layer 3 (End Inference): Armed with the bridge token's history, the query token precisely targets and retrieves the correct final entity ([END-T]).

Key Results

  • Pre-trained Collapse: When presented with $K$ total reasoning chains (1 target + distractors), base pre-trained LLMs perform at roughly $1/K$ accuracy—the mathematical equivalent of random guessing.
  • LoRA Data Efficiency: Fine-tuning base models on just 1,000 examples with a single distractor ($K=2$) completely fixes the issue, boosting accuracy to nearly 100%.
  • Strong Length Generalization: Remarkably, models fine-tuned only on a single distractor ($K=2$) generalize perfectly to much harder contexts containing up to five distractors ($K=5$).
  • Causal Validation: The authors' minimal 3-parameter mathematical model successfully replicates the abrupt phase transition and loss dynamics observed in the full Transformer, validating their hypothesis about how the sequential query mechanism forms.
Comparison between 3-layer transformer and 3-parameter model

Limitations and Future Work

While illuminating, the paper focuses strictly on in-context reasoning rather than in-weight reasoning (where facts are stored directly in the model's parameters). Additionally, the experiments are limited to the simplest multi-hop case: two hops. The authors note that extending this mechanistic analysis to higher-order multi-hop reasoning ($k > 2$) and evaluating how these circuits behave within massive, fully-trained LLMs operating on natural language remain important open directions for future research.

Summary updated

BibTeX
@misc{guo2025how,
  author = {Tianyu Guo and Hanlin Zhu and Ruiqi Zhang and Jiantao Jiao and Song Mei and Michael I. Jordan and Stuart Russell},
  title = {{How Do LLMs Perform Two-Hop Reasoning in Context?}},
  year = {2025},
  eprint = {2502.13913},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2502.13913},
}
Figure