5.4 Manage context effectively in large codebase exploration
Counter context degradation in long codebase-exploration sessions with scratchpad files, subagent delegation of verbose work, phase summaries injected into the next phase, `/compact` with focus instructions, and structured state exports for crash recovery.
Key points
- 1
Symptoms of context degradation in extended sessions: inconsistent answers to the same question, and references to "typical patterns" instead of the specific classes and flows discovered earlier. It is a context problem, not a temperature or model-size problem.
- 2
Scratchpad files (for example a notes or findings file) persist key findings across context boundaries. Have the agent record class names, flows and decisions as it discovers them and reference the file for subsequent questions.
- 3
Delegate verbose exploration ("find all test files", "trace refund flow dependencies", running a noisy test suite) to subagents. Each subagent runs in its own context window; only a condensed summary returns, so the main agent preserves high-level coordination.
- 4
Subagents start with a fresh, isolated context and do not see the parent conversation. Anything they need from earlier work must be passed explicitly in their task prompt or read from a file.
- 5
Between exploration phases, summarise the key findings of the finished phase and inject that summary into the initial context of the next phase's subagents. Passing raw transcripts instead is noisy and reintroduces lost-in-the-middle problems.
- 6
/compactreduces context by summarising the conversation, and accepts custom instructions (/compact Preserve the module map and open questions; drop test output) that tell Claude what to keep. Compact instructions can also be set inCLAUDE.md./cleardiscards the conversation entirely and is for switching to unrelated work. - 7
Auto-compaction happens when the window nears its limit, but without guidance it may not prioritise the specific findings you care about; persist them to a scratchpad before it fires.
- 8
CLAUDE.mdis for stable project instructions loaded every session; do not paste transcripts or class lists into it. Keep it concise and move workflow-specific detail into skills that load on demand. - 9
Crash recovery for long multi-agent jobs: each agent exports structured state (completed items, findings, open questions) to a known location, the coordinator maintains a manifest, and on resume it loads the manifest and injects each agent's saved state into its prompt. Replaying raw tool logs or relying on in-memory context does not achieve this.
- 10
Anthropic's research system saves the lead agent's plan to memory before the context is truncated and has subagents write outputs to external storage, passing lightweight references rather than copying large outputs through the conversation.
Read the source
Test yourself on 5.4 Manage context effectively in large codebase exploration
Ten questions, with the answer and explanation after each one.