Lesson 62.2 · 2 of 3 · 4 min read · Reviewed 24 Nov 2026
Exploring large codebases without overflowing context
By the end, you can
- Explore a large codebase without filling the context.
- Combine search, targeted reads and subagents.
A big repo can be millions of tokens. The agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. must explore like an experienced engineer: skim, search, then read closely.
A good sequence
- Orient: read CLAUDE.mdCLAUDE.md: A markdown file of standing instructions that Claude Code loads automatically, at user, project or directory level., the README and the top-level layout. Find entry points.
- Search: Grep for key names (
charge,PaymentIntent,/api/pay). Glob for likely files. - Read narrowly: open only the files and line ranges that matter.
- Delegate: send subagentsSubagent: A separate agent instance, with its own context and instructions, that a main agent delegates a task to. to map side areas (“how are webhooks verified?”) and return short findings (58.4).
- Write it down: record what was learned in notes (58.5) so it isn’t rediscovered after compactionCompaction: Summarising older conversation history so a long session fits in the context window while keeping what matters..
Help the agent
- A good CLAUDE.md with a map of the repo saves many tool calls.
- Subdirectory CLAUDE.md files add local knowledge only where it’s relevant.
- Consistent naming makes search work.
Key points
- Start with structure: directory layout, README, CLAUDE.md, entry points.
- Use Grep and Glob to find relevant code, then read only those parts.
- Delegate broad exploration to subagents that return short findings.
Explore a repo without reading everything
Find where payments are charged in a simulated repo while reading only a small share of its tokens.
You will have: A grep() tool, a search-then-read sequence and a printed comparison of tokens used against reading every file.
Steps
- Run the starter to see the token cost of reading every file.
- Write grep() to return (path, line) hits without reading whole files into context.
- Orient with README.md, grep for PaymentIntent, then read a few lines around each hit.
- Write what you learned into a small notes dict so it survives compaction.
Done when
- Exactly one file is read narrowly: src/payments/charge.py.
- Targeted reads use under 5% of the tokens of reading the whole repo.
- The notes record the entry point and where webhooks are verified.
Open in the Playground No API key needed.
Starter code (Python)
# A simulated repository: 40 files. Reading a file costs about len(text) / 4 tokens.
REPO = {f"src/feature_{i:02d}/module.py": f"# feature {i}\n" + "def helper():\n return 0\n" * 40 for i in range(36)}
REPO["README.md"] = "Payments live in src/payments. Webhooks are verified in src/payments/webhooks.py.\n"
REPO["src/payments/charge.py"] = "def charge(customer, amount):\n intent = PaymentIntent(customer, amount)\n" + " # retry logic\n" * 30 + " return intent\n"
REPO["src/payments/webhooks.py"] = "def verify(signature, body):\n return hmac_check(signature, body)\n" + "# notes\n" * 60
REPO["src/payments/refunds.py"] = "def refund(intent):\n return intent.refund()\n" + "# notes\n" * 50
tokens = lambda t: len(t) // 4
budget_used = 0
def read(path, start=0, end=None):
global budget_used
lines = REPO[path].splitlines()[start:end]
text = "\n".join(lines)
budget_used += tokens(text)
return text
def grep(pattern):
# TODO: return a list of (path, line_number) where the pattern appears, without counting tokens
return []
# The naive approach: read everything.
for p in REPO:
read(p)
whole = budget_used
print("tokens to read the whole repo:", whole)
budget_used = 0
# TODO: orient with README.md, grep for "PaymentIntent", then read only a few lines around each hit
print("tokens with search, then targeted reads:", budget_used)
Show a solution
# A simulated repository: 40 files. Reading a file costs about len(text) / 4 tokens.
REPO = {f"src/feature_{i:02d}/module.py": f"# feature {i}\n" + "def helper():\n return 0\n" * 40 for i in range(36)}
REPO["README.md"] = "Payments live in src/payments. Webhooks are verified in src/payments/webhooks.py.\n"
REPO["src/payments/charge.py"] = "def charge(customer, amount):\n intent = PaymentIntent(customer, amount)\n" + " # retry logic\n" * 30 + " return intent\n"
REPO["src/payments/webhooks.py"] = "def verify(signature, body):\n return hmac_check(signature, body)\n" + "# notes\n" * 60
REPO["src/payments/refunds.py"] = "def refund(intent):\n return intent.refund()\n" + "# notes\n" * 50
tokens = lambda t: len(t) // 4
budget_used = 0
def read(path, start=0, end=None):
global budget_used
lines = REPO[path].splitlines()[start:end]
text = "\n".join(lines)
budget_used += tokens(text)
return text
def grep(pattern):
hits = []
for path, text in REPO.items():
for n, line in enumerate(text.splitlines()):
if pattern in line:
hits.append((path, n))
return hits
for p in REPO:
read(p)
whole = budget_used
print("tokens to read the whole repo:", whole)
budget_used = 0
read("README.md") # orient first
hits = grep("PaymentIntent")
files = sorted({p for p, _ in hits})
for path, n in hits:
read(path, max(0, n - 2), n + 3) # a few lines around each hit
print("hits:", hits)
print(f"read {len(files)} file(s) narrowly")
print("tokens with search, then targeted reads:", budget_used)
print(f"share of the naive cost: {budget_used / whole:.1%}")
notes = {"payments entry point": files[0], "webhooks": "src/payments/webhooks.py (from README)"}
print("notes:", notes)