TL;DR
As large language models are given more test-time compute to "think" through complex problems, their massive reasoning traces quickly become prohibitively expensive to maintain in memory. Prefix Sliding solves this by discarding intermediate reasoning tokens while preserving the initial task instructions and a sliding window of recent thoughts. This simple tweak caps memory costs, stabilizes generation speeds, and delivers a 3x speedup on long-horizon reasoning tasks without sacrificing performance.
What Problem Does This Paper Address?
Recent breakthroughs in AI reasoning rely heavily on "test-time scaling"—allowing a model to generate long chains of thought to solve difficult math, science, or coding problems. However, standard transformers use full attention, meaning every new token must attend to every single previously generated token.
As a result, the computational and memory cost grows linearly with the length of the reasoning trace. For hard tasks requiring tens or hundreds of thousands of tokens of "thinking," full attention becomes unsustainably expensive, leading to out-of-memory errors, sluggish generation speeds, and issues like model distraction or repetitive loops.
Methods
The authors introduce Prefix Sliding, a mechanism that restricts the model's attention to only two critical regions:
- The Prefix: The system instructions and the initial task prompt, which contain the core problem definition and tool availability.
- The Sliding Window: A fixed cache of the most recent tokens (e.g., the last 4,096 tokens) representing the model's active working memory.
Full Attention: [Prefix] -> [Intermediate Reasoning Tokens] -> [Recent Tokens]
Prefix Sliding: [Prefix] -> [xxxxxxxx EVICTED xxxxxxxx] -> [Recent Tokens]
Key Implementation Details:
- Training-Free Deployment: It can be applied out-of-the-box to existing pretrained models using Continue PE (Position Embeddings), which reuses cached Key-Value (KV) representations without needing to recompute positional indices.
- RL Training with Truncated Backpropagation: To scale models even further, they train using Group Relative Policy Optimization (GRPO). To avoid out-of-memory errors during backpropagation on massive traces, they use truncated backpropagation, computing RL losses and updating gradients only on the final window of tokens while passing a slightly larger context ($4\times$ the window size) to ensure accurate log probabilities.
- Hardware Optimization: The authors wrote custom FlashAttention kernels for Nvidia Hopper architectures that utilize intra-tile masking and inter-tile skipping to completely bypass evicted token blocks, achieving true hardware efficiency.
Datasets
- Evaluation Benchmarks: The method was evaluated on standard hard reasoning benchmarks: GPQA (PhD-level science), MATH500, and AIME25 (American Invitational Mathematics Examination). It was also tested on LiveCodeBench (coding) and HealthBench (medical) to analyze edge cases.
- Training Dataset: For reinforcement learning, the authors curated a math dataset from public sources like SkyWork and s1 (which aggregates NuminaMATH, OlympicArena, OmniMath, AGIEval, etc.). They filtered the data based on three criteria: guessability (removing problems solvable without thinking), verifiability (removing subjective questions), and difficulty (removing trivial problems).
Key Insights and Ideas
- The Ephemerality of Scratchpads: The core intuition is that intermediate reasoning tokens quickly lose their value. If a model spends 500 tokens calculating an intermediate algebraic step, it only needs the final simplified equation to move forward; the step-by-step scratchpad work can be safely forgotten.
- Attention Sinks: The initial tokens of a prompt serve as "attention sinks" that absorb excess attention probability mass. Keeping them intact preserves the model's stability and structural awareness of the task.
- Avoiding the Sawtooth Cost: Alternative context-compression methods like "Last $k$" (deleting everything except the last $k$ tokens periodically) or "Summarization" create an irregular, inefficient "sawtooth" computational pattern due to periodic heavy reprocessing. Prefix Sliding maintains a perfectly flat, constant cost per token once the window is full.
Key Results
- 3x Speedup Training-Free: Applied directly to a Qwen3 base model without any extra training, Prefix Sliding matched full-attention accuracy on AIME25 and GPQA while running up to 3x faster.
- Constant Generation Speed: While full attention slows down indefinitely as context grows, Prefix Sliding stabilizes token throughput (e.g., around 5,000 tokens/second in their setup) once the sliding window capacity is reached.
- Outperforming Baselines: Prefix Sliding significantly beat vanilla sliding windows (which fail because they forget the initial prompt instructions) and outperformed summarization techniques, which models often ignore or waste time generating.
- Scaling to 100k+ Tokens: When combined with RL training, Prefix Sliding allowed models to successfully generate and learn from reasoning traces exceeding 100,000 tokens within reasonable memory limits.
Limitations and Future Work
- Information Loss in Coding: On LiveCodeBench, performance dropped with smaller window sizes. In coding tasks, models often write a function definition, write thousands of tokens of comments/reasoning, and then try to call the function. If the original function definition slides out of the window, the model fails.
- No Benefit for Short Tasks: For easy tasks that require fewer tokens than the window size (e.g., HealthBench), the model never fills the window, meaning Prefix Sliding behaves identically to full attention and yields no speedup.
- Agentic Context Flooding: In multi-turn agent tasks where a model reads a massive external file or webpage, the incoming text can completely flood the sliding window, evicting previous reasoning steps. The authors suggest future work should focus on teaching models to read external data step-by-step (e.g., using command-line tools like
headinstead ofcat).
Summary updated