5.4 Context management in large codebase exploration
Context degradation
In extended sessions the model starts giving inconsistent answers and referencing "typical patterns" rather than the specific classes it discovered earlier. That symptom — generic language replacing specific names — is the tell.
The toolkit
| Technique | Use |
|---|---|
| Subagent delegation | Isolate verbose exploration ("find all test files", "trace refund flow dependencies") while the main agent keeps high-level coordination |
| Scratchpad files | Persist key findings across context boundaries; reference the file for later questions |
| Phase summaries | Summarise findings from one exploration phase before spawning subagents for the next; inject the summary into their initial context |
/compact (slash commands, context editing) | Reduce context usage when the session fills with verbose discovery output |
| Structured state exports (manifests) | Crash recovery: each agent exports state to a known location; the coordinator loads a manifest on resume and injects it into agent prompts |
.agent-state/
├── manifest.json # phase, completed agents, artefact index
├── structure-map.md # phase 1 output
├── refund-flow.md # subagent output
└── test-inventory.json # subagent output
{
"phase": "coverage-analysis",
"completed": ["structure-map", "refund-flow", "test-inventory"],
"pending": ["prioritised-test-plan"],
"artefacts": {
"structure-map": ".agent-state/structure-map.md",
"refund-flow": ".agent-state/refund-flow.md"
}
}
Quiz
- Question 1
Three hours into exploring an unfamiliar 200k-line service, the agent starts describing "the typical repository pattern" instead of the concrete OrderRepo class it identified an hour earlier, and gives two different answers about where transactions are committed. Earlier answers in the same session were specific and correct. What is the right response?
- Question 2
Before a design discussion you need four facts from a large repository: where retry policy is configured, which modules write to the audit table, whether any test exercises the refund path, and what the migration numbering convention is. Each answer requires reading dozens of files. The design discussion needs a clean main session. How do you gather them?
- Question 3
A four-phase exploration spawns fresh subagents in each phase. By phase three the subagents are rediscovering facts that were established in phase one, because each of them starts from the original task description and nothing else. What should the coordinator do between phases?
- Question 4
An exploration session is at about 80% of its context, almost all of it verbose discovery output. The remaining work is discussion of what has already been found, with no further large reads planned, and the key findings are already recorded in a scratchpad file. What is the appropriate action?
Links
- https://code.claude.com/docs/en/context-window
- https://code.claude.com/docs/en/sub-agents
- https://code.claude.com/docs/en/agent-sdk/session-storage
- https://code.claude.com/docs/en/checkpointing
- https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents
- https://en.wikipedia.org/wiki/Scratchpad_memory