cs.LGOct 1, 2026
SaveTrained Agentic Context Management
Abstract
We study long context language models. Instead of training long context natively, or designing a long context harness, we train a model over the simplest possible harness: a tool to call itself with any specified prompt and a tool to read tokens in a range from the input context. We finetune Qwen3.6-35B-A3B on a diverse synthetic dataset using this harness. With only 8,000 tokens of context, our small model is as strong as GPT-5.4 with 1M tokens of context on the OOLONG-synth benchmark when document length exceeds 40K tokens.
Figures & tables
Figure 1: Results on RULER and OOLONG-synth. Our finetune runs in our harness with an 8K token per-agent context limit. GPT-5.4 (medium reasoning) and base Qwen3.6-35B-A3B (thinking disabled) receive the full document with the prompt (see Section 4 and Appendix A for more details). Base Qwen and GPT-5.4 are served with 64K and 1M token context limits, respectively. The OOLONG random baseline follows Bertsch et al. (2025, §2.3) .
Figure 2: Tree reduce , for order-independent tasks (aggregation, retrieval). Non-leaf nodes delegate half their range to subagents. Leaves read one chunk and compute a leaf op which is aggregated up the tree.
Figure 3: Sequential fold , for order-dependent tasks (variable tracking, running state). Each agent reads the next slice, updates the accumulator, and delegates the rest of the document to the next agent.
Figure 4: Boundary handling. A leaf/fold agent owns the data that starts in its range. It reads 200 additional tokens at a time until the last item in its range is not cut off (additional calls not pictured in Figures 2 and 3 , for brevity).
| RULER (mean of 13 tasks) | 10K | 20K | 40K | 80K | 160K | 320K |
| GPT-5.4 (full document) | 0.954 | 0.938 | 0.954 | 0.954 | 0.929 | 0.954 |
| Qwen3.6-35B-A3B (full document) | 0.923 | 0.938 | 0.938 | — | — | — |
| Finetuned Qwen3.6-35B-A3B (our harness) | 0.949 | 0.949 | 0.929 | 0.885 | 0.868 | 0.858 |
| OOLONG-synth (mean of 3 families) | 10K | 20K | 40K | 80K | 160K | 320K |
| GPT-5.4 (full document) | 0.718 | 0.666 | 0.600 | 0.539 | 0.556 | 0.479 |
| Qwen3.6-35B-A3B (full document) | 0.536 | 0.558 | 0.388 | — | — | — |
Table 1: Results by document length (tabular view of Figure 1 ). Our finetune runs in our harness with an 8K per-agent context limit. GPT-5.4 (medium reasoning, 1M token context limit) and base Qwen3.6-35B-A3B (reasoning disabled, 64K token Tinker serving limit) receive the full document in one call (see Appendix A for details). Bold marks the highest score per length.
| RULER | 10K | 20K | 40K | 80K | 160K | 320K |
|---|---|---|---|---|---|---|
| NIAH (8) | 1.00 | 1.00 | 1.00 | 1.00 | 1.00 | 1.00 |
| VT | 1.00 | 1.00 | 1.00 | 0.92 | 0.84 | 0.64 |
| CWE | 1.00 | 0.94 | 0.74 | 0.58 | 0.24 | 0.18 |
| FWE | 0.93 | 1.00 | 0.93 | 1.00 | 1.00 | 0.93 |
| QA-1 | 0.60 | 0.60 | 0.80 | 0.60 | 0.60 | 0.60 |
| QA-2 | 0.80 | 0.80 | 0.60 | 0.40 | 0.60 | 0.80 |
Table 2: Results by task family . Left: On RULER, our model chooses a lossy aggregation strategy for the common words “CWE” problem type, which degrades when common words are diluted by fillers as document length grows. Right: On OOLONG-synth, leaf classification and merge errors compound at length. Distribution mismatch also arises at 160K+ document length (it sees 14K doc size in training).
Appendix figures & tables3 assets
Supplementary material from the paper’s appendix.
Appendix
| Benchmark | System prompt task context | Root user message |
|---|---|---|
| OOLONG-synth | OOLONG’s dataset description, verbatim (its intro, the number of items, the label set) | OOLONG’s question, verbatim, + “Put your final answer inside \boxed{}.” |
| RULER | None (empty) | RULER’s task template, verbatim, with {context} replaced by a pointer to the document (below) + “Reply with the answer inside \boxed{}.” |
Table 3: Benchmark text fields. The task context is placed in the system prompt, so every subagent inherits it without the parent restating it. For RULER, {context} in the template is replaced by: “[The relevant text is in a separate document accessible via the read_chunk tool — see the system prompt for usage.] (Document length: N tokens.)”
| Event | Tool result returned to the calling agent (verbatim) |
|---|---|
| spawn_subagent | |
| Subagent ends with a boxed answer | The content of its last closed \boxed{…} |
| Subagent exceeds its context budget | agent overflowed context |
| Subagent stops without a boxed answer | agent did not box an answer |
| Tree node cap reached | Error: rollout node budget reached. Solve directly. |
| read_chunk | |
Table 4: Harness responses. An agent’s budget counts its whole rendered conversation (system prompt, tool schema, user message, its own turns and every tool result); each generation is capped at the remaining room. A subagent that answers returns only its boxed content, so its reads and reasoning never enter the parent’s context.
| Task | Strategy | Example question / what the leaf does | |
| Order-independent aggregation over structured records (one record per line) | |||
| synth_sum | tree | 60 | SUM of amt over all records |
| synth_count | tree | 60 | how many records have flag=Y |
| synth_max | tree | 60 | largest amt |
| synth_min | tree | 60 | smallest amt |
| synth_sumwhere | tree | 60 | SUM of amt over only the flag=Y records |
Table 5: Training tasks. is the number of problems per task. Tree : recursive halving, with each parent merging its two children’s partials (for retrieval, the half that finds the answer wins). Fold : one agent reads a slice and hands the rest of the document and its accumulator to the next. Every agent in each tree is a training example.
More in this topic
Long-Context Language Modeling
Long-context language modeling studies how language models can use, retain, and efficiently process information across very long sequences.
See all 182 papersRecent development of agents has renewed demand for long-context reasoning capacity of LLMs. However, training LLMs for this capacity requires costly long-document curation or heuristic context synthesis. We observe that agents produce massive trajectories when solving problems, invoking tools and receiving environment observations across many turns. The evidence needed to answer the original question is thus scattered throughout these turns, requiring integration of distant context segments. Nevertheless, standard agent SFT masks tool responses and only trains turn-level tool selection, creating a supervision blind spot where these scattered signals go unused. We propose Agent Context Compilation (ACC), which converts trajectories from search, software engineering, and database querying agents into long-context QA pairs that combine the original question with tool responses and environment observations gathered across multiple turns, training the model to answer directly without tool use. This makes the dependencies between the question and the evidence explicit, enabling direct supervision of long-context reasoning over distant segments without additional annotation. ACC is a simple but effective approach that can be combined with any existing long-context extension or training method, providing scalable supervised fine-tuning data. We validate ACC on long-range dependency modeling tasks through MRCR and GraphWalks, challenging benchmarks requiring cross-turn coreference resolution and graph traversal over extended contexts. Training Qwen3-30B-A3B with ACC achieves 68.3 on MRCR (+18.1) and 77.5 on GraphWalks (+7.6), results comparable to Qwen3-235B-A22B, while preserving general capabilities on GPQA, MMLU-Pro, AIME, and IFEval. Further mechanism analysis reveals that the ACC-trained model exhibits task-adaptive attention restructuring and expert specialization.
OctoLong: Mid-Training On Cross-Repository Code Contexts Enhances Long-Context Modeling
Context lengths of language models (LMs) have dramatically increased, driven by the demands for in-context learning, self-improvement, and long-horizon agentic workflows. However, existing long-context corpora are dominated by books, academic articles, and code repositories, which are finite and often lack long-distance dependencies. In this work, we introduce OctoLong, a context-engineering pipeline that instruments an AST parser, a language server backend, and a package manager to recursively retrieve code references, enabling the curation of dependency-rich code contexts up to millions of tokens long. We then train OctoLong-Instruct, a suite of capable long-context open LMs, derived from base models ranging in size from 600M to 14B parameters, via context-extension mid-training on a approx 50B-token mixture containing approx 6.2B tokens of OctoLong code contexts, followed by approx 10B tokens of instruction tuning. Our ablations and experimental evaluations against 18 strong long-context open-weight LMs show that supplanting just 12% of conventional context-extension corpora with OctoLong data yields substantial gains in long-range retrieval, long-term state tracking, repository-level code understanding, and downstream agentic tasks, while also enhancing API usage in regular coding scenarios.
ContextPilot: Teaching Agents for Proactive Context Management via Fine-grained RL
Long-horizon agentic tasks require large language models (LLMs) to iteratively retrieve, integrate, and maintain dispersed information across multi-turn interactions, but preserving all interaction histories leads to a continuously growing working context. Recent proactive context management methods allow models to edit their own working context with specialized tools, yet they still face three key limitations: (1) a limited toolset restricted to search, deletion, and summarization, with no support for global planning, long-term memory, and adaptive compression; (2) inefficient exploration that treats context management actions uniformly despite their heterogeneous impacts on final outcomes; and (3) coarse-grained credit assignment that assigns the final trajectory-level reward to all intermediate context editing actions during RL. To bridge these gaps, we introduce ContextPilot, a proactive context management framework for long-horizon agentic reasoning. Our approach systematically augments the toolset with planning, long-term memory, and soft context offloading tools. We further propose an RL method tailored for context management, which uses context and entropy variation to identify critical editing decisions for branch sampling and estimates action-level advantages from all branched trajectories that pass through the corresponding context editing action. Experiments on long-context QA and deep search tasks show that ContextPilot achieves stronger performance with a more compact working context, consistently outperforming existing baselines across various base models and benchmarks. Code is available at https://github.com/Tencent/ContextPilot.