Self-Context
Inference-Time Context Gateway · Zero Retraining · Unchanged Harnesses

Self-Context: Graph-Based Context Self-Management for Long-Horizon Agents

Ziheng Wang1,2*, Chengyi Yang1,2†*, Renxu Bao1,2*, Zhishang Xiang1,2, Chengsong Huang4, Wenhao Li5, Junnan Zhu6, Delai Qiu3, Yining Wang3, Shengping Liu3 and Jinsong Su1,2‡

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.

Long-Horizon Performance · Three Benchmarks (Qwen3.8-27B)
55.3
53.1
57.3↑ +4.2
BrowseComp-Plus
65.9
74.2
82.0↑ +7.8
Terminal-Bench 2.1
69.7
65.0
70.2↑ +5.2
QwenClawBench v1.1
Best prior context method Vanilla Harness Self-Context (Ours)
Cumulative Success vs. Turn Budget · Terminal-Bench 2.1

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.

01 · The Idea

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).

Conventional Harnesses
Self-Contextours
context state direct accumulation or irreversible compaction critical history F_t ⊕ editable working graph G_t
reversibility context noise buildup or permanently lost clues selective unfold from lossless native history H_t
control locus fixed external heuristics or auxiliary summarizer calls intrinsic XML <graph> ops (𝒯 & 𝒢) emitted by actor
integration entangled with custom agent loops & tool schemas plug-and-play HTTP gateway (OpenAI & Anthropic wire)
Qwen3.8-27B 64.1% macro avg · 27.0 mean turns 69.8% macro avg (+5.7 pp) · 20.6 mean turns (−23.7%)
Paper Figure 1 contrasting direct accumulation (Vanilla Harness), irreversible context compaction, and Self-Context on a long-horizon episode.
Figure 1 Overcoming accumulating context noise and irreversible context compaction. Direct accumulation floods the window with distractors; irreversible compaction overwrites subtle clues; Self-Context curates verified findings and subgoals in working graph G_t while keeping folded turns losslessly recoverable from native history H_t.
02 · Playground

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.

● Self-Context Engine · live whole-turn simulator
Model context c_t: 5,273 chars Native history H_t: 12,384 chars Footprint delta: −57.4%
Context Inspector · live
5 active nodes · 1 turn folded
c_t = F_t ⊕ G_t model-facing context
Critical History F_t (click a turn to fold / unfold)
Editable Working Graph G_t
Native History H_t stored in harness
Lossless Turn Trajectories (h_i, a_i, o_i)
Gateway Wire Invariants
Tool-call / result ID bijection3 / 3 paired ✓
Recoverable via unfold100% (12,384 ch)
Harness tool-schema edits0 (transparent)
Unfolded prefix cacheappend-stable
Turn 1 folded into a 1-line stub in F_t; raw 7,420-char biography stays intact in H_t.
Model <graph> Block
editable XML protocol
ready
initial episode state
03 · Architecture & Trajectories

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).

04 · Results

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%.

Table 1 · Qwen3.8-27B · Macro Average (%)
Mean across 3 runs · top label shows mean interaction turns
Context compaction & self-management baselines Vanilla Harness Self-Context (Ours)
Table 2 · Component Ablations (Qwen3.8-27B Macro Avg.)
Self-ContextHistory 𝒯 + Working graph 𝒢
69.8%
w/o 𝒯Without history management
64.0%
w/o 𝒢Without graph management
61.2%
w/o 𝒯 and 𝒢XML <commit> without state curation
57.6%

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%).

Figure 4 · Intrinsic Curation (255,208 Recorded Operations)
add
42.2%
remove
37.0%
edit
17.6%
fold
3.3%

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

Turn 61 vs. 100 On Terminal-Bench 2.1, Self-Context matches Vanilla Harness’s final 74.2% success in 61 turns (vs. 100), and in 19 vs. 27 turns on BrowseComp-Plus and 22 vs. 139 on QwenClawBench v1.1.
0.88× Remove / Add Nodes leave the working graph nearly as fast as they enter. Revision operations (edit, remove, link) make up 43.4%, 41.7%, and 30.6% of all graph ops across search, code, and working tasks.
−30.6% Shell Execs With Qwen3.8-27B, Self-Context cuts redundant shell executions by 30.6% on Terminal-Bench 2.1 and document reads by 19.8% on BrowseComp-Plus while raising task accuracy.
2× Findings / Hyp. Recovered graphs decompose each Goal into 2.2–3.6 Subgoals and pair Hypothesis nodes (16.1%) with roughly twice as many Finding nodes (32.6%) for hypothesis-driven exploration.

Every experiment uses unmodified native benchmark harnesses and identical actor sampling parameters across three independent runs.

05 · Discussion

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.

06 · Gateway & Code

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 + uv
git 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 compatible
from 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",
))
07 · Citation

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}
}