LLMs Take Out the Agent’s Trash: Researchers at Xiaohongshu detail LLM-based memory management technique for agents

AI agents typically compact the contents of their context windows by summarizing or deleting the oldest material.

Share
Illustrated process of context management, highlighting context editing before sending to LLM input.

AI agents typically compact the contents of their context windows by summarizing or deleting the oldest material. Researchers devised a method that manages an agent’s memory more selectively. 

What’s new: Xubin Hao and colleagues at the social-shopping site Xiaohongshu (also known as RedNote) built self-governing context (Self-GC). Like familiar agentic designs, Self-GC builds up a context of inputs and outputs that includes user requests and tool calls as well as results like URLs and file paths. Unlike typical compacting algorithms, a large language model decides which parts of the context to keep, trim, or throw away before sending it to the associated LLM. 

Key insight: Rules based on types or shelf lives of inputs and outputs aren’t sufficient to enable an agent to decide which contextual information it should retain. An older tool output may hold the only copy of a URL that must be reopened later, while a recent output may have become outdated. An LLM can make such judgments in a more flexible way, because it can read the information available as the context accrues and decide which details are likely to be useful later.

How it works: When input tokens fill more than 30 percent of the primary LLM’s context window, Self-GC sends the history to a planner model (by default, Qwen3.6-Plus) and asks what to do with each user request or tool call, plus their results.

  • The planner can retain these items or pick one of three actions. (i) “Fold” sets an item aside. Its content moves to separate storage, and a short note describes its location so the agent can bring it back word-for-word if necessary. (ii) “Mask” shortens an item in place, keeping its opening and closing text while cutting repetition in the middle, which suits long logs. (iii) “Prune” deletes items the task no longer needs, such as logs of failed commands.
  • Self-GC first carries out the planned actions on a copy of the history, discarding any action that may interfere with the most recent request or the agent's ongoing response to it. Then it measures how many input tokens the remaining actions would save.
  • Self-GC shortens the history only if it estimates that it would reduce the cost of running the model on future calls including savings from using cached inputs. In practice, they found that the plan was worth applying if it shortened the history by at least 30 percent, so they used that heuristic in their tests.

Results: The authors tested Self-GC on an agent that browses the web, runs shell commands, and edits documents for Xiaohongshu users. They compared it to methods that follow fixed rules such as deleting the oldest messages or deleting the tool outputs. Self-GC removed less history, but it was far less likely to lose useful information. To measure this, the authors replayed completed conversations between real-world users and its agent. For each conversation, they stopped partway through, ran Self-GC on the history up to that point, and used GPT-5.5 to judge whether the shortened history still held every detail the real-world conversation went on to use.

  • On a set of 33 conversations deemed to be especially demanding, Self-GC removed 43.95 percent of input tokens and kept the necessary details 84.85 percent of the time. The rule-based methods removed 61.90 to 69.87 percent of tokens and kept the necessary details 54.55 to 69.70 percent of the time.
  • On a larger set of 332 conversations, Self-GC removed 31 to 34 percent of input tokens while keeping the necessary details 91.27 to 94.58 percent of the time across three different planner models (Qwen3.6-Plus, Qwen3.7-Max, and GLM-5.1), showing that Self-GC can be effective with a variety of LLMs. The rule-based methods removed 40.19 to 47.76 percent of tokens while keeping the necessary details 77.71 to 87.46 percent of the time.
  • Running live on some real-world user accounts, Self-GC removed 10 to 15 percent of input tokens compared to accounts that did not use it.

Yes, but: The authors’ evaluation tested Self-GC’s ability to retain important details, but not its ability to produce good output. The output of a system that uses a shortened context may contain the same details and output as that of of a full-context system, but it may not be as useful.

Why it matters: Reducing context to save cost carries the risk that a model may forget a detail that’s required later. Knowing what to keep, trim, and remove enables long-running agents to be affordable without becoming forgetful.

We’re thinking: John McCarthy, who coined the term artificial intelligence, invented “garbage collection” for computers around 1959 so programmers would not have to manage computer memory by hand. The need to properly manage agent memory is another way in which LLMs can be treated like an operating system — and another way software engineering fundamentals are essential to building good AI applications!