Keep a long codebase exploration grounded
Pick the technique (subagent, scratchpad file, phase summary, manifest or /compact) that fixes a sign of context degradation, and order the phases of a long codebase exploration.
Scenario
Scenario: Developer Productivity with Claude. Your team built an onboarding agent on the Agent SDK. It helps new engineers find their way around a 15-year-old insurance claims system: about 2,000 Java files, with Read, Grep, Glob and Bash.
Twenty minutes into a session it found ClaimStateMachine, the class that moves a claim from "submitted" to "approved". Two hours later, asked the same question, it answers: "Typically a service layer updates the status through a repository."
Nothing crashed. The context just filled up with everything it had read.
Explanation
The sign to watch for. Claude's context window holds every message, file and command output. Performance degrades as it fills: the agent starts giving answers that contradict earlier ones, and describes "typical patterns" instead of the specific classes it found. That is context degradation, and a bigger window only delays it.
Five techniques keep a long exploration grounded. Each fixes a different problem.
| Problem | Technique |
|---|---|
| One question will produce a flood of output (every caller, a whole flow) | Subagent: it explores in its own context and returns only its final message |
| Findings from early on get blurred | Scratchpad file: the agent writes key findings to a file and reads it before later answers |
| The next phase re-explores what the last one found | Phase summary: summarize the phase's findings and put them in the next subagents' prompts |
| A crash means starting over | Manifest: each agent exports its state to a known location; on resume the coordinator loads the manifest and injects that state into the agents' prompts |
| The context is full of verbose discovery output, and the work continues | /compact: summarizes older history to free space |
Four details matter:
- A subagent knows only its prompt. It starts with a fresh context, so the prompt must hold the question, the file paths and the decisions it needs. Give it a specific question ("trace the dependencies of the claim approval flow"), not "look around".
- The scratchpad holds facts, not prose. Class names, file paths, decisions and open questions. It lives outside the context window, so it survives both degradation and compaction.
- Compaction is lossy. It replaces older messages with a summary, and specific early details may not survive. Put rules that must persist in CLAUDE.md, and tell the compactor there what to preserve.
- Manifests are structured. JSON with a status per unit of work is easier for a coordinator to load, and harder for a model to overwrite by accident, than free-form notes.
Worked example
An engineer asks the onboarding agent: "Explain how a claim gets approved, and what touches the payout tables."
Before: one session reads the claims module, then payments, then 40 test files, then the scheduler, all in the main context. By the payout question the answers are generic.
After: the agent works in phases and keeps state outside its context.
- It creates
notes/claims-approval.mdwith the two questions. - A mapping subagent lists the entry points and module boundaries. The agent writes the key findings to the notes:
ClaimStateMachine,ApprovalRules,PayoutWriter. - It starts two tracing subagents (approval flow, payout writes), each with that summary in its prompt, so neither re-explores the map.
- Each subagent's state goes into a manifest the coordinator can reload:
{
"task": "claims-approval",
"notes": "notes/claims-approval.md",
"agents": [
{ "id": "map", "status": "done" },
{ "id": "approval-flow", "status": "done" },
{
"id": "payout-writes",
"status": "in_progress",
"state": "state/payout-writes.json"
}
]
}
- Later questions are answered from the notes, which name the exact classes, not from what the session still remembers.