GO TO A SECTION

↑ ↓ to navigate · Enter to openExplore all sections →
FDEInterviews logo
arXiv preprint

Scaling Long-Horizon LLM Agent via Context-Folding

Give an agent two tools, branch and return, so it can do token-heavy work in a side thread and keep only the result. Trained with RL, a 32K-context agent matched a 327K one. Mind which baseline.

Breakdown by FDE Interviews · 12 min read · Updated

About the original · 2025
AUTHORS
Sun, Lu, Ling, Liu, Yao, Yang, Chen
FROM
ByteDance Seed, Carnegie Mellon University, Stanford University
PUBLISHED
Oct 2025 · arXiv preprint
CITED BY
149Semantic Scholar · Oct 2026
IN THIS BREAKDOWN · 10 SECTIONS

You can read 22% of this breakdown.

01

The paper in one minute

A ReAct agent appends every thought, tool call and tool output to one growing context. On long tasks, such as deep research with dozens of searches or a coding task that reads many files, that context becomes huge, and models use long contexts poorly and slowly. Weiwei Sun, Jiecao Chen and colleagues at ByteDance Seed, CMU and Stanford propose context folding: let the agent manage its own context with two tools.

branch(description, prompt) starts a sub-task in a separate working context. return(message) ends the branch: all of its intermediate steps are removed from the context, and only the message, a short account of what was found, is appended to the main thread. A search-heavy subtask like "find the publication with these authors" can take thousands of tokens inside its branch and leave a two-line result behind. Because the branch starts from the main thread's prefix, the KV cache can roll back to that point on return, so folding is cheap at inference.

The authors use a plan-execute pattern: the main thread plans and launches branches; branches execute, and cannot create further branches. Prompting alone helps a little, but the agent tends to do heavy work in the main thread or forget to return. So they train it with FoldGRPO, which applies the fold during reinforcement learning rollouts and adds token-level penalties: tokens in the main thread are penalized once it passes half of its 32K budget, a branch judged off-task by GPT-5-nano is penalized, and failed tool calls are penalized.

On BrowseComp-Plus (deep research over a fixed corpus) and SWE-Bench Verified, a Seed-OSS-36B folding agent with a 32K window and up to 10 branches scored 0.620 and 0.580. That beat the same model as a ReAct agent with a 327K context, trained with the same RL, which scored 0.540 and 0.574, and a summarization agent. During training the agent learned to call more tools and create more branches, and its main thread shrank to about 8,000 tokens.

THE IDEA, VISUALLY
BRANCH, WORK, RETURN, FOLD main thread: plan · branch(…) · result · branch(…) · result · answer branch 1: search, open, search… return(“short result”) → folded branch 2: verify authors… return(“short result”) → folded ACTIVE CONTEXT OVER A TASK (ILLUSTRATIVE) ReAct: grows every step folding: drops at each return steps → TRAINED WITH FOLDGRPO GRPO plus token-level penalties: −1 on main-thread tokens once the main thread passes 50% of its 32K budget; −0.2 on a branch a GPT-5-nano judge finds off-task; −1 on failed tool calls. Main thread fell to about 8K tokens while 100K+ were processed. PASS@1, SEED-OSS-36B (TABLE 1) BrowseComp+ SWE-Bench V ReAct, 32K 0.286 0.436 ReAct, 327K 0.478 0.552 ReAct, 327K + RL 0.540 0.574 Summary, 32K×10 + RL 0.527 0.550 Folding, 32K×10 0.420 0.492 Folding + GRPO 0.567 0.564 Folding + FoldGRPO 0.620 0.580 GPT-5, 327K: 0.793 · 0.718 Against the RL-trained 327K ReAct agent, folding gained 8.0 points on BrowseComp-Plus (150 questions) and 0.6 on SWE-Bench Verified (500). The headline +20 and +8.8 compare with the untrained folding agent. SWE-Bench tool calls per task: 72.8 before training, 96.5 after FoldGRPO.
Context folding lets the agent open a branch for a sub-task and collapse it to a short result on return, so the main thread stays small. After FoldGRPO training it beat a 327K-context ReAct agent on BrowseComp-Plus and tied it on SWE-Bench Verified with a 32K window.
Research notes: the problem, approach and results
0.620

BrowseComp-Plus pass@1 (150 questions) after FoldGRPO, against 0.540 for the RL-trained 327K ReAct agent

≈ 8K

tokens in the main thread after training, while the agent processed over 100K tokens per task

32.6

branches used on average for 50 combined questions, after training with at most 10

THE PROBLEM

Long agent tasks fill the context with tool output: search results, files, logs. Performance and efficiency degrade as it grows. Summarizing when the window fills interrupts the agent's reasoning, and multi-agent setups need hand-built workflows.

THE IDEA

Add two tools: branch(description, prompt) starts a sub-task in its own context; return(message) ends it, deletes the branch's steps and leaves the message in the main thread. Train with FoldGRPO, GRPO plus token-level penalties for unfolded work, off-task branches and failed calls.

THE RESULT

With Seed-OSS-36B and a 32K window plus up to 10 branches, the trained agent scored 0.620 on BrowseComp-Plus and 0.580 on SWE-Bench Verified, against 0.540 and 0.574 for an RL-trained ReAct agent with 327K, keeping its main thread near 8K tokens.

02

Why it matters to an FDE

Every long-running agent you deploy hits the context problem: logs, documents, search results and diffs pile up. Context folding is a clean pattern you can apply even without training.

Do heavy reading in a side context. Run "search the knowledge base", "read these five files" or "inspect this log" as a sub-task whose working context is thrown away, keeping only a written result. This is how many coding agents already use sub-agents.

The result message is the memory. Whatever the branch does not write into its return message is gone. Prompt for results that state what was found, what was not found, and where it came from, so the main thread can decide what to do next.

Folding saves money as well as context. Every call re-reads the context. Keeping the main thread small cuts input tokens over a long task several-fold, and the KV prefix stays cacheable.

Training made the difference, and it is not portable. The best results came after reinforcement learning on the target task family. With prompting alone, the folding agent was worse than the long-context agent. Expect a gap if you use the pattern with an off-the-shelf model.

THE FULL BREAKDOWN

Understand the mechanism. Try it on a real problem.

Unlock this breakdown and 99 more papers.

Premium opens every paper in full, plus every answer, concept, course and guide on the site. One payment for six months of access.

6 months · One payment · No auto-renewal

Get Premium · ₹2,000 / $25
  • ◇1 architecture diagram
  • </>1 runnable code experiment
  • ▦2 results tables
  • ?5 interview questions with answers
  1. 03Key takeaways

    The two tools, the plan-execute pattern, the rewards, the results, and misreadings to avoid

    Free account
  2. 04How it works

    The context manager, KV rollback, the FoldGRPO objective, and the evaluation setup

    Premium
  3. 05The numbers that matter

    Gains against each baseline, the training-data caveat, scaling with combined questions

    Premium
  4. 06Try it yourself

    Peak context and input tokens for ReAct, summarization and folding on a 60-call task

    Premium
  5. 07Where it breaks

    The limits of folding and of the paper's evaluation

    Premium
  6. 08Using it on a deployment

    Implementing branch and return for a customer agent without training

    Premium
  7. 09Interview angles

    Five questions this paper prepares you for, with answer hints

    Premium
Back to the paper library →