Skip to content

How Do Large Language Models Perform Two-Hop Reasoning in Context?How Do LLMs Perform Two-Hop Reasoning in Context?

arXiv· arXiv:2502.13913· Tianyu Guo, Hanlin Zhu, Ruiqi Zhang, Jiantao Jiao, Song Mei, Michael I. Jordan, Stuart RussellDepartment of Statistics, UC Berkeley; Department of EECS, UC BerkeleyIndexed by this site · 2026-10-14· Originally publishedAlignment

Quick read

Interpretability

Based on the paper PDF (AI-generated); refer to the original

The authors report that Llama2-7b, with distractors, gives endpoint probabilities close to 1/K; after fine-tuning on K=2, the target probability at K=5 is 0.86.

Questions
Two-hop reasoning requires deriving the endpoint from two adjacent facts. The authors find that when an unrelated reasoning chain is added to the same context, pretrained LLMs tend to distribute probability among various plausible endpoints rather than locking onto the target chain.
Methods
The authors synthesize two-hop data with distractors, fine-tune LLMs on prompts with only one distractor chain, and train a three-layer Transformer from scratch, using attention logits and logit lens to reverse-engineer information flow, then use a three-parameter model to contrast training dynamics.
Experiments and results
The four pretrained models spread probability across multiple endpoints as the number of chains increases. After fine-tuning only on K=2, Llama3.1-8B reaches a target endpoint probability of 1.00 at K=5, while OLMo-7B reaches 0.50. The three-layer model shifts from uniform guessing to sequential queries after about 800 steps.
Limitations and future work
The conclusion lists extension to multi-hop reasoning and more LLMs as future work. Experiments include four 7B/8B models, six templates, and over 50,000 contexts; fine-tuning is only at K=2; the symbolic task fixes five chains. The number of held-out test set items is not reported.
Experimental setupLlama2-7B、Qwen2.5-7B et al. · synthetic two-hop reasoning with distractors、symbolic two-hop reasoning · next-token probability of the first token of [END-T]、next-token probability of the first token of [END-NT] et al.
Evaluated models
Llama2-7BQwen2.5-7BLlama3.1-8BOLMo-7B3-layer Transformer (trained from scratch)
Benchmarks
synthetic two-hop reasoning with distractorssymbolic two-hop reasoning
Metrics
next-token probability of the first token of [END-T]next-token probability of the first token of [END-NT]cross-entropy loss at the query token
AI summaryPretrained large language models fail on simple two-hop reasoning tasks when distractors are present, often guessing randomly among all seemingly plausible answers; after minimal fine-tuning, models can achieve near-perfect accuracy with strong length generalization. The authors train a 3-layer Transformer from scratch on a synthetic two-hop reasoning task and reverse-engineer its internal information flow, finding a clear phase transition in attention logits during training, shifting from initial random guessing to a structured sequential-query mechanism. Under this mechanism, the model first retrieves the preceding and bridging concepts in early layers, then uses them to infer the final answer. The authors also show this dynamic can be characterized by a pure attention network with only three parameters.

Pretrained large language models fail on simple two-hop reasoning tasks when distractors are present, often guessing randomly among all seemingly plausible answers; after minimal fine-tuning, models can achieve near-perfect accuracy with strong length generalization. The authors train a 3-layer Transformer from scratch on a synthetic two-hop reasoning task and reverse-engineer its internal information flow, finding a clear phase transition in attention logits during training, shifting from initial random guessing to a structured sequential-query mechanism. Under this mechanism, the model first retrieves the preceding and bridging concepts in early layers, then uses them to infer the final answer. The authors also show this dynamic can be characterized by a pure attention network with only three parameters.

Paper analysis

6 questions; open one to read the full answer

  1. What problem does this paper attempt to solve?

    With distractors, the base model spreads endpoint probability across chains; after fine-tuning, it concentrates; the authors use a three-layer network to describe sequential queries.

    When multiple two-hop chains are placed in the same context, pretrained LLMs often spread the target endpoint's probability across endpoints; after a small amount of fine-tuning the probability concentrates, and the paper uses a three-layer Transformer to reverse-engineer this transition.

    • Setting: The authors define a premise as a fact connecting two entities with one relation. Two-hop connects a source entity [SRC] to a bridge entity [BRG] and then to an endpoint [END]. The authors consider this the simplest tractable form of multi-hop reasoning, critical for integrating information across steps, but whether pretrained LLMs can do it remains unclear.
    • Gap: The authors say prior mechanistic work mainly concerns in-weight multi-hop, retrieving and combining stored facts from weights. This paper examines in-context: facts must be extracted from the current context, not relying on memorized knowledge.
    • Observation: The distractor setting places multiple chains in the same context, with only the target chain leading to the correct inference; other entities are denoted [SRC-NT], [BRG-NT], [END-NT]. The authors report high [END-T] probability without distractors, and close to 1/K (K being the number of reasoning chains) with distractors, with a slight bias toward the target.
    • Approach: Synthetic distractor data generated from 6 templates; fine-tuning on 1,000 prompts with one target chain plus one distractor chain; then training a three-layer Transformer from scratch and analyzing the attention phase transition from random guessing to sequential queries, fitting the dynamics with a three-parameter network.
  2. What related research exists?

    Related work covers multi-hop, in-context induction heads, and small-model mechanisms; the authors place this paper in in-context two-hop and claim three layers are sufficient.

    In Section 1.1, the authors divide related work into three lines: multi-hop reasoning, in-context learning and induction heads, and LLM interpretability.

    Multi-Hop Reasoning

    • Evaluation and potential pathways: Zhong et al. [2023] is treated by the authors as a multi-hop benchmark for evaluating LLMs. Yang et al. [2024b] is cited as: under certain prompts, LLMs can follow latent reasoning paths.
    • Attention to intermediate steps: Biran et al. [2024], Wang et al. [2024a], and Feng et al. [2024] are grouped as mechanistic studies of how models attend to intermediate steps sequentially to perform multi-hop. The authors' comparison is positioned in this category: prior work is mainly in-weight, whereas this paper is in-context, not relying on memorized knowledge.

    In-Context Learning and Induction Heads

    • ICL: Brown et al. [2020] define ICL as predicting the label of a new input from a few examples without updating weights. The authors also list regression, classification, Bayesian inference, model selection, and sequential decision-making as in-context tasks, and say two-hop is more complex than standard regression-style ICL.
    • Induction heads: Elhage et al. [2021] and Olsson et al. [2022] describe induction heads as attention patterns that connect repeated tokens, thereby copying and completing sequences. Bietti, Nichani, Wang, Chen, and others analyze induction heads in small Transformers. The authors use Sanford et al. [2024a]'s conclusion for comparison: two layers suffice for induction heads.
    • Number of layers: The authors say two-hop requires combining two separate relational facts, and in this paper a two-layer Transformer cannot solve the task; the minimum is three layers. Sanford et al. [2024b] is cited as a theoretical result: log k layers are both necessary and sufficient for k-hop in context. The paper does not treat this as a criticism of itself.

    Interpretability and Small Systems

    • Mechanisms not directly comparable: The authors list grokking (Nanda et al. [2023]), function vectors (Todd et al. [2023]), circuit discovery, binding ID (Feng and Steinhardt [2023]), and association-storage (Meng et al. [2022], Geva et al. [2023]), and write that this paper is not directly comparable to them.
    • Small surrogate models: Olsson et al. [2022] reproduce induction heads in two-layer networks; Bietti et al. [2024] discuss the relative speed of bigram memorization and induction head emergence; Zhu et al. [2024] analyze the reversal curse in bilinear models and one-layer Transformers; Reddy [2023] characterizes the sudden emergence of induction heads with a two-parameter toy model; Guo et al. [2024] distinguish active from dormant attention heads on Bigram-Backcopy. The authors say the approach is similar, but two-hop is more complex than Bigram-Backcopy or copy-paste.

    Baselines and Data

    • Baseline methods: Natural language experiments use each model's pre-fine-tuning base as the control, with the fine-tuning setting written as Fine-tuned on K=2 in the result tables. Symbolic experiments use one- and two-layer Transformers as controls against three layers. Benchmarks are all synthetic: 6 templates, over 50,000 distractor contexts, and a symbolic task with 5 chains per context.

    The authors position this paper as an in-context rather than in-weight mechanistic study of two-hop, and say the symbolic task requires three layers; two are insufficient.

  3. How does the paper address this problem?

    Fine-tuning LLMs on two-hop with distractors, and using a three-layer Transformer to reverse-engineer attention from uniform guessing to sequential queries.

    The authors turn in-context two-hop into a fill-in-the-blank synthetic task: first, see which endpoint pretrained LLMs assign probability to, then reverse-engineer the layer-by-layer information-taking order of a three-layer attention-only Transformer on the symbolic version.

    Task Setup

    • Chain components: A valid two-hop chain has exactly two premises. [SRC] connects to [BRG], and [BRG] connects to [END]. With distractors, multiple chains share a context, and only the target chain's conclusion is queried; distractor chains only have their two premises. Target and non-target premises are randomly permuted.
    • Symbolic version: Predicates are hidden, and each entity is a distinct token. Premises are written as [SRC][BRG] and [BRG][END], and conclusions as [SRC][END]. The authors call the former in a pair the parent and the latter the child.
    • Fixed-length context: Each context samples 5 chains, 1 target and 4 distractors. Within a chain, source-to-bridge always precedes bridge-to-end, and chains are shuffled. The final query is [SRC-T], and the next token to predict is [END-T]. In the training configuration, sequence length N=23 (Table 6).

    Model Selection and Reverse-Engineering Tools

    • Depth: After comparing different depths, the authors claim that a three-layer Transformer with one attention head per layer is the minimal structure that exhibits both random guessing and structured learning. One and two layers still cannot consistently solve the symbolic task after long training. After removing the MLP, the authors claim the dynamics and phase transition remain, so subsequent analysis focuses on three-layer attention-only.
    • Training: Adam, learning rate 0.0003, batch 512, 10,000 steps, pre-layer norm, positional embeddings (Table 6). LLM fine-tuning uses LoRA, with configuration in the experiments section.
    • Two readings: Attention logits are pre-softmax scalars, used to see the strength with which one token takes information from another; after softmax, the authors call them attention weights. Logit lens projects hidden states through the final READOUT (layer norm then linear map to vocabulary) to the next token's logit. The authors write the query's probability as a weighted sum over preceding tokens, with weights being attention weights and terms being that token's value passed through READOUT.

    SOFTMAX(READOUT(hquery))≈SOFTMAX(∑i wi·READOUT(VAL(i)))

    Here w_i is the query's attention weight on the i-th preceding token.

    Information Flow in the Slow Learning Phase

    The authors take the attention map at training step 800 (Figure 2).

    • Layer 1: Checkerboard. Parents attend to children in earlier premises, and vice versa. [END] takes information from [SRC] and [BRG].
    • Layer 2: Parents attend uniformly to all preceding tokens; children also attend uniformly to all preceding parents, with these logits approximately equal in magnitude. The authors claim there is no distinction between relevant and irrelevant tokens at this point.
    • Layer 3: The query attends to all preceding children, including [BRG] in [SRC] to [BRG] and [END] in [BRG] to [END].

    The authors use logit lens to explain why probability falls on each [END] rather than all children (Figure 3 shows one distractor as an illustration; values are in Appendix B). Each [END] is positive at its own position and negative on preceding parents. The query's readout is written as an average over children:

    READOUT(hquery)≈1/(|CHILD|)∑i∈CHILD(ei−∑j∈PARENTaj ej)

    a_j>0, e_i is a vector with 1 at position i. Negative terms fall on [SRC] and [BRG], and the remaining large terms are the [END]s; after softmax, the authors claim other terms are negligible, and each [END] gets near-equal probability because the positive terms are similar in magnitude.

    Structured Phase and Three-Parameter Model

    • Three layers at step 10000 (Figure 4): In layer 1, the authors claim large terms only connect paired parents and children within each premise, with children copying parents. Layer 2 is mainly the query attending to [BRG-T]; since [BRG-T] has copied [SRC-T], the query holds both. Layer 3 has the query attending to [END-T]: both share information from [BRG-T], [END-T] gets it directly in layer 1, and the query gets it indirectly through layer 2. The authors claim the next token is therefore biased toward [END-T].
    • Three parameters: α, β, γ denote the progress of layer 1 copying, layer 2 query attending to [BRG-T], and layer 3 query attending to [END-T], respectively. Attention uses an approximate softmax, defined as exp(u)/(exp(u)+M), i.e., one logit is u and the remaining M are 0. The copying layer weight is approximately this function applied to (α, N), with the authors claiming N comes from averaging over 2N positions; the second parameter for the latter two layers is written as 2N. As α, β, γ go to infinity, the loss goes to 0, corresponding to after 10,000 steps; when all three are 0, the loss is close to uniform guessing over the vocabulary, corresponding to the untrained model. In simulations, the value readout of [END-T] is fixed as ξ times a one-hot vector, with ξ=30.
    • Contrast hypothesis: This model removes "guessing on each [END]" and keeps only sequential queries. Hypothesis C.1: The random guessing mechanism causes slow learning from 0–800 steps. Hypothesis C.2: Sequential queries cause the sudden phase transition from 800–10000 steps. Optimization is gradient descent with learning rate 0.1.
  4. What experiments did the paper conduct?

    All four models have higher target probabilities at K=5 than their respective base models, and OLMo-7B remains at 0.50 after fine-tuning.

    Experimental Setup

    • Tested LLMs: Llama2-7b (written as Llama2-7B in Table 2, Llama2-7B-Base in the conclusions), Qwen2.5-7B (written as Qwen2.5-7b in the Table 3 title), OLMo-7B, Llama3.1-8B (written as Llama3-8b in the Table 4 row names). For the symbolic experiments, one-, two-, and three-layer Transformers were additionally trained; the main analysis used a three-layer attention-only model with the MLP removed. Template design used Deepseek-R1.
    • Natural language data: 6 templates covering family, geography, biology, and arithmetic relations; location and biological categories use fictional names, which the authors say avoids prior-knowledge interference. Language names and person names are common names. The authors constructed over 50,000 contexts. Each sample draws one template, with exactly one target chain; distractor chains keep only two premises, the target conclusion is placed at the end, and premises are randomly ordered.
    • Fine-tuning: The main text says 1,000 prompts, each with one target chain plus one distractor chain; all result tables are labeled Fine-tuned on K=2. Table 2's Data Processing lists Distractor Numbers as 2. LoRA: r=16, alpha=32, dropout=0.05, modules q/k/v/o_proj, fp16, AdamW, learning rate 2×10^−4, cosine, warmup ratio 0.03, weight decay 0.01, 3 epochs, batch 16, max length 128. The appendix says the four models completed training on four A100s within one hour.
    • Metrics and protocol: On a held-out test set, next-token probabilities at the query position for the first token of the target endpoint entity [END-T] and the first token of non-target endpoints [END-NT]. With multiple distractors, [END-NT] is first averaged within a context, then averaged across contexts; parentheses give standard errors. K=1 means no distractors. The paper does not report the number of test-set examples and does not use LLM evaluation or human judgment. The abstract says near-perfect accuracy; the tables report probabilities.
    • Symbolic training: 5 chains per context (4 distractors), sequence length 23, Adam, learning rate 0.0003, β1=0.9, β2=0.99, weight decay 0.01, batch 512, 10,000 steps. The authors say results are consistent across different random seeds, without giving the number of seeds.

    Main Results

    The table below shows the average probability of the first token of [END-T] (standard errors in parentheses). The fine-tuned columns are all measured after fine-tuning on K=2. K is the number of reasoning chains in the context. Llama2-7b is from Table 1, Qwen2.5-7B from Table 3, Llama3.1-8B from Table 4, OLMo-7B from Table 5.

    Swipe horizontally to read the table

    ModelBase K=1Base K=5Fine-tuned on K=2, K=5
    Llama2-7b0.82 (0.01)0.20 (0.01)0.86 (0.01)
    Qwen2.5-7B0.80 (0.01)0.24 (0.01)0.99 (0.00)
    Llama3.1-8B0.90 (0.00)0.27 (0.01)1.00 (0.00)
    OLMo-7B0.72 (0.01)0.14 (0.00)0.50 (0.01)
    • Main-text interpretation for Llama2-7b: The authors say that before fine-tuning, [END-T] is selected only without distractors; with distractors, the model is close to guessing randomly over the endpoint set, with a slight bias toward the target. In Table 1, the average [END-NT] probability at K=5 is 0.11 (0.00); this is the average per non-target endpoint, not the sum. At K=2, the base model gives [END-T] 0.41 (0.01) and [END-NT] 0.34 (0.01).
    • Generalization claims and table definitions: Fine-tuning uses only one distractor chain. The authors say the model still handles more distractors, and the main text mentions up to five distractor chains. Table 1 defines K as the total number of reasoning chains, K=1 means no distractors, and the maximum in the table is K=5. For fine-tuned Llama2-7b, [END-T] at K=3 and K=4 is 0.92 (0.01) and 0.89 (0.01).
    • The appendix does not repeat the same curve: For fine-tuned Llama3.1-8B, [END-T] is 1.00 (0.00) for K=1 through K=5, and [END-NT] is 0.00 (0.00) throughout (Table 4). For fine-tuned Qwen2.5-7B, [END-NT] at K=3 is 0.01 (0.00) (Table 3). For fine-tuned OLMo-7B, K=3 is 0.66 (0.01) and K=4 is 0.57 (0.01) (Table 5). The main text does not discuss OLMo's decline. The base models also do not all track 1/K: Qwen2.5-7B has [END-T] of 0.26 (0.01) at both K=3 and K=4.

    Phase Transition on the Symbolic Task

    • What was measured: A three-layer single-head Transformer on symbolic two-hop with 4 distractors, cross-entropy at the query position, and prediction probabilities for each token type (Figure 1).
    • Results: The authors say a phase transition occurs around step 800. Before that, the probability over all possible endpoint tokens is near uniform, about 0.2, with a slight bias toward [END-T]. The figure caption says [END-NT] is the average over [End-NT1], [End-NT2], [End-NT3], and [Brg-NT4]; the original text indeed writes [Brg-NT4]. One- and two-layer models still cannot solve the task consistently after long training. With the MLP removed, the authors say the dynamics and phase transition are not impaired.
    • Authors' interpretation: Before 800 steps is slow learning, close to random guessing; after the phase transition, the model enters a structured stage capable of correct inference.

    Attention Logits and Logit Lens

    • Slow stage: Figure 2 takes step 800. The authors describe layer 1 as a checkerboard, layer 2 as child tokens having roughly equal logit magnitudes for preceding parents, and layer 3 as the query attending to all preceding children.
    • Why not uniform over all children: Figure 3 uses an example with one distractor; Figure 5 is the logit lens of layer-3 values averaged over 256 sequences at step 800. The authors say each token is positive for itself, [END] is negative for [BRG], and [BRG] is approximately 0 for [END]. After negative and positive terms cancel, the larger terms remain on the [END] tokens.
    • Structured stage: Figure 4 takes step 10000. The authors say layer 1 has children strongly attending to their paired parent, layer 2 has the query strongly attending to [BRG-T], and layer 3 has the query strongly attending to [END-T], thereby biasing the prediction toward the target endpoint.

    Three-Parameter Model Comparison

    • What was measured: A three-parameter system retaining only sequential queries and removing guessing over endpoints, compared against the three-layer Transformer's loss (Figure 6, Appendix C). It is used to test hypothesis C.1 (random guessing causes slow learning over steps 0–800) and C.2 (sequential queries cause the phase transition over steps 800–10000).
    • Results: The figure caption says the three-layer Transformer shows slow learning over steps 0–400 and a sudden phase transition around step 800; this does not exactly match Section 3.1's boundary description of "slow stage before about step 800." The three-parameter model's loss is flat for the first 1000 steps, then transitions suddenly around step 1000, with α, β, and γ changing together.
    • Authors' interpretation: The authors say that because removing random guessing eliminates slow learning while the sudden phase transition remains, the simulation supports C.1 and C.2.

    Other Ablations and Analyses

    • Removing the MLP: The authors say it does not change the dynamics or phase transition of the symbolic task; the paper gives no separate loss (Section 3.1).
    • One and two layers: The authors say they cannot solve the task consistently; the paper does not report their loss or probabilities.
    • Example 1 (titled 2 distractors): The base model gives the three endpoint names 0.33, 0.32, 0.31; after fine-tuning, the text writes 0.97, 0.01, 001, and the original text indeed reads 001.
    • Llama2-7b base model at K=3 and K=4 has [END-T] of 0.31 (0.01) and 0.25 (0.01), and [END-NT] of 0.20 (0.01) and 0.14 (0.00) (Table 1).
    • OLMo-7B base model at K=1 has [END-T] of 0.72 (0.01), the lowest K=1 value across the four tables (Table 5).
    • All four models share the LoRA configuration in Table 2; the paper does not report the number of fine-tuning steps for the LLMs, nor the number of held-out examples.
  5. What are some points that can be further explored?

    The authors list extension to multi-hop and more LLMs as future work only in the conclusions, without a separate limitations section.

    Limitations and Future Directions Noted by the Authors

    The paper has no dedicated Limitations section. Section 4 Conclusions explicitly states one future direction: extending this work to multi-hop reasoning and more LLMs would be important future work. Beyond that, the authors do not frame any experimental phenomenon as a limitation.

    Experimental Coverage

    • Natural language result tables cover Llama2-7b, Qwen2.5-7B, OLMo-7B, and Llama3.1-8B, all with the same LoRA configuration in Table 2; result tables are all labeled as fine-tuned on K=2, evaluated from K=1 to K=5 (Tables 1, 3, 4, 5, Appendix A).
    • There are 6 templates, and the authors constructed over 50,000 contexts; fine-tuning uses 1,000 prompts. Evaluation is on a held-out test set, whose size the paper does not report.
    • The symbolic task is fixed at 5 chains per context, sequence length 23, 10,000 steps, batch 512; after comparing one-, two-, and three-layer models, the authors say the three-layer single-head is the minimal structure, and perform the reverse-engineering on a three-layer attention-only model with the MLP removed (Section 3, Table 6).
    • The reported quantities are next-token probabilities for the first token of the endpoint entity and cross-entropy at the query position. The paper does not report a separate accuracy metric, nor inter-annotator or human agreement.
    • Attention and logit lens analyses target steps 800 and 10000 of the three-layer model; logit lens values are averaged over 256 sequences. The three-parameter model uses gradient descent with learning rate 0.1 and ξ fixed at 30 (Appendices B, C).
  6. Summarize the main content of the paper

    The authors say Llama2-7b, with distractors, assigns endpoint probabilities close to proportional to the number of chains; after the phase transition, the three-layer network sequentially attends to the bridge and endpoint.

    The authors study how Transformer language models perform two-hop reasoning in context, focusing on when there are also irrelevant chains in the same context.

    • Problem: A two-hop chain connects [SRC] via [BRG] to [END]. When distractor chains are mixed with the target chain, the model must select the target endpoint, not the endpoints of other chains. The authors view this as the simplest in-context form of multi-hop, as opposed to in-weight multi-hop where remembered facts are retrieved from weights.
    • Approach: Test pretrained models on 6 templates and over 50,000 synthetic contexts, and fine-tune with LoRA on 1,000 prompts of "one target chain plus one distractor chain." The symbolic version collapses entities into single tokens, with 5 chains per context, trains a three-layer single-head Transformer from scratch, uses attention logits and logit lens to see which tokens each layer takes, and then uses a system with only α, β, γ to contrast losses.
    • LLM numbers: Llama2-7b base has [END-T] probability 0.82 (0.01) at K=1 and 0.20 (0.01) at K=5 (Table 1). The same model fine-tuned only at K=2 gives 0.86 (0.01) at K=5. In the appendix, also fine-tuned only at K=2, Llama3.1-8B gives 1.00 (0.00) at K=5, Qwen2.5-7B gives 0.99 (0.00), and OLMo-7B gives 0.50 (0.01) (Tables 3–5). The main text's about 1/K claim is for Llama2-7b; Qwen2.5-7B base gives 0.26 (0.01) at both K=3 and K=4.
    • Small model: The authors claim that before about 800 steps, the three-layer model has about 0.2 probability for each endpoint token; layer 3 queries attend to all children, but positive and negative terms cancel, leaving probability on each [END]. At step 10000, layer 1 copies parents within premises, layer 2 queries attend to [BRG-T], and layer 3 queries attend to [END-T]. One and two layers cannot solve consistently. The authors claim the three-parameter model's loss is flat for the first 1000 steps and undergoes a phase transition around 1000 steps, so they attribute slow learning to random guessing and the phase transition to sequential queries.
    • Authors' conclusion: The abstract and conclusion state that very little fine-tuning suffices to switch to a mechanism that selects the target endpoint, with generalization to more distractors; the appendix's fine-tuning configuration is 3 epochs. The authors list extension to multi-hop and more LLMs as future work.
Read originalarxiv.org