Module 2 · Tools, Knowledge, Memory & Research Design · scripted

Compaction: The Handoff to Yourself

30 minoutcomes: context-engineering, agent-architectures

The wall approaches

There is a problem we have been walking toward for a while now, and a long-running agent eventually walks into it: the conversation outgrows the wall. Every iteration appends; nothing leaves. Watch the numbers on a real trajectory:

Iteration 41 of a long-running agent

iteration 1 wall:1,400 words next result: fits easily
iteration 12 wall:19,000 words next result: fits
iteration 27 wall:61,000 words next result: fits, barely
iteration 41 wall:118,000 words next result: WILL NOT FIT

…and the recurrence has been billing that growing wall on every single iteration along the way.

Two different limits can stop the loop, and it matters which one you design for:

The two limits

THE HARD LIMIT    the window's edge. The next result physically
                  cannot land on the wall. No choice remains.

THE CHOSEN LIMIT  a budget: "never pay for more than N words per
                  iteration." A line you draw far inside the
                  edge — crossed long before the wall is full.

Well-designed agents almost never hit the hard limit,
because they act at the chosen one.

So we must remove something. The question is what — and it is a far deeper question than it looks.

The wrong scissors

The obvious move: the conversation is too long, so delete the oldest messages. They're the stalest — surely they're the safest to cut? Watch what happens:

Example 1 · Cutting from the top
Contexttruncated: the 14 oldest messages were dropped to make room
user

[earlier messages removed]

user
⟨tool_result⟩
Booking search complete: SAS 07:15 Molde–Oslo–Trondheim, 2,890 kr,
non-refundable. Widerøe 09:40 direct, 4,120 kr, refundable.
assistant
⟨tool_use⟩
book_flight(option = "SAS 07:15", fare = "non-refundable")
user
⟨tool_result⟩
Booked: SAS 07:15, non-refundable, 2,890 kr.
assistant

I've booked the cheaper SAS option — 2,890 kr, saving 1,230 kr against the direct flight.

NoteCompetent, efficient — and wrong. The first message of this conversation, hours ago, said: "the dates might move — whatever you book must be refundable." That constraint lived exactly where the scissors cut. The oldest messages aren't the stalest; they're the stage-setting — the goal, the constraints, the reason anyone is on stage at all. Cut Act 1, and the actors play Act 3 flawlessly in the wrong play.

Age is simply the wrong axis. Some of the oldest words are the most load-bearing; some of the newest are already worthless. We need to cut along a different axis entirely — and finding it requires a small revelation about what "efficient" even means.

Efficiency can only be measured from here

Here is the revelation. Take one conversation and judge it twice — once in the middle of its work, once near the end:

The same wall, judged at two moments

The conversation contains, among other things:
the goal and constraints(turn 1)
subtask A: 30 turns of extraction — attempts, errors,
corrections — ending in a clean 12-row table
subtask B: 9 turns of checking, ending in "all rows valid"
the open work: write the final report from the tables
JUDGED AT ITERATION 15 (inside subtask A):
the extraction attempts are SIGNAL — the agent is actively
using them to converge. Efficiency: high.
JUDGED AT ITERATION 41 (subtasks done, report remaining):
the same 39 turns are NOISE — the report needs the two
results, not the roads that led to them. Efficiency: awful.

Not one word on the wall changed. The DESTINATION changed.

The principle

Signal-to-noise is not a property of the conversation.
It is a relation between the conversation and the work
that REMAINS.

The value of a word on the wall is not what it did —
it is what it still does for what is left.

This explains the strange feeling every long conversation produces: it was efficient the whole way along, and yet it is inefficient now. Both are true. Every iteration of the loop moves the destination closer, and every step quietly re-prices every word behind it. The scaffolding around a finished wall was load-bearing during construction — that doesn't make it part of the building.

So run the thought experiment that turns this into a mechanism: if a fresh agent had to start right now and finish only the remaining work — what would you write as its opening prompt? Not the transcript. Nobody briefs a newcomer by reading them the minutes. You'd write the goal, the current state, what was decided and why, what remains. That document — short, forward-facing — is the ideal conversation for this exact moment. And it is not the history.

The core idea

The best prompt for finishing a task
is not the history of the task.

Compaction is how we trade the one for the other.

The shift change

Humans solved this problem long ago, in places where the work never stops but the workers must. At 07:00 in every hospital, the night nurse hands over to the day nurse — and does not replay the shift:

Two ways to hand over a shift

THE TRANSCRIPTTHE HANDOFF REPORT
"23:47, walked to room 4,"Room 4: fever down since 03:00,
checked the drip, adjusteddrip at reduced rate — recheck
the rate, back at the deskat 10:00.
00:15, phone rang, wrongRoom 7: new admission, stable,
number, 00:31 checked roombloods pending.
4 again, fever unchanged,Watch: room 4 spikes again →
01:10 charted, 01:55 room 7call Dr. Berg directly."
admission began, paperwork…"— organized by what the NEXT
— eight hours, in order,eight hours need. Short
signal and noise entangled.because it looks forward.

The handoff report is compaction, performed by professionals every twelve hours. And the move we're about to make is exactly this — with one twist that makes it stranger and simpler at once: the agent is handing off to itself. Same task, same seat — but the incoming worker gets a briefing instead of a transcript.

The compaction call

Now the mechanism, and notice that it's built entirely of parts we already own. Compaction is self-dialogue pointed at the conversation itself: the whole history goes into a disposable exchange, a summary comes back, the loop restarts on it. Here is the prompt that does it — read it closely, because every line is a design decision:

The compaction prompt

"Below is the full conversation of an agent mid-task. Rewrite it as a briefing for an agent that must COMPLETE the task, in at most 800 words. Preserve exactly:

the original goal and every user-stated constraint, verbatim
current state: what is done, what remains
decisions made, each with its one-line reason
rules learned along the way (tool requirements, formats,
protocols discovered mid-task)
open questions and loose ends

Collapse each COMPLETED subtask to one line: what was done → its result. DROP: intermediate attempts, corrected errors, superseded drafts, and the contents of tool results whose conclusions are already captured above."

That keep-list is where the judgment lives — it is the axis the scissors should have cut along all along: not old versus new but still-load-bearing versus scaffolding. Watch it run:

Example 2 · The handoff, performed
Contextiteration 41wall 118,000 wordschosen limit crossed
assistant
⟨tool_use⟩
compact_conversation(max_words = 800)
user
⟨tool_result⟩
BRIEFING (replacing 41 iterations):
GOAL: reconcile the department's Q3 travel expenses; constraint
(user, verbatim): "flag anything over policy rather than fixing it
silently."
DONE: (1) 34 receipts extracted → table T1 (12 flagged unusual).
(2) Policy check complete → 9 within policy; 3 over-limit: rows
4, 11, 29 of T1. (3) Rule learned: log_expense requires ISO
currency codes and absolute dates.
REMAINING: draft the summary report; flag rows 4, 11, 29 with
amounts and policy clauses; await user decision on the flagged
three.
OPEN: row 29's receipt was illegible — amount is OCR's best guess.
NoteForty-one iterations became one page. The three subtask results survived; their hundred intermediate steps did not. The learned rule survived — tomorrow's calls still need it. The verbatim constraint survived — it governs the report that hasn't been written yet. And the one honest uncertainty (row 29) survived, because the next decision depends on it.
user

[new loop, iteration 1 — the briefing above is the conversation] Continue the task.

assistant
⟨tool_use⟩
draft_report(source = "T1", flag_rows = [4, 11, 29])
NoteThe loop restarts light: the next iteration pays for one page, not 118,000 words — and pays that smaller bill every iteration from here on. One expensive summarization purchased a permanently cheaper loop.
Read the compaction prompt like a designer
  • Which line of the keep-list protects against Example 1's disaster?
  • "Collapse each completed subtask to one line" — when is that rule wrong? Find a case where an intermediate step still matters after its subtask succeeded.
  • Who wrote this compaction prompt? Who should write it — the agent's designer, or the agent?

The archive: nothing lost, merely not loaded

Compaction is judgment, and judgment is fallible: the summarizer cannot perfectly predict what the future will need. Row 29's uncertainty made the briefing — but what about the vendor name the summary dropped, the one the user suddenly asks about? The answer is a pattern you already know from designing results: don't delete the past — archive it behind a handle, and say so on the wall:

The compaction marker — a result that instructs

[COMPACTED: 41 iterations (118,000 words) archived as conv-88. The briefing above summarizes them. If you need a detail it omits: search_history(conv, pattern) · read_turns(conv, from, to) · ask_history(conv, question) are available.]

The conversation's own past has become a document among documents — and the whole retrieval ladder applies to it: search it, page it, ask it.

Example 3 · Reaching into the archive
user

Wait — which hotel was row 11? My department head is asking.

assistant

The briefing flags row 11 as over-limit but doesn't name the vendor. That detail is in the archived conversation.

assistant
⟨tool_use⟩
ask_history(conv = "conv-88", question = "For expense row 11 of
table T1: vendor name, amount, and dates?")
user
⟨tool_result⟩
Row 11: Hotel Union Geiranger, 6,840 kr, 12–14 July (extracted at
iteration 19).
assistant

Row 11 is Hotel Union Geiranger — 6,840 kr for two nights in July, which is what put it over the lodging limit.

NoteThe summary was wrong to drop this detail — and it didn't matter. Fallible compaction plus a searchable archive beats perfect compaction, which does not exist. The agent forgot the way you forget things filed in a cabinet: not gone, just not carried.

The price of rewriting

One honest complication. The caching lesson taught append-never- rewrite: identical prefixes are cheap, so never edit history. Compaction deliberately breaks that rule — the new briefing shares no prefix with the old wall, so the first iteration after compacting pays full price, plus the cost of the summarization itself. This is a real trade, not a free lunch:

Two cost curves

KEEP APPENDINGevery iteration re-bills the whole growing wall — cheap per word (cached), but the wall compounds. The bill curves upward, forever.
COMPACT NOWpay a one-time toll: the summarization call + one cold, uncached iteration. Then every later iteration bills one page. The bill drops and stays flat.

The curves cross. Before the crossing, compaction is premature. After it, every iteration without compacting is a small overpay — and the crossing is exactly where the CHOSEN limit belongs.

This is why the chosen limit from the start of the lesson isn't timidity — it's arithmetic. You compact when the toll beats the compounding, not when the wall forces your hand.

What must survive — and what may safely die

Look once more at the keep-list's fourth line: rules learned along the way. The conversation spent real money learning them — an error taught the ISO-code rule; a manual taught the calibration protocol. Compact carelessly and the agent forgets — and re-errs, and re-pays.

But notice what we built earlier, and how it catches this exact fall: if the tools teach their rules at the moment of violation, then a forgotten rule costs one corrected error, not a derailed task. The two designs are partners:

A symbiosis

Instructive errors make forgetting SURVIVABLE —
  the world re-teaches any rule the summary dropped.

Compaction makes remembering AFFORDABLE —
  the wall stays small enough to keep running.

Design both, and the conversation can stay light precisely
because the world around it is instructive.
The forgetting audit
  • Rank the five keep-list items by the cost of losing each. Which loss is caught cheaply by an instructive error — and which one is caught by nothing at all?
  • A conversation's tone — caution learned from a near-miss, the user's irritation at verbosity — rarely survives a briefing. Does that matter? When?

The trajectory, unburdened

End where the threads meet. A conversation is a trajectory — but a trajectory is a path, not a cargo hold. Its obligation is to reach the destination, not to carry every step that came before. The agent cannot see its own wall, so the loop's designer manages it on the agent's behalf: a chosen limit, a compaction prompt whose keep-list encodes what "still matters" means for this task, a marker that instructs, and an archive with handles. The conversation becomes, at intervals, its own editor — pausing to write the briefing it wishes it had, and then becoming it.

The one-liner

A trajectory is not obliged to carry its whole past.
It is obliged to reach its destination.
Your turn

Take the longest agent transcript you have — or one we provide. (1) Draw the cut line where you would compact, and defend it with the two-limits arithmetic. (2) Write the compaction prompt for this task: your keep-list, your drop-list, your word budget. (3) Produce the briefing and restart the task from it. (4) Audit the run: what did the compacted agent do differently, what did it reach into the archive for — and what got lost that neither the briefing nor the archive brought back?