CTXAgent - Self-Controlled Context Management of LLM Agents
Authors: Matthias Busch, Marius Tacke, Roland C. Aydin, Christian Feiler, Christian J. Cyron
Organizations: Institute for Artificial Intelligence and Simulation in Mechanics, Hamburg University of Technology, Eißendorfer Straße, 21073 Hamburg, Germany · Institute of Material Systems Modeling, Helmholtz-Zentrum Hereon, Max-Planck-Straße, 21502 Geesthacht, Germany · German Research Center for Artificial Intelligence (DFKI), Stuhlsatzenhausweg, 66123 Saarbrücken, Germany · Department of Materials Science and Engineering, Saarland University, 66123 Saarbrücken, Germany · Institute of Surface Science, Helmholtz-Zentrum Hereon, Max-Planck-Straße, 21502 Geesthacht, Germany · Institute for Interface Physics and Engineering, Hamburg University of Technology, Am Irrgarten, 21073 Hamburg, Germany
Agent harnesses keep long sessions inside the context window by compacting the history when a token threshold is crossed. We study an alternative in which the model manages its own context: it decides when to compact, what to keep and what to write as the hand-off, given two tools and one paragraph of guidance. Since earlier work found that open-weight models cannot manage their own context without scaffolding, we compare seven frontier models under identical conditions, with and without the context tools, on token cost and benchmark score, and we compare our harness with three widely used harnesses on two of these models. The benchmark is assembled from existing ones: coding exercises, multi-step SciCode problems and SWE-bench Pro and Terminal-Bench 4.0 tasks are interleaved with planted facts, ephemeral observations and standing instructions, all checked deterministically, in sessions of 35-212 turns that resemble a user working through different tasks in one chat. Most of the strongest models manage their context: they compact and curate regularly, the live context of the mixed sessions stays mostly below 100k tokens, and their benchmark scores mostly do not drop significantly. For the models that manage their context well, the cost reduction is substantial but varies between models, because some models compact so often that other costs increase. With Claude Opus 5.5, the self-managed harness matches Claude Code on task success at 34% lower cost than its explicit 200k-window configuration and 49% lower than its default, with no context window to choose. We release the agent, the benchmark and all result rows.
Figures & tables
Figure 1: One session script is shared by every arm. Coding exercises and SciCode steps are the filler; facts, ephemeral observations and standing rules are planted among them and probed at controlled distances; the context trajectory is read from the wire so that every probe can be labelled with whether a compaction lay between establishing turn and probe. The arms run the same model id through Claude Code, Codex, OpenCode and ctxagent behind one logging proxy.
family
sessions
turns
tasks per session
run with
Mixed
3 seeds
101–115
34 exercises, 6–7 SciCode problems, 20–23 edges
Claude Code, Codex, OpenCode, ctxagent (Opus, Sol); seven models, arms A and B
Clean chains
5 chats, 2 reps
3–11
1–2 SciCode problems, no edges
ctxagent vs. Claude Code or Codex at 200k (Opus, Sol)
Marathon
1
212
118 exercises, 15 SciCode problems, 21 edges
ctxagent , Claude Code, OpenCode (Opus); Codex, OpenCode, ctxagent (Sol)
five models, arms A and B; Claude Code, OpenCode (Opus) and Codex, OpenCode (Sol) at 200k
Table 1: The session families. Edges = planted facts and ephemeral observations with their probes; every family also plants two standing rules. Arms A and B are ctxagent with and without context management at a 1M window.
harness
version
windows
compaction
Claude Code
2.1.301
native, 1M, 200k
tool-result trimming; auto-compaction near the window (summary)
Codex CLI
0.163.0
native, 1M, 200k
server-side compaction at window −13 k; user messages kept
OpenCode
1.19.3
200k
auto-compaction when the configured window is reached
ctxagent with compaction
2a407b9
1M
model-decided ( compact_context , curate_context ); age masking as safety net; deep tier at 0.9 × 1M never reached
Common to all arms: same model id via OpenRouter, provider pinned (no fallbacks); reasoning effort high; output reserve 20k; 100 iterations and 1500 s per turn (3600 s on the hard sessions); fresh process and configuration per session; order shuffled by seed; never two trials of the same (model, harness) at once; tools aligned by class (files, shell, jobs); ctxagent keeps its context tools additionally.
Table 2: Harnesses and arms. “native” is the harness’s own window for the model id; 200k is set explicitly. Web access is off by flag or configuration in every harness, stated in an identical prompt preamble, and audited in every transcript (zero hits).
Figure 2: Context size per request against the user turn for seven models under the identical instruction to manage their own context (arm A, solid blue) and with no context management (arm B, dashed orange), three seeds each. Opus, Sol and Astra cut; Sonnet in one session of three; Fable through many small drops; Haiku and Muse Wonder never.
Figure 3: Mixed sessions, pooled over three seeds. Top: change of arm A (self-managed) against arm B (no context management) per model; blue marks a change in favour of arm A, vermilion one against it. Bottom: the absolute values, arm A (dark) above arm B (light). Left: session cost at list rates. Right: solvable SciCode steps passed out of 80; exercises and memory probes are at ceiling in both arms. The light-blue bars with an asterisk give the cost recomputed with the cache-read price set to a twentieth of the input price, the ratio shared by Opus, Sonnet, Sol and Muse Wonder (on the list, Fable’s cache reads cost a fortieth of its input, Astra’s a tenth). The token anatomy behind the cost change is Figure 10 in the appendix.
Figure 4: Hard sessions: change of arm A against arm B per model, pooled over three seeds, five models. Left: session cost at list rates. Right: SciCode-Verified steps and container tasks (SWE-bench Pro, Terminal-Bench 4.0) passed, taken together (51 per arm); memory items and probes are at ceiling in both arms. Blue marks a change in favour of arm A, vermilion one against it; Fable’s light-blue bar with an asterisk is the cost change at a cache-read price of a twentieth of the input price, as in Figure 3 . The absolute values are Figure 12 and the token anatomy is Figure 11 , both in the appendix.
Figure 5: Mixed sessions at a fixed model. The threshold harnesses run every session up to their cut (Claude Code at approximately 168k, Codex at approximately 145k of an explicit 200k window); the self-managed arm stays below 100k, with cuts at task boundaries.
Figure 6: Hard sessions: each harness arm at an explicit 200k window relative to ctxagent at 1M (arm A) on the same model, pooled over the three seeds: change in list cost per session (left) and in SciCode-Verified steps and container tasks passed, taken together, 51 per arm (right). Blue marks a difference in favour of ctxagent , vermilion one against it.
Figure 7: Marathon (212 turns): context size per request (left) and cumulative cost at list rates (right); OpenCode at 200k dotted.
Appendix figures & tables9 assets
Supplementary material from the paper’s appendix.
Appendix
Figure 8: Mixed dependent sessions, three seeds per arm (101–115 turns each): list cost per session, mean over seeds with the seed range, for every harness and window; the number at the end of each bar is the mean success on the 188 scored questions. Paired McNemar Claude Code vs. ctxagent on the same (seed, turn): Opus 3/0, p=0.25 ; Sol 7/1, p=0.07 . Memory probes and rules were at 1.0 in every arm.
model
arm
steps /86
per-chat
$ (10 chats)
$/step
peak
cuts
tool calls
wall
Opus 5.5
ctxagent 1M
78
0.895
3.74
0.048
23–38k
0
128
24
Opus 5.5
Claude Code 200k
78
0.895
4.72
0.061
38–51k
0
157
23
Sol 6.1
ctxagent 1M
62
0.749
2.89
0.047
19–42k
7
276
63
Sol 6.1
Codex 200k
68
0.804
2.62
0.039
22–50k
0
262
59
Appendix
Table 3: Five SciCode chats (43 steps per set, 86 over two reps), nothing else in context. No arm compacted except ctxagent -Sol. The eight Opus misses per arm are the four unsolvable-as-specified steps × two reps.
Figure 9: One 212-turn marathon session per arm (118 exercises in three languages, 50 SciCode steps, 21 edges at distances up to 200, two rules; 168 scored questions). Left: list cost of the session. Right: largest context, with the number of cuts. Success was 0.986 / 0.981 / 0.980 for Opus with ctxagent , Claude Code and OpenCode, and 0.960 / 0.953 / 0.965 for Sol with ctxagent , Codex and OpenCode; every memory item was kept in every arm.
model (prices)
arm
success
steps
memory
$/sess.
A/B
peak
prompt
cuts
occ. at cut
context calls
wall
Haiku 5.5 (0.1/0.01/0.125/0.5)
A
0.873
50
1.000
2.71
0.99
339k
174k
0.3
121k
cur 16, rec 2
23
B
0.870
49
1.000
2.72
417k
203k
0.0
–
–
23
Sonnet 5.5 (2/0.10/2.5/10)
A
0.926
65
1.000
4.50
0.84
202k
107k
1.0
110k [101–112k]
cmp 3
20
B
0.954
73
1.000
5.37
333k
163k
0.0
–
–
22
Opus 5.5 (4/0.20/5/20)
A
0.968
77
1.000
4.80
0.57
77k
42k
3.7
72k [40–80k]
cmp 11, cur 2, rec 3
21
B
0.979
80
1.000
8.37
244k
127k
0.0
–
–
24
Appendix
Table 4: Seven models, arm A (self-managed) vs arm B (no context management), 3 seeds per cell, list rates. Success = all scored turns; steps = solvable SciCode steps passed out of 80; memory = facts, ephemerals, probes, rules. Occupancy = prompt size of the last request before a cut, median [range]; a cut is a drop of the context between consecutive requests as in Figure 2 . Prices in $/M tokens: input / cache read / cache write / output. Context calls over three sessions: cmp = compact_context , cur = curate_context , rec = recall .
Figure 10: Token anatomy of the session cost on the mixed sessions (mean of three seeds) for arms A and B of the seven-model experiment: cache read, cache write, uncached input and output at list prices. The black marker gives the list cost including the long-prompt tiers (Haiku above 100k context tokens, OpenAI above 272k), which the flat split omits; the dotted line spans the surcharge.
Figure 11: Token anatomy of the session cost on the hard sessions (mean of three seeds), five models, arms A and B, as in Figure 10 .
Figure 12: Hard sessions: the absolute values behind Figure 4 , arm A (dark) above arm B (light). Left: list cost per session, mean of three seeds. Right: SciCode-Verified steps and container tasks passed, taken together, pooled over three seeds (51 per arm). Fable’s light-blue bars with an asterisk give the cost at a cache-read price of a twentieth of the input price, as in Figure 3 .
model
arm
success
questions /51
SWE /9
TB /9
steps /33
memory
$/sess.
A/B
peak
context calls
wall
Haiku 5.5
A
0.760
29
6
1
22
1.000
3.79
554k
cmp 1, cur 8, rec 1
53
B
0.782
31
8
2
21
1.000
4.05
0.94
625k
–
61
Sonnet 5.5
A
0.793
32
7
4
21
1.000
6.29
308k
cmp 1
59
B
0.793
32
7
4
21
1.000
5.45
1.15
366k
–
49
Opus 5.5
A
0.869
39
8
6
25
1.000
10.72
222k
cmp 8, cur 10, rec 1
55
B
0.826
35
7
6
22
1.000
12.71
0.84
426k
–
50
Appendix
Table 5: Hard sessions: five models, arm A (self-managed) vs arm B (no context management), three seeds with disjoint task sets, list rates. Success = all scored turns; SWE = SWE-bench Pro instances solved of 9; TB = Terminal-Bench 4.0 tasks solved of 9; steps = SciCode-Verified steps passed of 33; memory = facts, ephemerals, probes, rules; peak = largest prompt over the three sessions; context calls over three sessions as in Table 4 . Below: outcome of every container task, arm A/arm B, 1 = the task’s verifier passed.
model
arm
seed
success
memory
list $
requests
mean prompt (k)
peak (k)
cuts
wall (min)
Opus 5.5
Claude Code (native)
0
0.966
1.000
9.02
220
142
263
0
23
1
0.989
1.000
9.08
223
141
255
0
24
2
0.980
1.000
9.94
235
148
277
0
22
Opus 5.5
Claude Code (1M)
0
0.966
1.000
17.04
214
289
654
0
22
1
0.989
1.000
9.24
221
146
259
0
21
2
0.970
1.000
16.74
236
255
688
0
20
Appendix
Table 6: Per-session rows of the mixed dependent sessions (seeds 0–2): the baselines at native, 1M and explicit 200k windows, and ctxagent . Success = all scored turns; memory = probes, ephemerals and rules; cuts = compactions detected on the wire.
Long-horizon LLM agents accumulate tool results, files, plans, and user constraints that are too structured to be treated as a disposable text suffix. Current systems mostly rely on in-run heuristics such as chronological pruning and tool-output masking, or on final self-summary near a context limit. Heuristics are cheap but blind to future dependencies; summaries preserve narrative state but often hide exact evidence, locators, and editable artifacts. We present Self-GC, where GC denotes self-governing context while deliberately echoing garbage collection: the system does not merely reclaim unused tokens, but governs the lifecycle of agent context objects. Self-GC turns user turns, tool spans, and skill state into indexed objects; asks a side-channel planner to propose fold, mask, and prune actions; and lets the harness enforce recoverable sidecars, safe commit boundaries, and cache-aware commit. 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. On a 332-session production-derived suite, three planner backbones reach no-impact rates of 91.27% to 94.58%, while baselines remain at 77.71% to 87.46%. In production, an online account-level split reduces daytime average input tokens by 10% to 15%, with peak reductions near 20%. These results point to context management as runtime lifecycle control over indexed, recoverable objects rather than post hoc text cleanup.
Long-horizon large language model (LLM) agents are fundamentally limited by context. As interactions become longer, tool descriptions, retrieved memories, and raw environmental feedback accumulate and push out the information needed for decision-making. At the same time, useful experience gained from tasks is often lost across episodes. We argue that long-horizon performance is determined not by context length, but by how much decision-relevant information is maintained within a finite context budget. We present GenericAgent (GA), a general-purpose, self-evolving LLM agent system built around a single principle: context information density maximization. GA implements this through four closely connected components: a minimal atomic tool set that keeps the interface simple, a hierarchical on-demand memory that only shows a small high-level view by default, a self-evolution mechanism that turns verified past trajectories into reusable SOPs and executable code, and a context truncation and compression layer that maintains information density during long executions. Across task completion, tool use efficiency, memory effectiveness, self-evolution, and web browsing, GA consistently outperforms leading agent systems while using significantly fewer tokens and interactions, and it continues to evolve over time. Project: https://github.com/lsdefine/GenericAgent
We present Context Window Lifecycle (CWL), a context-management scheme that gives long-horizon LLM agents an effectively unbounded working horizon. As a session accumulates history, CWL keeps the context within budget through graduated, semantically-aware eviction: the agent annotates its trajectory as typed, dependency-linked episodes as work proceeds, and a deterministic, LLM-free policy evicts content in priority order within that structure when a token budget is exceeded. CWL preserves user turns and the exploratory context the agent is actively reasoning over, while aggressively shedding action episodes whose effects are already persisted in the environment, keeping active context near a stable ceiling that also avoids the performance degradation associated with very large prompts. Compared to summarization-based compaction, CWL avoids four well-known limitations: unpredictable lossiness, destruction of causal structure, blocking model cost, and compression-induced hallucination. Compared to recency truncation, CWL is semantically aware: it drops the oldest-and-most-recoverable content according to the dependency graph rather than oldest-in-time regardless of relevance. We describe the annotation protocol, the episode graph, the eviction policy, and the token-accounting loop, and evaluate CWL on long-horizon agentic benchmarks: a single agent session completing 89 sequential tasks across 80 million tokens with no measurable degradation in task accuracy relative to per-task isolated sessions