Where files live, how context actually fills, the habits that compound, and the recipes that turn an autonomous coding agent into a tool you actually trust. Distilled from the official docs, written for people who'd rather ship than read.
Part 01
Setup & layout
If you've poked around Claude Code and seen the .claude directory tree, the docs make it look like a lot. It isn't. Most days you'll touch two files. Everything else is opt-in.
The two locations
.claude/ in your project. Commit it. Share it with your team.
~/.claude/ in your home directory. Personal config, applies across every project.
Project stuff travels with the repo. Home stuff travels with you.
The two files you'll actually edit
CLAUDE.md is the instructions file loaded every session. Use it for "here's how this codebase works" content: project conventions, gotchas, build commands, anything you'd otherwise repeat in every prompt.
Three rules that matter:
Run /init first. It generates a starter CLAUDE.md from your codebase.
Keep it short. For each line, ask: "Would removing this cause Claude to make mistakes?" If not, cut it.
A bloated CLAUDE.md gets ignored. If Claude isn't following a rule, the file is probably too long, not too short.
✓ Belongs in CLAUDE.md
Bash commands Claude can't guess
Code style that differs from defaults
Test runner preferences
Repo etiquette (branches, PRs)
Environment quirks, required env vars
Common gotchas, non-obvious behaviors
✗ Doesn't belong
Anything Claude can read from the code
Standard language conventions
Long API docs (link to them)
File-by-file descriptions
"Write clean code" platitudes
Stuff that changes frequently
settings.json is the configuration knob: permissions, hooks, env vars, model defaults. Do these two files. The rest is opt-in.
Part 02
The mental model
Almost every best practice traces back to one thing.
Claude's context window fills fast, and performance degrades as it fills.
If you don't internalize this, nothing else in this guide will stick. Every file read, every command output, every back-and-forth costs tokens. When the window gets full, Claude starts forgetting earlier instructions and making mistakes.
What's already loaded before you type
Most people think a fresh Claude Code session starts empty. It doesn't. Before your first prompt, the context window already holds a baseline of startup content.
Representative startup context budget
system
CLAUDE.md
memory
MCP
skills
System prompt (fixed)CLAUDE.md (project + global)Auto memory entriesMCP tool names from connected serversSkill names + descriptions (not bodies)
Numbers vary by setup. The point: you start with thousands of tokens consumed before a single prompt. Optional additions like output styles or --append-system-prompt stack on top of this.
What happens as Claude works
Each file read adds tokens. A 500-line file might cost 5K to 10K. Reading 20 files in an investigation can blow past 100K.
Path-scoped rules auto-load alongside matching files. Useful, but counts toward your budget.
Hooks fire after each edit and their output enters context too.
Tool outputs accumulate. Test results, command output, error traces. All of it.
The two big levers
Subagents have their own context window. When you delegate "investigate how token refresh works" to a subagent, it reads 30 files in its context. Only the summary comes back to yours. This is why subagents are the single most powerful pattern in Claude Code, not just a nicety. Part 06 covers this in depth.
/compact replaces conversation with a structured summary. Most startup content reloads automatically afterward. Skill listings are the one exception (re-loaded but counted again). Use this when a session is too valuable to /clear but too cluttered to keep going.
The rest of this guide is essentially: how to manage context aggressively so you never feel the degradation.
Part 03
Three habits that compound
i.
Give Claude a way to verify its work.
Tests, screenshots, expected outputs. Without verification, Claude produces plausible-looking code that doesn't handle edge cases, and you become the only feedback loop. With verification, Claude catches its own mistakes before you see them. This is the single highest-leverage thing you can do.
ii.
Explore, plan, then code.
For anything non-trivial, use plan mode first (claude --permission-mode plan or Shift+Tab mid-session). Let Claude read files and answer questions before making changes. Get a written plan, then switch to default mode and implement against it. Skip planning only when you could describe the diff in one sentence.
iii.
Be specific.
Reference exact files with @. Point to existing patterns ("look at HotDogWidget.php as a model and follow that"). Describe symptoms with the likely location and what "fixed" looks like. Vague prompts have their place during exploration. For actual work, specificity cuts iterations.
Part 04
Workflow recipes
Prompt patterns for everyday tasks. Each works in any Claude Code surface. Adapt the wording to your project.
R/01
Onboard to a new codebase
Start broad, then narrow: "give me an overview of this codebase" → "explain the main architecture patterns" → "how is authentication handled?" Ask the same questions you'd ask a senior engineer. Useful follow-up: ask for a glossary of project-specific terms.
R/02
Fix bugs efficiently
Share the reproduction command and the stack trace. "users report login fails after session timeout. check the auth flow in src/auth/, especially token refresh. write a failing test that reproduces it, then fix it." Ask for a few options before applying any one fix.
R/03
Refactor safely
Identify legacy code first, then ask for recommendations, then apply in small testable chunks. Always end with: "run tests for the refactored code." Ask Claude to maintain backward compatibility when it matters.
R/04
Add tests
"find functions in NotificationsService.swift not covered by tests" → "add tests for the notification service" → "add edge cases" → "run the new tests and fix failures." Claude examines existing test files to match style and assertion patterns.
R/05
Open a PR
Ask directly: "create a pr for my changes", or step through summary → PR → refinement. PRs created with gh pr create auto-link the session, so claude --from-pr <number> takes you back later. Always ask Claude to flag risks before submitting.
R/06
Document undocumented code
"find functions without proper JSDoc comments in the auth module" → "add JSDoc comments" → "improve with examples and context." Specify the doc style up front (JSDoc, docstrings, etc.).
R/07
Work with images
Drag/drop, ctrl+v paste, or pass a path. Useful for error screenshots, design mockups, schemas, diagrams. "generate CSS to match this design mockup" works surprisingly well.
R/08
Reference files with @
@src/utils/auth.js includes the file's full content. @src/components gives a directory listing. @github:repos/owner/repo/issues pulls from a connected MCP server. Multiple references work in one message: "compare @file1.js and @file2.js."
R/09
Run on a schedule
Pick by where the task should run:
Routines: Anthropic infra, runs while your machine is off.
Desktop scheduled tasks: local files and uncommitted changes.
GitHub Actions: repo events and CI.
/loop: in-session polling.
Be explicit about success criteria. The task can't ask clarifying questions.
R/10
Work in non-code folders
Claude Code runs in any directory. Notes vault, docs folder, any markdown collection. Edit and reorganize the same way you would code. The .claude/ directory sits alongside other tools' configs without conflict.
Part 05
Session control & cost
Course-correct fast
Esc stops Claude mid-action without losing context.
Esc Esc or /rewind opens the rewind menu to roll back conversation, code, or both.
/clear resets context between unrelated tasks. Use this often.
Subagents investigate in their own context window and report back summaries. If you'd otherwise tell Claude to "go read 40 files," delegate it. (Part 06 covers this in depth.)
If you've corrected Claude twice on the same issue, the context is polluted with failed approaches. /clear and start over with a better prompt that bakes in what you learned. A clean session almost always beats a long one.
Token-saving patterns
Once you understand how context fills, the cost-saving patterns become obvious. These are the ones with the biggest ROI:
Subagents for any read-heavy task. "Investigate," "find all usages of," "map how X works" all belong in a subagent. Their reads don't count against your main context.
/clear between unrelated tasks. Two 80% sessions beat one 200% session that's been compacted twice.
/compact <instructions> when you can't /clear. Try "focus on the API changes and modified files" to keep what matters.
/btw for side questions. The answer appears in a dismissible overlay and never enters conversation history.
@ references over copy-paste. Cleaner than dumping content, and the file path stays as a stable reference.
CLI tools over raw API calls.gh, aws, gcloud return structured, compact output. Untyped curl chains don't.
Custom compaction rules in CLAUDE.md."When compacting, always preserve the full list of modified files and any test commands" ensures critical context survives summarization.
Resume across sittings
Claude Code saves every conversation locally. claude --continue picks up the most recent session in the current directory. claude --resume opens a picker. Name important sessions with /rename so you can find them later.
Plan before editing
claude --permission-mode plan
Claude reads files and proposes a plan but makes no edits until you approve. Press Shift+Tab mid-session to toggle into plan mode. Press Ctrl+G to open the plan in your text editor for direct edits before Claude proceeds.
Run parallel sessions with worktrees
claude --worktree feature-auth
Each worktree is a separate checkout on its own branch. Run the same command with a different name in another terminal to start an isolated parallel session. Edits don't collide. Useful for the Writer/Reviewer pattern: one Claude implements, a second reviews with fresh context.
Pipe Claude into scripts
git log --oneline -20 | claude -p "summarize these recent commits"
Non-interactive mode plugs Claude into CI, pre-commit hooks, and shell loops. Pair with --allowedTools to scope what it can do. Output formats: plain text, JSON, streaming JSON.
Part 06
Working with agents
If subagents were just a power-user feature, they'd belong further down this guide. They aren't. Subagents are how you keep the main conversation usable on any task that involves more than a couple of files, and they're the single biggest cost lever in Claude Code. This is the section worth reading twice.
Why they matter
Three reasons, in order of how often they actually matter day-to-day:
Context preservation. The subagent reads the 40 files in its own context window. You get the summary. Your main conversation stays sharp for implementation.
Cost control. Run read-only investigations on Haiku. Reserve Opus for the actual code. The model field on a subagent overrides the session default.
Specialization and safety. A focused system prompt and a restricted tool set produce better, safer output than a generalist with everything turned on.
How a subagent keeps your context clean
Main conversation
System prompt · CLAUDE.md · your messages · current task state
Same as before, plus a clean summary. No 30 files in your context.
The built-ins you already have
You don't need to define anything to start. Claude Code ships with these and Claude delegates to them automatically.
Explore
model: haiku · tools: read-only
Fast codebase search and discovery. Fires whenever Claude needs to understand a project without changing it. When you say "find," "investigate," or "look through," Explore is usually doing the work.
Plan
model: inherits · tools: read-only
Research agent used during plan mode. Gathers codebase context before Claude presents a plan, without nesting (subagents can't spawn other subagents).
General-purpose
model: inherits · tools: all
Full power. For complex multi-step tasks that need both exploration and changes. Used when the task requires reasoning across results and several dependent steps.
Confirm any of these are working with /agents.
Three ways to invoke
Method
What it does
Natural language
Name the agent in your prompt; Claude decides whether to delegate
@-mention
@code-reviewer (agent). Guarantees that subagent runs
Session-wide
claude --agent code-reviewer runs the whole session as that agent
How custom agents work
A subagent is a markdown file with YAML frontmatter. Drop it in .claude/agents/ (project) or ~/.claude/agents/ (personal). That's the entire mechanism.
---
name: code-reviewer
description: Expert code review specialist. Use proactively after code changes.
tools: Read, Grep, Glob, Bash
model: sonnet
memory: project
---
You are a senior code reviewer ensuring high standards of code quality and security.
When invoked:
1. Run git diff to see recent changes
2. Focus on modified files
3. Begin review immediately
Review checklist:
- Code is clear and readable
- Proper error handling
- No exposed secrets or API keys
- Input validation implemented
- Good test coverage
Provide feedback by priority: Critical / Warnings / Suggestions.
Include specific examples of how to fix each issue.
The body is the system prompt. The frontmatter is configuration. Easiest way to start: run /agents, pick "Generate with Claude," describe what you want, save. The agent is available immediately, no restart needed.
The frontmatter fields that actually matter
The docs document 16 fields. These are the ones worth knowing.
name, description
Required. The description is what Claude reads to decide when to delegate. Vague descriptions get vague delegation. Include "Use proactively" to encourage automatic invocation.
tools
Allowlist (e.g., Read, Grep, Glob, Bash). Or use disallowedTools as a denylist. Locking a code reviewer to read-only is one line of YAML.
model
The cost lever.haiku, sonnet, opus, or inherit. A read-only investigator on Haiku costs a fraction of one on Opus and is usually fast enough.
memory
The killer feature most people miss.project, user, or local. Gives the subagent a MEMORY.md it reads at startup and updates as it learns. Use project as the default so learnings ship with the repo.
skills
Preload skill content into the subagent at startup. Subagents don't inherit skills from the parent, so list them explicitly.
mcpServers
Scope MCP servers to this subagent only. Keeps tool descriptions out of your main context. Useful for keeping a Playwright or database server isolated to one specialist.
isolation
Set to worktree to give the subagent its own git checkout. Safe place to try risky changes. Auto-cleaned if no changes are made.
background
Set to true to always run concurrent with the main conversation. Ctrl+B backgrounds a running task on demand.
permissionMode
default, acceptEdits, auto, bypassPermissions, or plan. Inherits from parent in most cases. Use sparingly.
Persistent memory: the underused feature
When memory is set, the subagent gets a directory it owns. It reads MEMORY.md at startup and updates it as it works. Over time, this becomes institutional knowledge: codebase patterns, recurring bugs, architectural decisions, library locations.
Two prompts make this real:
Before work:"Review this PR, and check your memory for patterns you've seen before."
After work:"Save what you learned to your memory."
Or bake it into the system prompt so it happens automatically:
Update your agent memory as you discover codepaths, patterns, library
locations, and key architectural decisions. Write concise notes about
what you found and where.
This is how a code-reviewer becomes more useful in month three than month one. It compounds.
Patterns that work
Isolate verbose operations. Test runs, log analysis, doc fetches. The output stays in the subagent. The summary comes back.
Run parallel research. Independent investigations spawned simultaneously: "research the auth, database, and API modules in parallel using separate subagents."
Chain subagents."Use the code-reviewer subagent to find performance issues, then use the optimizer subagent to fix them." Each runs in its own context. Claude passes only the relevant findings between them.
Cost-tier your fleet. Read-only Haiku agents for investigation. Sonnet for review and refactors. Opus for hard implementation. Define them once, reuse forever.
When NOT to use a subagent
Subagents aren't free. They start fresh, take time to spin up, and their results have to fit back into your main context. Stay in the main conversation when:
The task is small and targeted (one-line fix, rename, typo)
You need iterative back-and-forth or quick refinement
Latency matters and you'll be waiting on the result
For a quick question about something already in your conversation, use /btw instead. It sees full context, has no tool access, and the answer never enters history. For workflows that need nested delegation, use Skills or chain subagents from the main conversation. Subagents cannot spawn other subagents.
Best practices, the short list
One agent, one job. Focused agents outperform generalists.
Write detailed descriptions. Claude uses the description to decide when to delegate.
Limit tool access. Read-only agents stay read-only. Write access is a permission, not a default.
Check project agents into version control. If your team would benefit from it, ship it with the repo.
Part 07
Building it out
Add these when you have a real reason. Not before. The hardest part is knowing which mechanism fits which need.
When to use what
The biggest source of confusion in Claude Code is having seven different ways to give Claude information. Here's the short version:
You want…
Use
Project rules every session needs
CLAUDE.md
Rules that only apply to certain paths
rules/*.md
Domain knowledge or workflows on demand
skills/<name>/SKILL.md
A reusable single-file prompt
commands/*.md
A specialized agent with isolated context
agents/*.md (see Part 06)
An action that must always happen
hooks (in settings.json)
Connection to an external service
MCP server (.mcp.json)
Personal override for one project
settings.local.json or CLAUDE.local.md
The most common mistake: stuffing everything into CLAUDE.md. If a piece of context only applies sometimes, it's a skill. If it must happen every time without fail, it's a hook. If it reads many files, it's a subagent. CLAUDE.md is for the always-relevant baseline.
Capabilities worth turning on
Hooks run scripts deterministically at specific points. CLAUDE.md is advisory. Hooks are guarantees. Use them when something must happen every time, no exceptions (lint after every edit, block writes to migrations/, validate SQL before execution).
CLI tools like gh, aws, gcloud, sentry-cli are the most context-efficient way to talk to external services. Install them and tell Claude they're available.
MCP servers via claude mcp add connect Notion, Figma, your database, your monitoring stack.
Permissions: use auto mode to let a classifier handle approvals, allowlist trusted commands, or sandbox for OS-level isolation. Default-prompt-everything is safe but tedious.
Plugins via /plugin bundle skills, hooks, subagents, and MCP servers. Code intelligence plugins are especially worth it for typed languages.
Part 08
Reference & cheatsheet
Slash commands for what actually loaded
The directory shows what can exist. These commands show what's actually live in your session.
/context
Token usage by category, with optimization suggestions. Run this first.
/memory
Which CLAUDE.md and rules files loaded, plus auto-memory entries.
/agents
Manage subagents (Running, Library tabs). Create, edit, delete, see active.
/hooks
Active hook configurations.
/mcp
Connected MCP servers and status.
/skills
Available skills from project, user, and plugin sources.
/permissions
Current allow and deny rules.
/doctor
Full installation and config diagnostics.
Settings precedence (top wins)
Managed settings (managed-settings.json) deployed by your org. Can't override. Lives at the system level, varies by OS. Worth knowing exists before you waste an hour wondering why your settings aren't sticking.
CLI flags like --permission-mode or --settings.
Environment variables (varies by setting).
settings.json.
When something doesn't behave the way you expect, walk this list.
Failure patterns to recognize
The kitchen sink session.
Multiple unrelated tasks in one conversation. Fix:/clear between tasks.
The correction loop.
Same issue corrected twice and still wrong. Fix:/clear and write a better prompt that bakes in what you learned.
The bloated CLAUDE.md.
Claude ignores rules buried in noise. Fix: ruthlessly prune, or convert advisory rules to hooks.
The trust-then-verify gap.
Plausible code that fails on edge cases. Fix: provide tests, screenshots, or expected outputs up front.
The infinite exploration.
"Investigate X" with no scope, Claude reads hundreds of files. Fix: scope it, or push it to a subagent.
The do-it-all subagent.
One agent with all tools and a vague description. Claude doesn't know when to use it. Fix: split into focused agents with clear descriptions.
The cheatsheet
The commands you'll actually reach for, in one place.