Xiaohongshu's Self-GC Cuts Agent Token Costs by 20% Without Losing Critical Context

Xiaohongshu's Self-GC uses a planner LLM to fold, mask, or prune agent context, retaining critical details 84.85% of the time versus 54.55% for rule-based baselines.

·
·
Xiaohongshu's Self-GC Cuts Agent Token Costs by 20% Without Losing Critical Context
Read4 min
TopicAgents · Llms
SubtopicMemory · Long Context
  • Xiaohongshu researchers introduce Self-GC, an LLM-planner-based context manager for long-horizon agents.
  • Planner picks fold, mask, or prune actions on indexed context objects instead of using fixed rules.
  • Retained necessary details 84.85% of the time on hard set vs 54.55 to 69.70% for baselines.
  • On 332 production sessions, no-impact rates hit 91.27 to 94.58% across three planner models.
  • Live deployment cut daytime input tokens 10 to 15%, with peaks near 20%.
  • Only commits cleanup if projected savings exceed 30% after prompt-cache accounting.

Long-running AI agents have a hoarding problem. Every tool call, file path, URL, and intermediate log piles up in the context window until costs balloon or the model starts forgetting what it was doing. Researchers at Xiaohongshu (also known as RedNote) have proposed a smarter cleanup mechanism they named after a concept every programmer already knows: garbage collection.

Their system, Self-GC (Self-Governing Context), hands the cleanup job to an LLM planner instead of fixed rules like deleting the oldest messages. The name deliberately echoes garbage collection: rather than reclaiming unused tokens by position, it governs the full lifecycle of agent context objects.

Why rule-based trimming keeps failing

Most agent frameworks compact context with blunt heuristics: chop the oldest messages, drop tool outputs, or run a summarization pass when approaching the token limit. Heuristics are cheap but blind to future dependencies; summaries preserve narrative state but routinely bury exact evidence, locators, and editable artifacts.

Importance does not correlate with age. An old tool call might contain the only copy of a URL the agent needs to revisit an hour later, while a fresh output could already be stale. Fixed rules cannot make that judgment, so they either keep too much (expensive) or drop something critical (broken agent).

How Self-GC works

Self-GC treats the context as a list of indexed objects: user turns, tool spans, and skill state. When token usage crosses a threshold, a side planner model reviews each object and picks one of three actions.

  • Fold: move the item to separate storage and leave a short breadcrumb noting where it lives, so the agent can retrieve it verbatim if needed.
  • Mask: shorten in place by keeping the opening and closing text while collapsing repetitive middles, which works well for long logs.
  • Prune: delete outright, for items like failed shell command logs that the task will never revisit.

For offline experiments, Self-GC applies a 30% compression threshold and mandatory last-turn retention. The planner sees indexed context objects and emits fold, mask, or prune actions over existing identifiers. Before any edit reaches the active view, the harness rehearses the plan, removes invalid or cut-turn actions, normalizes overlapping instructions, materializes the projected context, and estimates token savings. The cleanup only commits if it saves at least 30% of tokens after accounting for prompt caching, so planner-model costs are not burned on a trivial trim.

Benchmark results

The team tested Self-GC against fixed-rule baselines on real conversations from a Xiaohongshu agent that browses the web, runs shell commands, and edits documents. Each conversation was replayed, a cleanup was applied partway through, and GPT-5.5 judged whether the shortened history still contained every detail the rest of the conversation actually needed.

On a 33-session Hard Set, Self-GC prunes 43.95% of prefix tokens while leaving 84.85% of future continuations unaffected, compared with no-impact rates of 54.55% to 69.70% for heuristic baselines. The rule-based methods pruned more aggressively, up to 69.87%, but shed information in the process.

On a 332-session production-derived suite, three planner backbones reach no-impact rates of 91.27% to 94.58%, while baselines stay at 77.71% to 87.46%. The team swapped in Qwen3.6-Plus, Qwen3.7-Max, and GLM-5.1 as planners, suggesting the framework does not depend on a single model. In live production, an online account-level split cuts daytime average input tokens by 10% to 15%, with peak reductions near 20%.

Open questions

The evaluation measures whether necessary details survive cleanup, not whether the agent's final output improves. A context that retains the right tokens can still steer a model toward a worse answer, so downstream quality remains unexamined. Running a planner LLM on the side also adds latency and cost, though the 30% savings gate is designed to keep that overhead net-positive.

What agent builders can take from this

The core architectural idea is context management as runtime lifecycle control over indexed, recoverable objects, rather than ad hoc text trimming. Treating context like memory in an operating system, with typed objects, references, and eviction policies, is a fundamentally different design from treating it as a scrolling transcript, and one that maps onto patterns engineers have used in runtime systems for decades.

For anyone building long-horizon agents that chew through tool outputs, the pattern is worth adopting in pieces. Route context through structured objects, let a cheap planner model classify them into fold, mask, or prune, and commit only when savings clear a threshold. That sits as a practical middle ground between naive truncation and expensive full-context replays.

Comments

avatar