A long-running agent degrades after about 100 steps as its context fills. How do you manage context and memory?
Answer
Model answer, written in a candidate's voice
In short
- Treat context as a budget: compact before degradation, preserving goals, decisions, identifiers, constraints; drop stale tool outputs
- Keep external memory: notes/to-do files written and re-read just-in-time, not preloaded
- Use sub-agents with fresh contexts that return condensed results
- Stay prompt-cache aware: never-edited stable prefix, append-only history between compactions, compaction only at deliberate checkpoints because each one invalidates the cached history
- Measure task success against trajectory length (e.g. at 50, 100, 200 steps) to confirm the degradation is gone
- After compaction, verify constraints, identifiers, and open tasks survive before continuing
In full
I’d treat context as a finite budget rather than a transcript to keep forever. Before the run degrades, I compact: summarise the goal, decisions, identifiers, constraints, and open tasks, and drop stale tool outputs while keeping the few artefacts needed to resume. I also keep external memory—notes and to-do files written during the run and re-read just in time—so the agent does not preload everything and can pull only the relevant slice. For broad exploration I use sub-agents with fresh contexts that return condensed results, not raw logs. I’m prompt-cache aware: the system prompt and tool definitions form a stable prefix that is never edited, history stays append-only between compactions, and I compact at deliberate checkpoints rather than every turn, because each compaction invalidates the cached history after that prefix. Finally I measure task success as a function of trajectory length (for example at 50, 100 and 200 steps) to confirm the degradation is gone, and after every compaction I verify that goals, identifiers, constraints, and unfinished work still survive before continuing.
What a strong answer covers
- Compaction: summarise history while preserving goals, decisions, identifiers; drop stale tool outputs
- External memory: notes/to-do files written and re-read; just-in-time retrieval rather than preloading
- Sub-agents with fresh contexts returning condensed results
- Prompt-cache awareness (append-only history keeps cache hits)
- Evaluates success vs trajectory length; checks that constraints survive compaction
Common mistakes
- Truncating the oldest messages (dropping the original goal)
- summaries that lose file paths and IDs