Codebase Exploration Context Strategies

Core

Manage context effectively in large codebase exploration · Difficulty 3/5

0%
codebaseexplorationscratchpadsubagentdegradation

Explanation

Large codebase exploration requires specific strategies to maintain accuracy as context accumulates during extended investigation sessions.

Context Degradation Problem

After exploring many files, the model's responses degrade:

  • References become vague ("typical patterns" instead of specific class names)
  • Earlier findings are forgotten or contradicted
  • Answers become inconsistent across similar questions

Scratchpad Pattern

Have agents maintain scratchpad files that record key findings:

  1. After each significant discovery, write findings to a scratchpad file
  2. Before answering subsequent questions, re-read the scratchpad
  3. This externalizes memory beyond the context window

Subagent Delegation

Spawn subagents for specific investigation tasks:

  • "Find all test files" -- subagent explores, returns structured list
  • "Trace refund flow dependencies" -- subagent traces, returns dependency graph
  • Main agent stays focused on high-level coordination with clean context

Phase-Based Exploration

  1. Phase 1: Map structure (subagent explores, returns summary)
  2. Inject Phase 1 summary into Phase 2 context
  3. Phase 2: Deep-dive specific areas based on Phase 1 findings
  4. Use /compact between phases when context is heavy

Key Takeaways

  • Use scratchpad files to externalize findings beyond the context window
  • Spawn subagents for verbose exploration tasks to keep main agent context clean
  • Summarize findings between exploration phases and inject into next phase context
  • Use /compact to reduce context during extended sessions

Related Concepts