Command Palette
Search for a command to run...
ACM: Agentic Context Management for Long Horizon Tasks
ACM: Agentic Context Management for Long Horizon Tasks
Xiaochuan Li Ryan Ming Meng Chu Shuai Shao Rong Jin Chenyan Xiong
Abstract
Agentic tasks are inherently long-horizon and multi-turn, constantly accumulating context through interactions with the environment. Existing context compression methods inevitably incur information loss and are triggered by rigid heuristic rules, leaving them misaligned with the agent’s evolving reasoning focus. We propose Agentic Context Management (ACM), a framework that equips agents with purposebuilt context editing tools for lossless context management. Inspired by the interaction between short-term and long-term human memory, the agent autonomously decides when to compress its context, offloads discarded content to an external memory system, and queries it on demand for later retrieval. Building on this framework, we further develop a post-training pipeline that constructs high-quality demonstrations of context management and improves model performance on both agentic search and coding tasks. Further analysis reveals that effective context management reduces peak token pressure, enables extended explorations, and yields more consistent solutions across independent trials.
One-sentence Summary
Researchers from Carnegie Mellon University and Meta propose Agentic Context Management (ACM), a framework that equips agents with lossless context editing tools for autonomous, memory-inspired compression and retrieval, and demonstrate through a post-training pipeline that ACM reduces peak token pressure, extends exploration, and improves consistency on agentic search and coding tasks.
Key Contributions
- Agentic Context Management (ACM) is introduced as a framework that gives language model agents explicit tools for lossless compression and retrieval of context, replacing external modules or fixed heuristics with agent-initiated decisions.
- A teacher-guided post-training pipeline with dual constraints produces high-quality demonstrations of context management, enabling the model to learn when to compress and when to refrain without heavy reinforcement learning.
- Experiments on agentic search and coding benchmarks show that ACM outperforms ReAct and summary-based baselines, with further analysis revealing reduced peak token pressure, longer exploration horizons, and more consistent solutions across independent trials.
Introduction
Large language model (LLM) agents tackling complex, long-horizon tasks generate verbose histories that mix reasoning traces with noisy tool outputs. These histories routinely exceed practical context limits, degrading performance even on models with nominally large context windows. Prior mitigation strategies include long-context pretraining (which still suffers degradation), hybrid attention (still bounded by the window), and heuristic context compression pipelines that rely on fixed, external rules rather than the model’s own reasoning.
The authors propose Agentic Context Management (ACM), a framework that gives the agent explicit memory tools so it can autonomously decide when to summarize and offload irrelevant information to external storage and when to retrieve it. They further introduce a teacher-student post-training pipeline with dual constraints that teaches the model when to invoke and when to avoid context management actions. This approach reduces peak token usage, extends test-time exploration, and yields more consistent solutions across independent trials.
Dataset
The authors construct a training dataset through a teacher-guided pipeline designed to teach models when to invoke context management tools. The data is generated dynamically from student model rollouts and teacher annotations rather than collected from static sources.
Dataset composition and sources
- The dataset is built from student model trajectories on tasks that require context management.
- A teacher model from the same model family annotates these trajectories under two complementary constraints.
- The final training set mixes teacher-annotated data with resampled original student rollouts for stability.
Key details for each subset
- Injection data (H⁻ rollouts): The student completes tasks without access to context management tools. The teacher, using a guided prompt, identifies turns where compression would be beneficial (e.g., redundant queries, unproductive loops, sufficient accumulated context) and inserts a context management action with a reasoning trace.
- Refinement data (H⁺ rollouts): The student completes tasks with access to context management tools. The teacher identifies turns where the student’s compression calls are premature or unnecessary and replaces them with a more productive action (e.g., searching for evidence, opening a document, committing to an answer), again with a reasoning trace.
- Resampled rollouts: Original student trajectories are resampled in a self-distillation manner and mixed in to stabilize training.
How the paper uses the data
- The student is trained via on-policy distillation: the teacher provides soft next-token distributions (top-K, with K=20) for all assistant-token positions in the rollout.
- The loss is applied only to assistant-generated tokens; system-prompt, user-input, and tool-output tokens are masked out.
- This objective jointly teaches the student when to invoke context management and when to refrain.
Processing details
- Quality filtering: Rejection sampling retains only trajectories where the student fails to complete all trials successfully, focusing learning on genuinely challenging problems. Content filters verify that the teacher’s reasoning traces do not leak information from the reference answer; annotations must cite trajectory-level cues (redundant queries, cyclic patterns, sufficient evidence) without revealing the target answer.
- Cropping strategy: Not applicable; data comes from full model rollouts.
- Metadata construction: Each annotated turn includes a replacement action and a reasoning trace that justifies the decision.
Method
The authors formulate the agent interaction where the agent πθ conditions on the accumulated history Ht={s,(a1,o1),…,(at−1,ot−1)} to produce an action at∼πθ(⋅∣Ht), and the environment returns a response ot∼πγ(⋅∣Ht;at). The interaction terminates when the agent selects a finish action or the context window limit is reached. Unlike the ReAct paradigm, which eventually hits the context limit, or the Summary Agent paradigm, where an external monitor forces compression when usage exceeds a predefined threshold and discards original messages, the proposed Agentic Context Management (ACM) framework allows the agent to autonomously manage its context.
Drawing inspiration from human cognition, the ACM agent utilizes two context management tools to mimic short-term and long-term memory mechanisms. The manage_context tool compresses previous turns into a concise summary and offloads the raw messages to an external file on disk, while the query_memory tool allows the agent to query the stored raw messages to retrieve information precisely. When the agent invokes manage_context, a summarizer LLM compresses all messages up to the previous summary boundary. Crucially, the original messages are preserved in external storage and mapped to the summary via a unique identifier. When the agent needs to revisit earlier content, it invokes query_memory with the specified identifier, and a querier LLM returns the relevant information. This design ensures lossless information compression and agent-initiated context management, allowing the agent to invoke compression at any point during the reasoning process rather than relying on a fixed schedule.
To address the challenge of determining the appropriate timing for context management, the authors design a teacher-guided data generation pipeline with dual constraints. The pipeline employs a teacher-student framework operating in two phases.
In the first phase, Student Rollout, a student model completes the task under two conditions: with and without access to context management tools, producing trajectories denoted H+ and H−, respectively. H+ captures the student's untrained usage behavior of the tools, while H− reflects its ordinary exploration behavior. In the second phase, Teacher Annotation, a teacher model receives guided instruction prompts P+ or P− along with the corresponding student trajectory and the reference answer A∗. The teacher produces annotations under two complementary constraints. For injection on H−, the teacher using P+ identifies turns where context management would be beneficial, such as when the student begins querying redundant topics or enters unproductive loops, and replaces the action with a context management tool call at′. For refinement on H+, the teacher using P− identifies turns where the student's context management calls are premature or unnecessary, replacing the inappropriate call with a more productive action at′, such as searching for additional evidence.
The student is then trained using on-policy distillation. A stronger teacher from the same model family annotates each student-generated assistant token with a soft next-token distribution. The student is optimized to match these teacher distributions over all assistant-token positions in the rollout using the following loss function:
LACM(θ)=−Eτ∼πθ[t∈Ta(τ)∑v∈V∑pT(v∣s;h<t)logπθ(v∣s;h<t)].where τ is a trajectory sampled from the student policy, Ta(τ) denotes the set of assistant-token positions, and V contains the teacher's top-K candidate tokens at position t. The distribution pT(⋅∣s;h<t) denotes the teacher probabilities restricted and renormalized over V, while πθ(⋅∣s;h<t) denotes the student's next-token distribution. The loss is applied to all student-generated assistant tokens, while system-prompt, user-input, and tool-output tokens are masked out.
To ensure data quality, the authors apply two filtering mechanisms. Rejection sampling retains only trajectories in which the student fails to complete all trials successfully, ensuring the student learns from the teacher's behavior on genuinely challenging problems. Content filters verify that the teacher's reasoning traces do not leak information from the reference answer A∗, encouraging the model to recognize compression-worthy patterns from the trajectory structure rather than memorizing answer-dependent cues. Finally, to stabilize training, trajectories are resampled from the student's original rollouts and mixed with the teacher-annotated data.
Experiment
The evaluation uses three long-horizon benchmarks (BrowseComp-Plus, DeepSearchQA, SWE-Bench Verified) with Qwen3.5-9B as the student model and a much larger teacher for on-policy distillation. The ACM framework enables agents to proactively compress context, which already outperforms ReAct, summary-based, and memory-based baselines; post-training on curated context management data yields a 27% relative gain and nearly matches models 40 times larger. Behavior analysis shows ACM agents trigger compression well before context limits, dramatically reducing peak token usage while enabling more exploratory tool calls, and the approach narrows the gap between Pass@1 and Pass@4 by making correct solutions more reliable. Ablations confirm that dedicated context management data and teacher distillation are complementary, and a case study demonstrates the model self-monitoring, re-reading compressed history, and maintaining a compact working window through a 222K-token trajectory.
ACM uniquely combines active compression, trainable policy, lossless storage, agent-initiated triggers, and open-source training data, unlike prior methods which each omit one or more of these properties. Post-training on context-management data substantially improves reliability (Pass@1 and Pass^4) and narrows the gap to Pass@4, while also increasing the frequency of compression and retrieval tool calls. Combining synthesized context-management data with general distillation yields the best overall performance, as the two sources provide complementary skills. ACM is the only method that is simultaneously compact, trainable, lossless, agent-initiated, and trained on open-source data. Prior memory-augmented approaches accumulate knowledge across tasks but do not compress working context within a single episode. Post-training on context-management data substantially raises Pass@1 and Pass^4, making correct solutions more reliable rather than only expanding the capability boundary. GPT-5.5 distillation alone fails to surpass the post-trained ACM agent on agentic search, while dedicated context-management data delivers consistent gains across tasks. Combining distillation with context-management data yields the best overall performance, indicating the two sources are mutually reinforcing. ACM post-training increases the frequency of manage_context and retrieval tool calls, enabling broader exploration of reasoning paths. In a case study, the model self-monitors and compresses only under genuine memory pressure, interleaving compression with memory probes while keeping the working window well below the limit.
Equipping an agent with the ACM framework improves performance over baselines, and post-training on context management data yields further gains including a 27% relative improvement on BrowseComp-Plus. The approach enables smaller models to explore more effectively through increased tool calls while simultaneously reducing peak token usage, lowering both reasoning burden and KV-cache overhead. ACM-equipped agents surpass all baselines without post-training, demonstrating the effectiveness of agent-initiated context management. Post-training on context management data yields a 27% relative gain on BrowseComp-Plus and approaches the performance of open-source models 40 times larger. Smaller agent models rely more on exploration via tool calls to solve problems, and context management enables them to explore effectively. Peak token usage decreases dramatically under the ACM framework, especially compared to the Summary Agent, reducing model reasoning burden and server KV-cache overhead. Post-training on context management data substantially increases Pass@1 and consistency across trials by keeping context clean and well-organized.
Combining distilled trajectories from a strong teacher with synthesized agentic context management data yields the best overall performance across tasks, though the two sources contribute complementary strengths. Context management data alone consistently improves performance and increases tool usage for exploration, while distillation alone can underperform the base agent on search tasks. The results indicate that dedicated context management training is necessary and that distillation and ACM data are mutually reinforcing. Adding ACM data alone improves pass@1 over the base model on all three tasks, whereas distillation alone reduces pass@1 on BrowseComp-Plus and DeepSearchQA. ACM training substantially increases the average number of tool calls per episode on search tasks, with the +ACM variant issuing 46.2 tools on BrowseComp-Plus compared to 30.8 for the base model. Distillation alone reduces peak token count on DeepSearchQA but raises it on BrowseComp-Plus, while ACM alone lowers peak tokens on both search benchmarks relative to the base model. Combining distillation with ACM achieves the highest pass@1 on BrowseComp-Plus and SWE-Bench Verified, but slightly underperforms ACM alone on DeepSearchQA.
The evaluation compares an Active Context Management (ACM) agent against baselines on agentic search and software engineering tasks. ACM uniquely integrates active compression, a trainable policy, lossless storage, agent-initiated triggers, and open-source training data. Post-training on synthesized context-management data significantly improves reliability and consistency, while combining this data with general distillation from a strong teacher yields the best overall performance, as the two sources provide complementary skills. The ACM framework enables smaller models to explore more effectively through increased tool calls while simultaneously reducing peak token usage and KV-cache overhead.