arXiv2026Preprint, arXiv 2610.07863

ReFold

Training-Free Reversible Inter-Turn Context Folding for Long-Horizon Agents

Repeated output becomes a stub and finished steps fold into one-line notes. No extra model calls, no training, and anything removed can be restored.

1University of California, Santa Barbara 2Intel

Demo

Method

The agent's history is never edited. Before every model call, ReFold renders a shorter prompt from it, with no extra model call and nothing lost for good.

Turn 1 of 10

  • Repeat → stubA tool output that an earlier turn still shows becomes a pointer to that turn.
  • Done → noteEach reply names the turns it no longer needs (STALE). They fold into one line.
  • One boundaryBoth act only behind btb_t, which moves every k=3k = 3 turns. In between, the prompt only grows at its end, so its prefix stays cached.
  • Nothing lostrestore <step> brings any removed turn back from the history, without running it again.
  • Reused from the prefix cache
  • Computed at this call
  • Turn still to come
A toy session of ten turns with k=3k = 3, after Figure 3 of the paper. Each card is one turn: the agent's reply and the tool's output. Click a turn in the history to jump to it.
How each part works, with examples from a real run
Deduplicate

Repeated output becomes a stub

When a file range, a search result or a command output repeats one that an earlier turn still shows, the later copy becomes a stub that points to that turn. For a long span the stub keeps the top-level class and function signatures, so the agent can still find its way around.

A stub from the recorded run below.
Fold

Finished turns fold into a one-line note

Every reply starts with two short lines: PROGRESS says what the turn does, STALE names earlier turns whose output the agent will not read again. They come with the normal reply, so there is no extra model call.

𝐚t=PROGRESSt⊕⟨STALEt⟩⊕𝐚˜t\mat{a}_t = \sc{PROGRESS}_t \oplus \langle\sc{STALE}_t\rangle \oplus \tilde{\mat{a}}_t St=St−1∪(STALEt∩{t−k,…,t−1})\class{c-fold}{S}_t = \class{c-fold}{S}_{t-1} \cup (\sc{STALE}_t \cap \{t{-}k, \dots, t{-}1\})

StS_t starts empty and only takes turns from the last kk. Behind the boundary, a turn in it leaves the prompt, reply and output together; adjacent folded turns share one note.

A note from the recorded run below.
Chunked rendering

The cached prefix changes once every 𝒌\boldsymbol{k} turns

Both operators act only behind a boundary btb_t that moves every k=3k = 3 turns; later turns are sent as received. Between two moves the prompt only grows at its end, so the server keeps reusing its cache.

bt=max⁡(0,k⌊t−kk⌋)\class{c-chunk}{b}_t = \max\!\left(0,\,k\!\left\lfloor\frac{t-k}{k}\right\rfloor\right)

The prompt at step tt

𝐜t=𝐩sys⊕𝐩task⊕⨁i=1t{fiif i≤bt and i∈St𝐚i⊕𝐨iif i>bt𝐚i⊕diotherwise\mat{c}_t = \mat{p}_{\text{sys}} \oplus \mat{p}_{\text{task}} \oplus \bigoplus_{i=1}^{t} \begin{cases} \class{c-fold}{f}_i & \text{if } i \le \class{c-chunk}{b}_t \text{ and } i \in \class{c-fold}{S}_t \\ \mat{a}_i \oplus \mat{o}_i & \text{if } i > \class{c-chunk}{b}_t \\ \mat{a}_i \oplus \class{c-dedup}{d}_i & \text{otherwise} \end{cases} 𝐜t=𝐩sys⊕𝐩task⊕⨁i=1t{fiif i≤bt and i∈St𝐚i⊕𝐨iif i>bt𝐚i⊕diotherwise\begin{aligned} \mat{c}_t ={}& \mat{p}_{\text{sys}} \oplus \mat{p}_{\text{task}} \\ &{}\oplus \bigoplus_{i=1}^{t} \begin{cases} \class{c-fold}{f}_i & \text{if } i \le \class{c-chunk}{b}_t \text{ and } i \in \class{c-fold}{S}_t \\ \mat{a}_i \oplus \mat{o}_i & \text{if } i > \class{c-chunk}{b}_t \\ \mat{a}_i \oplus \class{c-dedup}{d}_i & \text{otherwise} \end{cases} \end{aligned}

fif_i is the note of a folded turn, did_i the output with its repeats replaced by stubs, 𝐚i⊕𝐨i\mat{a}_i \oplus \mat{o}_i the turn as received.

Prompt tokens that hit the prefix cache

One recorded trajectory re-rendered with chunking on and off (Figure 4c of the paper).
Restore

Anything removed can come back

Every stub and note names the turns it replaces. restore <step> is answered from the history: the original reply, command and output are appended at the end of the prompt, nothing runs again, and the cached prefix stays as it is.

What happens to tool-output tokens

  • kept in the prompt
  • removed, can come back
  • removed, lost
With reversibility off, removed output only comes back by re-reading an unchanged file, and about a third of all tool-output tokens are lost, as many as compact loses (Figure 4c).

Results

ReFold in mini-swe-agent with Qwen3.6-27B and Qwen3.6-35B-A3B on vLLM, five benchmarks of 50 tasks each, against base, which keeps the full history.

Tokens

−48%

mean per task

Peak KV cache

−52%

median per session

Server KV-cache use

2× sessions

on the same memory

Cost and duration

3.4× cheaper1.7× faster

per task, at 48 and 16 sessions

  • base
  • ReFold
The savings of one session carry to the server (Figure 1 of the paper). Qwen3.6-27B on SWE-bench Verified: (a, b) the 50 tasks over two runs, the dashed lines at the mean tokens and the median KV cache; (c, d) one vLLM server with ww concurrent sessions (Table 2). At w=32w = 32 ReFold needs the KV cache that base needs at 16.
Five benchmarks, two models

Fewer tokens everywhere, the same tasks solved

18–60%fewer tokens per task, in all ten settings
30–58%less peak KV cache per session
−1 to +3tasks resolved, out of 50
Model
Change against base

Full 256k context window, 8 concurrent sessions, 50 tasks per benchmark (Table 1).

More results

Under concurrent load

Qwen3.6-27B on SWE-bench Verified with more sessions on the same server (Table 2). base sessions compete for KV-cache memory and stall; at 48 sessions the mean queuing delay per request falls from 8.96 s to 0.02 s with ReFold.

Tasks resolved

out of 50

Cost per task

cents, with the cached-input discount

Duration per task

median minutes

  • ReFold
  • base
  • x-axis: concurrent sessions ww

Under a capped context budget

The compact baseline summarizes the history whenever the prompt passes 75% of the budget. With ReFold underneath the prompt grows more slowly: compactions fall by 92% at 32k, and cost by 24–36% at 24k–64k (Table 2).

Compactions per task

Qwen3.6-27B, SWE-bench Verified, 8 sessions

Cost per task

cents, same runs

  • compact
  • compact + ReFold

Overhead inside the agent loop

Context-management work per step, Qwen3.6-27B on SWE-bench Verified (Figure 4a). ReFold renders in 0.4 ms; compact waits 3.3 s for its summary calls.

Overhead per step

log scale

Table 1 in numbers

A real run

One SWE-bench Verified task, replayed call by call. Play it, jump to a moment, or switch a mechanism off.

SWE-bench Verifiedinstance
Recorded run

Where this run comes from

Mechanisms
Jump to
Historyeverything so far, never edited
0 tokens
Sent to the modelthe prompt at this call
0 tokens

  • Sent as received
  • Stub or trimmed output
  • Note for folded steps
  • Restored step
  • In the history only

Click a row to read it.

The model's reply
Against base, which sends the full history

Prompt size at each call

Bars: what ReFold sends, split into the cached prefix and the recomputed part. Line: base. Click a bar to jump there.

  • ReFold, cached prefix
  • ReFold, recomputed
  • base

Prompt tokens so far

The running total of the left chart. The gap between the lines is the token bill ReFold saves.

  • ReFold
  • base

Code

ReFold is a plugin for mini-swe-agent: a rendering hook before every model call and a few lines of prompt. The agent's tools and loop stay the same. The code and the scripts behind every table and figure are on GitHub.

YupengSu/ReFold
The prompt lines ReFold adds
Replaces the first item of the response format
Begin every response with these two lines, then your reasoning and your bash tool call:
  PROGRESS: <one short line on what THIS step does>
  STALE: <step numbers whose output you will not read again, or `-` if none>
Each earlier tool output is labeled `[step N]`.
…
Only the last 3 steps before this one can still be named; older numbers are ignored, so saving up a big cleanup for later is wasted.
Appended to the system prompt
Context management: to save tokens, the harness omits parts of earlier tool outputs that already appear elsewhere and folds steps you have marked STALE. Every omission is marked by a `[context-manager]` note, which is NOT a shell error: it says what was omitted and where the same content still is. The bash command `restore <step>` shows a step's output again exactly as first shown, without running anything; re-running the original command shows its current state instead. …

From Appendix C.2 of the paper, for k=3k = 3. Every tool output is also prefixed with its step number, and a reply without the two lines gets a reminder.

Citation

BibTeX
@article{su2026refold,
  title   = {{ReFold}: Training-Free Reversible Inter-Turn Context Folding
             for Long-Horizon Agents},
  author  = {Su, Yupeng and Tian, Jiayi and Zhang, Zheng and Kundu, Souvik},
  journal = {arXiv preprint arXiv:2610.07863},
  year    = {2026}
}