Self-Context: Graph-Based Context Self-Management for Long-Horizon Agents
1Xiamen University ·
2Key Lab of Digital Protection and Intelligent Processing of Intangible Cultural Heritage of Fujian-Taiwan (XMU)
3Unisound ·
4Washington University in St. Louis ·
5Nanyang Technological University ·
6Institute of Automation, Chinese Academy of Sciences
*Equal contributions · †Project leader · ‡Corresponding author
Harnesses stuff the prompt. Self-Context lets the model curate it.
Faster convergence: Self-Context reaches Vanilla Harness’s final 74.2% success by turn 61 (vs. turn 100) and finishes at 82.0% in 33.6 mean turns.
Curating context from the inside, without losing raw history
Long-horizon agents face two core context bottlenecks: accumulating context noise as verbose shell logs, superseded hypotheses, and distractor documents crowd the window, and irreversible context compaction when external summarization discards raw evidence that later turns need again. Self-Context is a graph-based plug-and-play context self-management framework between a frozen LLM and its native harness, assembling c_t = F_t ⊕ G_t on every turn via history management 𝒯 (reversible whole-turn fold / unfold yielding critical history F_t) and graph management 𝒢 (an editable working graph G_t).
F_t ⊕ editable working graph G_t
unfold from lossless native history H_t
<graph> ops (𝒯 & 𝒢) emitted by actor
G_t while keeping folded turns losslessly recoverable from native history H_t.
Try c_t = F_t ⊕ G_t in your browser
Inspect a multi-turn search episode (“Which river runs through Mira Vale’s birthplace?”). Click any turn card on the left to toggle whole-turn Fold / Unfold, or edit and apply a <graph> XML block on the right. Compare the model-facing context c_t against the untouched native history H_t.
c_t: 5,273 chars
Native history H_t: 12,384 chars
Footprint delta: −57.4%
Context Inspector · live
5 active nodes · 1 turn foldedunfold100% (12,384 ch)Model <graph> Block
editable XML protocol
Step through the turn pipeline and real benchmark episodes
Every turn passes through four deterministic stages inside the gateway: parsing <graph> operations from the model reply, delivering clean native tool calls to the harness, applying graph and fold updates, and reassembling c_t = F_t ⊕ G_t. Switch tabs below to inspect the animated architecture (Figure 2) or replay real BrowseComp-Plus episodes (Figures 5 & 6).
Consistent gains across search, code, and working tasks
Across 1,019 tasks spanning BrowseComp-Plus (830 search tasks), Terminal-Bench 2.1 (89 code tasks), and QwenClawBench v1.1 (100 working tasks), Self-Context achieves the highest macro-average score among all nine evaluated methods on both Qwen3.8-27B and Qwen3.6-27B while cutting interaction turns by up to 31.8%.
| Backbone | Method | Search: BCP (%) | Turns | Code: TB 2.1 (%) | Turns | Working: QwenClaw (%) | Turns | Macro Avg. (%) | Macro Turns |
|---|
Complementary components: removing history management (w/o 𝒯) drops Terminal-Bench 2.1 by 13.1 pp (82.0% → 68.9%), while removing graph management (w/o 𝒢) drops BrowseComp-Plus by 8.8 pp (57.3% → 48.5%).
Active pruning, not append-only notes: the ratio of remove to add is 0.88× (94,441 vs. 107,571), and 17.6% of operations revise existing nodes in place via edit.
Also in the paper
edit, remove, link) make up 43.4%, 41.7%, and 30.6% of all graph ops across search, code, and working tasks.
Every experiment uses unmodified native benchmark harnesses and identical actor sampling parameters across three independent runs.
Why intrinsic, reversible self-management beats external compaction
External compaction guesses what future turns will need; the actor already knows. Dedicated compaction baselines often fall below Vanilla Harness in macro average (e.g., 62.9% for ACM and 57.7% for CLM vs. 64.1% for Vanilla Harness on Qwen3.8-27B) because periodic summarization and hard resets sever the link between candidate hypotheses, rejected distractors, and exact tool identifiers.
History management 𝒯 and graph management 𝒢 target complementary bottlenecks. In code tasks, verbose shell traces flood the prompt; whole-turn fold collapses completed turns into compact stubs while preserving 1-to-1 tool-call/result pairing in H_t so unfold can recover exact tracebacks on demand. In multi-hop search, the editable working graph G_t prevents cyclic re-queries by linking disproved candidates and verified findings under explicit subgoals.
Drop into your existing OpenAI or Anthropic agent in two lines
Keep your native harness, tool definitions, and container sandbox unchanged. Point your SDK’s base_url at self-context-gateway, or run the dependency-free scripted demo offline in seconds.
1 · Offline Demo & CLI Gateway
POSIX / Python 3.12 + uvgit clone https://github.com/XMUDeepLIT/Self-Context.git
cd Self-Context && uv sync --locked
# Run offline mechanism demo (no API key or GPU needed)
uv run --no-sync python quick_start.py
# Or launch the drop-in HTTP gateway in front of vLLM / SGLang
uv run --no-sync self-context-gateway \
--upstream http://127.0.0.1:8000/v1 --model Qwen3.8-27B \
--provider openai --tokenizer /path/to/Qwen3.8-27B-FP8 \
--port 8700 --log-dir outputs/gateway
2 · Python API & SDK Integration
OpenAI & Anthropic compatiblefrom openai import OpenAI
from self_context import GatewayConfig, Graph, create_app
# Point any existing OpenAI / Anthropic harness at the gateway
client = OpenAI(
base_url="http://127.0.0.1:8700/v1",
api_key="EMPTY",
)
# Or mount the ASGI gateway directly in-process
app = create_app(GatewayConfig(
provider="openai",
base_url="http://127.0.0.1:8000/v1",
model="Qwen3.8-27B",
default_mode="self_context",
))
Cite Self-Context
BibTeX · arXiv:2612.01681
@misc{wang2026self-context,
title = {Self-Context: Graph-Based Context Self-Management for Long-Horizon Agents},
author = {Ziheng Wang and Chengyi Yang and Renxu Bao and Zhishang Xiang and Chengsong Huang and Wenhao Li and Junnan Zhu and Delai Qiu and Yining Wang and Shengping Liu and Jinsong Su},
year = {2026},
eprint = {2612.01681},
archivePrefix = {arXiv},
primaryClass = {cs.AI},
url = {https://arxiv.org/abs/2612.01681}
}