Long-horizon language agents accumulate tool results and replies until the history no longer fits the context window. Current agents shrink this history by having a model summarize it, so each compaction rereads and rewrites the history it replaces, and cache-aware triggers decide when to compact while taking the size of the result as given. We present Folio, a context manager that records, folds, and recovers history through one set of handles. While the agent runs, Folio records three linked levels: a short turn note the model writes as part of its ordinary reply (L1), an event manifest of every call written by the runtime (L2), and the captured source records (L3). Because these views already exist, the runtime folds the context programmatically, selecting coarser views for older turns. The fold prints the handles that index L2 and L3, so recovery tools that read bounded pages, compute over full text in files, and keep findings in a persistent dossier reach the evidence the agent needs with little added input. The fold trigger prices an explicit landing point that includes the retained budget. On 75 LOCA-bench tasks with DeepSeek-V4-Pro behind every system, Folio solves 66 tasks against 54 for the same runtime with model-written compaction and 49-59 for Claude Code, Codex, and SoL-Pi, in about half their working time, at 2.4 times lower cost than that runtime; each fold takes a median of 0.6 seconds.
Figures & tables
Figure 1: Overview of 1. Record : the runtime turns tool events into a three-level indexed history. Fold : the cost-aware trigger selects views of this history to build the active context. Recover : the handle in the outline ( t3.12 ) is passed to scan for a bounded read or to ctx to compute over the full text, and the result is kept in the dossier.
Figure 2: Folding and recovery on one example. (a) Turn t3 exists as a turn note (L1), the manifest row of call t3.12 (L2), and the 900-row table it returned (L3). Within the layout budget L , the fold shows t1 briefly, t2 with its full note, and t3 with the row of t3.12 ; the open turn t4 keeps a raw head and tail. (b) Through the same handle, scan checks one value and ctx counts every row; the finding and its source go to the dossier, which the next fold carries.
Success
Time (min/task)
System
/75
per chain
mean
median
Flag.
Claude Code
59
11.8 ± 0.8
18.5
13.7
9
Codex
57
11.4 ± 1.7
20.2
15.4
27
SoL-Pi
49
0 9.8 ± 1.3
21.0
17.9
24
Piki-Classic
54
10.8 ± 1.3
16.3
13.7
11
1 (128K)
60
12.0 ± 1.2
16.4
0 7.8
9
Table 1: LOCA-bench, 5 chains × 15 tasks, DeepSeek-V4-Pro for every system. Per chain: mean ± s.d. of successes out of 15. Flag.: runs flagged by the benchmark’s contamination scan.
Figure 3: (a) Cumulative successes along the 15-task session, averaged over the five chains; the dotted line solves every task. (b) Distribution of working time per task over all 75 tasks.
Figure 4: Successes per LOCA-bench environment (out of 5 seeds). Rows are sorted by the mean of Claude Code, Codex, and SoL-Pi, hardest first.
1
Piki-
Per task
1M
128K
Classic
Input tokens (M)
4.01
2.72
14.12
cached
3.69
2.46
13.29
uncached
0.32
0.27
0.83
Output tokens (M)
0.07
0.10
0.10
Cache hit rate
92.0%
90.2%
94.1%
Table 2: Usage per task (means over 75 tasks). Normalized cost is h+30u+90o in cached-read token equivalents; for Piki-Classic it includes its summary requests.
Variant
n
Succ.
Cost
Folds
Min
A 1
75
66
19.75
85
10.2
B − dossier
75
65
19.73
85
10.7
− L1 notes
75
63
19.43
80
0 9.5
A 1
15
13
18.07
16
0 7.8
B − dossier
15
13
20.89
18
12.5
C − scan , ctx
15
0 8 †
18.65
18
21.7
Table 3: Ablations (1M window, L=50 K). Top: all 75 tasks. Bottom: the first chain, on which C and D were run. Cost: normalized tokens per task (M); Min: minutes per task. Each of B–D removes one more component than the row above. † Two timeouts and one agent error.
Figure 5: Landing-budget sweep on the fifth chain (15 tasks; mean of an ascending and a descending run, one run at L=10 K). Recoveries are scan and ctx calls that reach content removed by the last fold. In the shaded region the raw area hits its 60K cap, so the effective budget is 78–90K (hollow markers).
Figure 6: One 1 session (fifth chain, 1M window, 432 requests). Top: input tokens of each request and the landing point S ; red dots mark the 17 folds. Bottom: carried excess relative to the fold price; a fold occurs when it reaches 1.
1 fold
Classic
1M ( n=85 )
128K ( n=236 )
( n=24 )
Model calls
0
0
24
Latency
613 ms
346 ms
101 s
Context before
242K
121K
908K
Context after
64K
39K
58K
Compression
7.3 ×
7.0 ×
–
Table 4: Folds of the main 1 runs and compactions of Piki-Classic. Latency, context sizes, and ratios are medians. Compression is the folded source history over the rendered outline.