Compress & Restart

When conversations grow long, summarize what matters and start fresh. Context quality degrades well before hitting advertised limits.

The Problem This Solves

Old tool results accumulate alongside completed work. In long-running conversations and agent loops, the model keeps rereading that history even after a result has stopped being relevant, competing with the evidence it needs for the current task.

Quality can degrade before the window fills, especially when old observations compete with instructions that still apply to the next action. Waiting for the API to reject the request leaves no room to prepare a useful restart.

How It Works

Before the context fills, prepare a compact continuation state and validate it against the current task. Resume with that state and the evidence needed for the next action, while keeping the original history recoverable outside the active window.

The compression cycle:

  1. Detect: Monitor context growth and reserve room for the compaction request, its output, and pending tool results.
  2. Summarize: Preserve the current goal, decisions, and their supporting evidence. Carry forward constraints and unresolved questions without promoting proposed work to completed work.
  3. Validate: Check that the summary preserves what the next action depends on. Validate Compression describes this check; a fluent recap can still omit a critical constraint.
  4. Restart: Begin a new context with the validated state and the specific artifacts needed to continue. If work continued during summarization, retain the messages after the summary’s snapshot boundary.
  5. Preserve: Remove old material from the active window, but retain the original evidence under the application’s retention and access rules.

Use explicit fields for task state. A field named constraints makes omissions easier to spot, but only a check against the live task can establish whether it still contains the requirement that the next action depends on. Context Compaction in Production covers asynchronous checks, prompt caching, and preserving instruction provenance.

Example

During a debugging session, the agent explores several hypotheses before settling on a likely race condition in the upload queue. The restart needs to carry that hypothesis and its evidence forward without presenting it as a proven diagnosis.

Compressed restart:

State

  • Task: Fix race condition in upload queue (issue #847)
  • Leading hypothesis: processQueue() in worker.ts reads shared state without locking
  • Evidence: Concurrent-upload test fails intermittently at worker.ts:142; captured output: artifacts/upload-race.log
  • Failed approaches: Adding retry logic (masks the bug), mutex on the full queue (deadlocks with cleanup worker)
  • Next step: Implement per-item locking using the existing Redis lock utility
  • Relevant files: worker.ts, redis-lock.ts, upload-queue.test.ts

Keep the test output with the revision it checked, and retain links to the original evidence outside the window so the agent can recheck the diagnosis if the proposed fix fails. The restart should expose what is known without hiding what still needs testing.

When to Use

  • Long-running agent loops (debugging, research, multi-step generation)
  • Conversations that span many turns on the same topic
  • Anytime you notice output quality degrading mid-session

When Not to Use

  • Short, focused tasks that complete well within the context window
  • When Isolate can prevent history growth by giving independent subtasks separate contexts
  • When compression itself would lose critical nuance (complex negotiations, subtle reasoning chains)