Every long Claude Code session ends the same way: you lose hours of progress and start over from scratch.
The fix isn't a smarter model, it's a system the Claude Code team uses internally and never wrote a guide for.
Most devs don't have it and don't know it exists.
Here's the full system 👇
🧠Before we dive in, I share daily notes on AI & vibe coding in my Telegram channel:https://t.me/zodchixquant

What context engineering actually means
Claude reads thousands of lines every session. Most of it is noise, and the 5% that matters gets buried by hour two.
That's when Claude starts inventing functions and fixing bugs you never wrote. The fix isn't a bigger model, it's controlling what enters the session.
These are the 6 patterns the Claude Code team runs on their own monorepo.

Pattern 1: Layered CLAUDE.md (Steps 1-3)
One CLAUDE.md isn't enough. The Claude Code team runs three layers, each scoped to a different concern.
Step 1: Root CLAUDE.md with project-wide rules
Drop a CLAUDE.md in your repo root. Keep it under 100 lines. It should answer four questions Claude has on every prompt:
1# Project: [name]23## Stack4- Runtime: Node 22, TypeScript 5.55- Framework: Next.js 15, React 196- Database: Postgres 16, Drizzle ORM7- Testing: Vitest, Playwright89## Conventions10- Imports: absolute paths from `@/` only11- Components: kebab-case files, PascalCase exports12- Tests: colocated as `[file].test.ts`13- Commits: conventional commits, no co-author footer1415## Don't touch16- `infra/terraform/` (managed by ops)17- `migrations/` (generated, never hand-edited)18- `vendor/` (committed dependencies)1920## Defaults21- Run `npm test` before declaring a task complete22- Type-check with `tsc --noEmit` on every edit23- Format with Prettier on save
Step 2: Subdirectory CLAUDE.md for module-specific context
Add CLAUDE.md in any folder where the rules change. Claude reads the closest one when working in that folder, on top of the root one.
Example, src/auth/CLAUDE.md:
1# Auth module23This module owns session management and OAuth flows.45## Rules6- Never log tokens, even truncated7- All session reads go through `getSession()`, never raw cookies8- New providers require a security review before merging910## Files to read first11- `session.ts` for the contract12- `providers/index.ts` for the registry pattern
Step 3: Personal CLAUDE.md at ~/.claude/CLAUDE.md
This is your global preference layer. Lives outside any repo, applies everywhere.
1## My preferences - Explain what you're about to do before doing it on multi-file edits - Push back if my request looks like it'll break something - Prefer composition over inheritance - I work in zsh on macOS
The three layers stack: global preferences + project rules + module rules. Claude reads all three on every prompt without re-uploading them through your context.
Pattern 2: Surgical file references (Steps 4-5)
The default "let Claude figure out which files matter" is the slowest, dumbest way to use context.
Step 4:@file references with autocomplete
When you type @ in a prompt, Claude opens a fuzzy file picker. Use it instead of describing files in prose.
Bad: "look at the user authentication code and the session helper"
Good: "look at
@src/auth /session.ts and
@src/auth /providers/google.ts, fix the token refresh"
Surgical references load exactly those files. Prose descriptions force Claude to search, read 4-5 candidate files, and pick wrong half the time.
Step 5: /focus to scope an entire workflow
For longer tasks, scope Claude's whole session to specific folders:
1/focus src/auth src/api/auth-routes
Now every search, grep, and read operates inside those two folders only.
Cuts context usage by 60-80% on focused tasks, and Claude stops "helpfully" reading unrelated files for context it doesn't need.
Run /focus clear to reset.
Pattern 3: Compact-and-continue (Steps 6-8)
Long sessions hit the context limit and crash. The trick isn't avoiding long sessions, it's running them through controlled compaction.
Step 6: Watch context usage with /stats
Run /stats periodically.
When you're past 70% context usage, you're in the danger zone. Past 85%, Claude starts dropping early messages without telling you.
Step 7: /compact with preservation instructions
/compact alone summarizes the conversation. With instructions, you tell Claude what to preserve verbatim:
1/compact preserve: current task plan, all decisions about the auth refactor, the failing test output from step 3
Step 8: /resume for sessions that span days
If a task spans multiple days, end each session with /compact preserve: ... and start the next one with:
1claude --resume [session-id]
Or just claude --continue for the most recent session. Picks up exactly where you left off with the compacted state intact.
Pattern 4: Plan-first reading (Steps 9-10)
The biggest context leak is Claude editing files before it understands them. Plan mode forces it to read first, write second.
Step 9: Toggle plan mode on every risky task
Hit Shift+Tab to drop into plan mode before any task that touches more than one file.
In plan mode Claude can only read. It explains what it would do, lists every file it plans to touch, and shows the proposed changes. Nothing runs until you approve.
The savings are real: one read, one edit, instead of read 8, edit 3 wrong, revert, re-read, edit again.
Step 10: Lock plan mode for sensitive paths via hooks
Make plan mode mandatory for risky areas. In .claude/settings.json:
1{2 "hooks": {3 "PreToolUse": [4 {5 "matcher": "Edit(src/auth/**)|Edit(migrations/**)",6 "hooks": [7 {8 "type": "command",9 "command": "claude-require-plan-mode"10 }11 ]12 }13 ]14 }15}
Any edit attempt in src/auth/*\ or migrations/\\ *triggers a plan-mode requirement. Claude can't bypass it.
Pattern 5: Subagent context isolation (Steps 11-13)
Subagents are the cleanest way to keep your main context small. Each subagent runs in its own window, so the parent session never sees the noise.
Step 11: Design subagents with minimal tool surface
Restrict tools to what the job needs.
A code-reviewer doesn't need Write or Edit.
A doc-updater doesn't need Bash. Less surface, less context burned on irrelevant docs and tool descriptions.
1---2name: code-reviewer3description: Review code changes for bugs and security issues4tools: Read, Grep, Glob5model: sonnet6---
Step 12: Pass only the file references the agent needs
Don't say "review the recent changes." Say "review @src/auth/session.ts and @src/auth/providers/google.ts."
The subagent reads only those, not its best guess at what "recent" means.
Step 13: Use cheaper models for subagents
Set the model per agent based on the task.
Sonnet for review and test tasks, Haiku for doc updates and lints, Opus only for architecture or security audits where deep reasoning matters.
Set globally if needed:
1export CLAUDE_CODE_SUBAGENT_MODEL="claude-sonnet-4-6"
Cheaper subagents = more subagent calls per dollar = more isolation = smaller main context.
The 30-minute context engineering starter
10 minutes: write a tight root CLAUDE.md with stack, conventions, don't-touch list, and defaults.
5 minutes: add subdirectory CLAUDE.md in your 2-3 most-edited modules.
5 minutes: drop a personal CLAUDE.md in ~/.claude/CLAUDE.md with your preferences.
5 minutes: build one skill in .claude/skills/ for the workflow you do most often.
5 minutes: bind Shift+Tab muscle memory and start every multi-file task in plan mode.
Done!
Your next session will use 50-70% less context for the same amount of work. By session 5 the patterns become reflex, and you stop hitting the limit at all.
Context isn't something Claude manages for you. It's something you engineer.
Now you have the system.
Thanks for reading!
I share daily notes on AI, finance, and vibe coding in my Telegram channel: https://t.me/zodchixquant






