Context management in large codebase exploration
What this covers
- Recognize context degradation once it shows as generic patterns replacing earlier specifics
- Re-verify the conclusions drawn before the degradation was noticed
- Use scratchpad files to persist findings across context boundaries
- Design crash recovery with structured state exports
- Inject recovered state into each resumed agent, because exporting it is only half
- Delegate verbose exploration so only its answer enters the main context
- Write a phase handoff with exact findings, evidence locations, decisions, and open questions
Key terms
- Command line (CLI)
- A text-based way to run programs and work with files.
- Subagent
- A separate agent given a limited task and its own context.
Signs that the context is overloaded
An extended session doesn't fail loudly. It starts giving inconsistent answers, and — the tell worth memorizing — it begins referring to "typical patterns" instead of the specific classes it discovered earlier.
That shift from the particular to the generic is the symptom. By the time you notice it, conclusions drawn in the preceding turns are suspect.
Someone who gives directions by landmark — second left after the gas station — knows the route. Someone who says it is somewhere on the north side is no longer remembering; they are reconstructing. Confidence sounds the same in both.
Scratchpad files
Have agents record key findings in a scratchpad file and reference it for subsequent questions. The file survives context boundaries that the conversation doesn't, so a specific finding from an hour ago is still a specific finding.
Delegate the verbose parts
Spawn subagents for particular questions — "find all test files", "trace the refund flow's dependencies" — while the main agent keeps high-level coordination. The exploration's verbose output stays in the subagent; the main context receives the answer.
Spawn them with the Agent tool. A subagent doesn't inherit the coordinator's conversation history, so its prompt must include everything it needs. A new agent started after a crash also begins from its prompt alone. Writing state to disk helps only if that state is explicitly loaded into the new prompt.
Between phases, write a structured handoff artifact containing exact findings, evidence locations, decisions, unresolved questions, and next steps. Inject that state into the next phase's initial context. A short narrative summary can orient the reader, but it must not replace the exact handoff fields.
/compact
/compact reclaims room in the context window during extended exploration. Use it when the window has filled with verbose discovery — and note it is lossy, which is why scratchpad files and summaries matter. Compaction is a relief valve, not a persistence strategy.
Crash recovery
For long multi-agent work, design for interruption: each agent exports its state to a known location, and the coordinator loads a manifest on resume and injects it into agent prompts.
Resuming a session is a different mechanism and doesn't cover this. It continues a conversation that survived, carrying its old tool results forward with it, and a crash is precisely the case where there is no conversation left to continue.
Without this, a crash three hours into an exploration means three hours of rediscovery.
scratchpad filessubagent delegationmanifests/compact
Field note — common misconceptions
- MythThat degradation announces itself as an error
- ActuallyThe tell is inconsistent answers and talk of typical patterns instead of specifics found earlier.
- MythThat compaction preserves specifics
- Actually
/compactis lossy; a scratchpad file is what keeps a specific finding available later. - MythThat a crashed exploration must restart from nothing
- ActuallyAgents export state to a known location and the coordinator injects the manifest into prompts.
Guided review
Review this lesson as a study deck
Review the lesson's main ideas in five guided slides, then test yourself with three flashcards.
Open Task 5.4 study deckCross-domain reasoning
Connect this idea
Delegation depends on context
A delegated task succeeds only when its scope is clear and the receiving agent gets the instructions, facts, saved state, and sources it needs.
- 1.2 · Orchestrate multi-agent systems with coordinator-subagent patterns
- 1.3 · Configure subagent invocation, context passing, and spawning
- 1.6 · Design task decomposition strategies for complex workflows
- 5.1 · Manage conversation context to preserve critical information
- 5.6 · Preserve information provenance and handle uncertainty in multi-agent systems
Applied practice
Practice this lesson in a lab
Use a related lab to create a decision, implementation or diagram, evidence record, and review.
- Lab 9 · Context budget and durable state
Decide what stays active, what becomes durable, and what must be revalidated after recovery.