Flows category icon
Memory & Context
Advanced

Implement Context Compaction

Keep long agent sessions alive by compacting old conversation into structured summaries before the context window fills.

4 steps12 verify checks90-120 minutesWorks with: emergent · chatgpt · claude · cursor
Start Guided Walkthrough

The Route

0/4 verified

Context Pack

Paste this first. It briefs the AI on requirements, constraints, and the Definition of Done before your first build prompt.

Context Pack
PROJECT CONTEXT:
My agent dies or degrades on long tasks as the context window fills. Analyses of production harnesses show the fix: monitor usage, and at a threshold compact old turns into a structured summary that preserves the load-bearing facts.

GOAL:
Implement threshold-triggered compaction: measure context usage, summarize old turns into a structured digest, and continue the session seamlessly.

REQUIREMENTS:
- Continuous token accounting for the assembled context
- A compaction trigger at a configurable threshold (default ~75% of window)
- A structured summary format: task state, decisions, file/tool facts, open items
- Recent turns kept verbatim; only older turns are compacted
- Post-compaction continuity: the agent resumes work without re-asking or re-doing

CONSTRAINTS:
- The summary is produced by a dedicated summarization call with its own prompt, not ad hoc
- Critical anchors (original task, active plan, key constraints) must never be summarized away

DEFINITION OF DONE:
- A session exceeding the window completes via at least one compaction
- Post-compaction, the agent correctly answers questions about pre-compaction decisions
- Token accounting logs show usage dropping after compaction and staying under the window
- The original task statement and active plan survive verbatim

COMMON FAILURES TO AVOID:
- Compacting everything including the task itself, leaving an agent with amnesia
- Triggering at 100% so compaction itself fails for lack of room
- Freeform prose summaries that drop file paths, decisions, and constraints
- Summarizing tool results the agent still needs at full fidelity

Paste this into your AI builder first. It teaches the AI what you want before you give it the build prompt.

Related routes

More Memory & Context flows that share ground with this one.

Flows category icon
Memory & Context

Session Summaries That Survive Compaction

Write durable session summaries at the right moments so knowledge outlives compaction and process restarts.

01Define what deserves to survive
02Write at the moments that matter
03Load summaries into new sessions

+1 more steps to Done

Best forbuilders who have shipped a basic app before

4 steps60-90 minutesIntermediate
Flows category icon
Memory & Context

Add File-Based Persistent Memory

Give your agent durable memory with plain files - the radical-minimalism approach production practitioners keep converging on.

01Design the memory file structure
02Load memory at session start
03Build the guarded memory_update tool

+1 more steps to Done

Best foryour first pass at this - no prior setup assumed

4 steps45-75 minutesBeginner
Flows category icon
Memory & Context

Cross-Session Project State

Keep long-horizon work coherent across many sessions: externalized project state that any session can load, advance, and hand off.

01Design the project-state document
02Implement the session-start protocol
03Implement the session-end advance

+1 more steps to Done

Best forproduction-grade builds with strict verification

4 steps90-120 minutesAdvanced

Made with Emergent