A large codebase has more relevant code than any context window can hold, so the question is never “how do I fit it all in” but “how do I keep the right slice in and the rest out.” This chapter is the exam-angle inventory of the levers; the design book owns the at-scale mechanics. It is an architectural pattern — the levers are stable, the commands and hooks that drive them are the surface.
The codebase does not fit, and reading it all is the trap
Context is cumulative and finite (D5.1): conversation history and every tool input and output accumulate in the window over a session. [Official] How the agent loop works · AnthropicT1-official original A large codebase has far more relevant files than that window holds, and the naive response — have the agent read everything “to be safe” — is itself the failure: it fills the window with material that crowds out the work and degrades the model’s attention to what matters.
Compaction extends a long session
When a working session approaches the limit, compaction reclaims room by summarizing older turns, and it is both automatic and steerable. Automatic compaction triggers near the context limit,
[Official]
How the agent loop works · AnthropicT1-official original and three knobs customize it: a “Summary instructions” section in CLAUDE.md that tells the compactor what to preserve, the PreCompact hook that runs before compaction, and a manual /compact sent on demand.
[Official]
How the agent loop works · AnthropicT1-official original
Compaction is lossy (D5.1), so the same caution applies: durable rules belong in re-injected CLAUDE.md, not in turns the summary may discard.
/compact vs /clear, and the scratchpad beneath both
Two commands free context, and confusing them wastes work. /compact [instructions] “frees context by summarizing” — it condenses the conversation in place, so you keep going on the same task with a shorter history, and the optional instructions focus what the summary keeps.
[Official]
Commands · AnthropicT1-official original /clear instead starts a fresh conversation — it discards the working context entirely, with the previous one still available via /resume (aliases /reset, /new).
[Official]
Commands · AnthropicT1-official original
The decision rule is continuity. Reach for /compact to continue a task whose history has grown long but is still relevant. Reach for /clear to switch to an unrelated task — or when a session is cluttered with failed approaches, the D3.5 rule that after more than two corrections on the same issue, “a clean session with a better prompt almost always outperforms a long session with accumulated corrections.”
[Official]
Best practices for Claude Code · AnthropicT1-official original Compaction keeps a lossy summary; clearing keeps nothing in the window at all.
Delegation pays exploration cost in another window
The lever that matters most for breadth is delegation. “Since context is your fundamental constraint, subagents are one of the most powerful tools available. When Claude researches a codebase it reads lots of files, all of which consume your context. Subagents run in separate context windows and report back summaries.” [Official] Best practices for Claude Code · AnthropicT1-official original A subagent can read the twenty files that answer “where is auth enforced,” and the main agent receives the three-line answer rather than the twenty files — the exploration cost is paid in the child’s window and discarded with it. Scratchpads are the complementary move: state written to a file (D1.7) lives on disk, not in the window, and the main agent reads it back only when needed.
Where the depth lives
This chapter is the exam-angle inventory; the design book owns the at-scale mechanics. The engineering of context at codebase scale — retrieval, the discipline of what to assemble into a window, and the cost trade-offs of fan-out — lives in the Agentic Systems Design book’s chapters on the environment at scale and context assembly. The exam-relevant skill is selecting the lever: compaction for a long single session, delegation for breadth of exploration, scratchpads for state that must outlive a compaction.
Practice
Exercise solutions
B. Delegation pays the exploration cost in the subagents’ separate context windows and returns only summaries, so the main session learns where authentication flows without absorbing dozens of files — exactly the “subagents run in separate context windows and report back summaries” pattern. A is the trap this chapter names: reading everything into the main window fills it with material that crowds out the actual change and dilutes attention. C buys a bigger budget but still spends it on noise, and a larger window degrades on irrelevant bulk just the same. D fights symptoms — compacting mid-exploration repeatedly summarizes away the very findings you are gathering, and is no substitute for never loading the bulk into the main window in the first place.
The three are (1) a CLAUDE.md “Summary instructions” section, (2) the PreCompact hook, and (3) manual /compact. The one that steers what content survives the summary is the CLAUDE.md “Summary instructions” section — the compactor reads CLAUDE.md like any other context, so a section describing what to preserve directs what the summary keeps. The PreCompact hook runs before compaction (e.g. to archive the full transcript) and manual /compact controls when it happens, not what survives — though /compact’s optional focus instructions also nudge the summary’s content.
Dispatching a subagent protects the main agent’s context because the subagent runs in its own separate context window: it reads the files needed to answer the question there, spending that exploration cost against its own budget, and that window is discarded when it returns. The main agent receives back only a summary — the three-line answer (“auth is enforced in middleware X via pattern Y”), not the twenty files the subagent read — so the main context learns the conclusion without absorbing the bulk that produced it. Exploration cost is paid in the child window and thrown away with it.
Exam essentials
- Reading it all is the trap — a large codebase exceeds any window; loading everything “to be safe” fills the context with noise and degrades attention. Load the task’s slice, keep the rest out.
- Compaction — triggers automatically near the limit and is steerable three ways: a CLAUDE.md “Summary instructions” section (steers what survives), the
PreCompacthook, and manual/compact. /compactvs/clear—/compactcondenses the conversation to continue the same task (keeps a lossy summary);/clearstarts a fresh conversation for an unrelated task or after >2 failed corrections (previous in/resume; aliases/reset,/new). A disk scratchpad (D1.7) survives both — neither command touches a file.- Delegation — subagents read files in their own context windows and report back summaries, so exploration cost is paid in the child window, not the main one; scratchpads (D1.7) park state on disk.
- Pick the lever — compaction for a long single session, delegation for breadth of exploration, scratchpads for state that must outlive a compaction; depth lives in the design book’s at-scale and context-assembly chapters.