Let's (not) just put things in Context: Test-time Training for Long-context LLMs

Rachit Bansal, Aston Zhang, Rishabh Tiwari, Lovish Madaan, Sai Surya Duvvuri, Fnu Devvrit, David Brandfonbrener, David Alvarez-Melis, Prajjwal Bhargava, Mihir Kale, Samy Jelassi

TL;DR

While modern Large Language Models (LLMs) can "consume" millions of tokens, they often fail to "use" them effectively due to a mathematical phenomenon called score dilution, where relevant signals are drowned out by distractors. This paper introduces query-only test-time-training (qTTT), a method that reallocates inference compute to update the model's query projections for a specific context. This approach leads to massive performance gains (up to 14%) over standard "thinking token" strategies on long-context tasks.

The Challenge of Score Dilution in Long Contexts

We are currently in the era of the "infinite context window," with models claiming to handle millions of tokens. However, researchers have long noted the "lost in the middle" phenomenon: LLMs frequently overlook facts buried in long documents or fail to retrieve function definitions in large codebases.

The authors identify the root cause of this failure as score dilution. In standard self-attention, the attention "budget" is finite. As the context length ($T$) grows, the probability mass assigned to a specific "target" token (the needle) tends to vanish because it is overwhelmed by the sheer number of "distractor" tokens (the haystack).

Crucially, the authors show that popular "inference-time scaling" strategies—like generating "thinking" tokens (Chain-of-Thought)—fail to solve this issue. Because these tokens are generated using the same static, diluted attention mechanism, they cannot magically "find" evidence that the model is already ignoring.

Methodology: Query-Only Test-Time Training (qTTT)

To combat score dilution, the authors propose query-only test-time-training (qTTT). Instead of asking the model to "think" more tokens, they use that same computational budget to briefly train the model on the specific context it just read.

Architecture of query-only Test-Time Training (qTTT)

The process works in two main phases:

  1. Single-Pass KV Caching (Prefill): The model performs a standard prefill of the long context, and the Key (K) and Value (V) tensors are computed and stored in a cache. This expensive step is only done once.
  2. Targeted Gradient Updates (qTTT): The model performs a few steps of gradient descent using a standard next-token prediction loss on small, random spans of the input context.

The "Secret Sauce": Unlike "naive" test-time training which updates all parameters, qTTT only updates the Query ($W_Q$) projection matrices.

  • Why $W_Q$? Updating $W_K$ or $W_V$ would invalidate the KV cache, requiring a massive, expensive re-computation of the entire context. By only updating the queries, the model "reshapes" how it searches through the existing, frozen evidence.
  • Efficiency: The authors prove a "FLOP Equivalence": running a few qTTT steps is computationally equivalent to generating a few thousand "thinking" tokens.

Experimental Setup

The researchers evaluated qTTT using the Qwen3 family of models (1.7B, 4B, and 8B parameters) across several tasks:

  • Synthetic Sandboxes: Two custom tasks designed to stress-test retrieval: Bug Localization in a large code repository and Anomaly Detection in banking transaction logs.
  • Real-world Benchmarks:
    • LongBench-v2: Six categories covering code, dialogue, and multi-document QA.
  • ZeroScrolls: Eight datasets focused on reasoning and summarization over long documents.

Key Insights

  • The Logarithmic Margin Requirement: The authors mathematically prove that to avoid vanishing attention on a target, the "logit gap" between the target and distractors must grow at a rate of $\Omega(\log T)$. Static models cannot achieve this dynamically, but qTTT can.
  • Thinking vs. Training: For long-context tasks, "thinking" is often a waste of FLOPs. If the model's attention is diluted, it is essentially hallucinating based on a weak signal. qTTT uses those FLOPs to "sharpen the lens" before the model starts generating an answer.
  • KV Cache Preservation: By isolating updates to the query matrices, qTTT becomes a "plug-and-play" enhancement for existing inference pipelines without the prohibitive memory costs of full fine-tuning.

Empirical Results

Performance comparison of qTTT vs. In-Context Learning and Thinking
  • Massive Accuracy Boosts: On Qwen3-4B, qTTT improved average scores by 12.6 percentage points on LongBench-v2 and 14.1 points on ZeroScrolls compared to standard in-context learning.
  • Outperforming "Thinking": In a head-to-head FLOP-matched comparison, qTTT consistently beat "thinking" token strategies. For example, on the Long Dialogue History task, Qwen3-4B jumped from 30.8% (with thinking) to 43.6% (with qTTT).
  • Scaling Strength: The benefits of qTTT actually increase as the model size grows. On the Code Repositories task, the 8B model saw a 22-point jump over the base in-context performance.
  • Attention Recovery: Empirical analysis showed that qTTT successfully moved attention mass back onto "needle" tokens that the vanilla model had completely ignored.

Limitations and Future Directions

  • Task Specificity: While qTTT is a powerhouse for retrieval and multi-hop reasoning, the gains are more modest for summarization tasks, where generation quality is the bottleneck rather than information retrieval.
  • Hyperparameter Tuning: While the authors found a stable range for learning rates ($1e-5$ to $1e-6$), the optimal "budget" (how many steps vs. how many tokens) likely varies by task.
  • Combining Strategies: The paper focused on qTTT in isolation. Future work could explore combining qTTT with other scaling laws, such as "Best-of-N" sampling or self-consistency, to see if the gains compound.

Summary updated

BibTeX
@inproceedings{bansal2026let,
  author = {Rachit Bansal and Aston Zhang and Rishabh Tiwari and Lovish Madaan and Sai Surya Duvvuri and Fnu Devvrit and David Brandfonbrener and David Alvarez-Melis and Prajjwal Bhargava and Mihir Kale and Samy Jelassi},
  title = {{Let's (not) just put things in Context: Test-time Training for Long-context LLMs}},
  booktitle = {International Conference on Learning Representations},
  year = {2026},
  url = {https://openreview.net/forum?id=H0bcEdPCoc},
}
Figure