---
title: A long-running agent degrades after about 100 steps as its context fills. How do you manage context and memory?
url: https://gurkanozkan.ai/learn/questions/applied-llm-agt-05/
markdown: https://gurkanozkan.ai/learn/questions/applied-llm-agt-05.md
kind: question
role: Applied LLM
stage: Technical
difficulty: Hard
json: https://gurkanozkan.ai/api/learn/questions/applied-llm-agt-05.json
robots: noindex
---

# A long-running agent degrades after about 100 steps as its context fills. How do you manage context and memory?

Applied LLM · Technical · Hard

Theme reported publicly: [PolyAI](https://job-boards.eu.greenhouse.io/polyai/jobs/5036991101), [Anthropic](https://www.aced.io/guides/anthropic-forward-deployed-engineer-interview)

## 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.

*Corrected by an independent AI reviewer.*

## 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

## Topic guides

- [Agents and tool use](https://gurkanozkan.ai/learn/topics/agents.md)
- [Long context and context management](https://gurkanozkan.ai/learn/topics/long-context.md)
---

*Study material compiled with AI: drafted by a local model (Inkling 2 Mini) from public sources, then checked against those sources by independent AI reviewers (Claude) in November 2026. Not reviewed line by line by a person.*
