Source: ai-research/anthropic-best-practices-claude-code-2026-05-17.md — Anthropic’s official “Best practices for Claude Code” doc at code.claude.com/docs/en/best-practices. Primary source for everything in this article. Plus refresh: raw/x-elias-al-cal-rueb-code-with-claude-best-practices.md — X-signal anchor (Cal Rueb at Code with Claude SF, May 22, 2025 — the talk that produced the published doc).
The canonical Anthropic guide on operating Claude Code effectively — the engineering-team-distilled patterns underneath the Karpathy CLAUDE.md techniques this vault tracks. Anchored by one constraint: Claude’s context window fills up fast, and performance degrades as it fills. Every pattern in the doc is downstream of that single observation. Captures the full inventory of context-management tools (/clear, /compact, /compact <instructions>, /rewind directional summarization, CLAUDE.md compaction directives, /btw) that the Reddit-mediated Week 21 summary surfaced from inside the broader best-practices framing.
Key Takeaways
The single constraint everything else follows from
- Context window = the most important resource to manage. A single debugging session or codebase exploration consumes tens of thousands of tokens.
- LLM performance degrades as context fills — Claude starts “forgetting” earlier instructions and making more mistakes near the limit.
- Anthropic frames this as the root cause behind most failure patterns; every other best practice in the doc is a context-management lever.
The single highest-leverage move
“Include tests, screenshots, or expected outputs so Claude can check itself. This is the single highest-leverage thing you can do.”
Verification options: test suite, linter, Bash command that checks output, screenshot comparison via Claude in Chrome, or pasted expected output. Without it, the user becomes the only feedback loop and every mistake demands attention.
The 4-phase workflow (explore → plan → code → commit)
| Phase | Mode | What happens |
|---|---|---|
| Explore | Plan mode | Claude reads files + answers questions, no changes made |
| Plan | Plan mode (continued) | Detailed implementation plan; Ctrl+G opens plan in editor for direct edits |
| Implement | Default mode | Claude codes against the plan; verifies as it goes |
| Commit | Default mode | Descriptive message + PR |
Skip the plan when: scope is clear, fix is small (typo, log line, rename). Plan when: uncertain about approach, change touches multiple files, unfamiliar code. If you can describe the diff in one sentence, skip the plan.
CLAUDE.md is loaded every session
Inclusion/exclusion table from the official doc:
| ✅ Include | ❌ Exclude |
|---|---|
| Bash commands Claude can’t guess | Anything Claude can figure out by reading code |
| Code style rules that differ from defaults | Standard language conventions Claude already knows |
| Testing instructions + preferred test runners | Detailed API documentation (link instead) |
| Repository etiquette (branch naming, PR conventions) | Information that changes frequently |
| Project-specific architectural decisions | Long explanations or tutorials |
| Developer environment quirks (required env vars) | File-by-file descriptions of the codebase |
| Common gotchas + non-obvious behaviors | Self-evident practices like “write clean code” |
Tuning rules (Anthropic’s own, verbatim):
- “If Claude keeps doing something you don’t want despite having a rule against it, the file is probably too long and the rule is getting lost.”
- “If Claude asks you questions that are answered in CLAUDE.md, the phrasing might be ambiguous.”
- “Treat CLAUDE.md like code: review it when things go wrong, prune it regularly.”
Imports via @path/to/import syntax; placement options span home folder, project root, child directories (loaded on-demand), parent directories (monorepo pattern).
The complete context-tool inventory (8 tools, not 2)
Reddit 1tfjja8 surfaced 4 tools between /clear and /compact. The official doc actually documents 8 distinct context-management surfaces:
| Tool | What it does | When to reach for it |
|---|---|---|
/clear | Reset context entirely | Between unrelated tasks. Admission you waited too long if reached mid-flow. |
| Auto compaction | Triggers near context limit; Claude summarizes preserving code patterns + file states + key decisions | Default safety net. |
/compact <instructions> | Direct the summary explicitly (/compact Focus on the API changes) | When you know what the next step needs and the auto-summary might miss it. |
/rewind → Summarize from here | Condenses messages forward from checkpoint; keeps earlier context intact | Keep early architectural decisions; compress messy debugging after them. |
/rewind → Summarize up to here | Condenses messages before checkpoint; keeps recent ones in full | Drop the setup noise; keep recent precise working state. |
| CLAUDE.md compaction directives | Lines like “When compacting, always preserve the full list of modified files and any test commands” | Set invariants once; bake them into every compaction. |
/btw | Side questions appear in a dismissible overlay and never enter conversation history | Quick lookups (“what does this flag do?”) you don’t want polluting context. |
| Subagents | Run in separate context windows; return summaries to main | Heavy investigation. The single most powerful context-saving tool in the doc. |
Decision rule from the doc: “/clear is admission you waited too long. The earlier instrument you reach for, the cheaper your session stays.”
Communication patterns
Ask Claude questions you’d ask a senior engineer. Concrete examples from the doc:
- How does logging work?
- What edge cases does
CustomerOnboardingFlowImplhandle? - Why does this code call
foo()instead ofbar()on line 333?
Let Claude interview you for larger features:
“I want to build [brief description]. Interview me in detail using the AskUserQuestion tool. Ask about technical implementation, UI/UX, edge cases, concerns, and tradeoffs. Don’t ask obvious questions, dig into the hard parts I might not have considered. Keep interviewing until we’ve covered everything, then write a complete spec to SPEC.md.”
Then start a fresh session to execute against the spec.
Permission modes — three ways to reduce interruptions
| Mode | What it does |
|---|---|
| Auto mode | Classifier model reviews commands, blocks only what looks risky (scope escalation, unknown infrastructure, hostile-content-driven actions) |
| Permission allowlists | Permit specific tools (npm run lint, git commit) without per-call approval |
| Sandboxing | OS-level isolation restricting filesystem + network access |
Pairs with /goal (see goal walkthrough) for uninterrupted long-running sessions — auto mode is the standard companion to /goal per the doc.
Extension points (5 distinct surfaces)
| Surface | Use when |
|---|---|
CLI tools (gh, aws, gcloud, sentry-cli) | Most context-efficient. Anthropic’s recommended default. |
| MCP servers | External services without a CLI (Notion, Figma, databases) |
| Hooks | Actions that must happen every time with zero exceptions — deterministic, unlike advisory CLAUDE.md |
Skills (SKILL.md in .claude/skills/) | Domain knowledge + reusable workflows; loaded on demand, not bloating every conversation |
Subagents (.claude/agents/) | Isolated tasks with their own context + scoped toolset; delegated work |
| Plugins | Bundle skills + hooks + subagents + MCP into a single installable unit |
Skill frontmatter pattern with disable-model-invocation: true — for workflows with side effects (deploy, send message) that should be triggered manually only.
Parallel-session scaling
Four parallelization paths:
- Worktrees — separate CLI sessions, isolated git checkouts
- Desktop app — visual multi-session, each in its own worktree
- Claude Code on the web — Anthropic-managed cloud VMs (isolated, no local resource cost)
- Agent teams — automated coordination with shared tasks + messaging + team lead
Writer/Reviewer pattern — fresh context in the Reviewer Claude removes bias toward code it just wrote. Anthropic explicitly recommends this for high-quality reviews.
Fan-out for batch work
for file in $(cat files.txt); do
claude -p "Migrate $file from React to Vue. Return OK or FAIL." \
--allowedTools "Edit,Bash(git commit *)"
doneTest on a few files, then run at scale. --allowedTools matters for unattended operations — restricts what Claude can do when there’s no human in the loop.
The 5 named failure patterns (from the doc)
- The kitchen sink session — Context full of unrelated work. Fix:
/clearbetween unrelated tasks. - Correcting over and over — Context polluted with failed approaches. Fix: After two failed corrections,
/clear+ better initial prompt incorporating what you learned. - The over-specified CLAUDE.md — Important rules lost in noise. Fix: Ruthlessly prune. If Claude does something correctly without the instruction, delete it.
- The trust-then-verify gap — Plausible-looking output that doesn’t handle edge cases. Fix: Always provide verification (tests, scripts, screenshots). If you can’t verify it, don’t ship it.
- The infinite exploration — Unscoped “investigate” → Claude reads hundreds of files. Fix: Scope narrowly or use subagents.
Develop your intuition (final section)
The patterns are starting points, not rules. Sometimes you should:
- Let context accumulate (deep in one complex problem; history is valuable)
- Skip planning (exploratory tasks)
- Use a vague prompt (see how Claude interprets before constraining)
Anthropic frames this as intuition no guide can capture — pay attention to what works, ask why when Claude struggles.
Cross-references inside the karpathy wiki
This doc is the upstream primary source for several existing articles in this vault:
- karpathy-techniques-for-claude-code — Karpathy’s CLAUDE.md techniques distill Anthropic’s patterns above for the specific Karpathy-LLM-wiki pattern
- whats-new-2026-w21 — the 4-context-tools surfacing in W21 (see expanded inventory above) was a Reddit-mediated quote from this doc; the full source is now captured
- claude-code-goal-command-walkthrough —
/goalpairs with auto mode per this doc’s explicit recommendation - agent-skills-overview — skills extension point referenced here
- subagents — subagent investigation pattern explicitly described here
- hooks — hooks-vs-CLAUDE.md distinction (deterministic vs advisory) is from this doc
- cli-reference — auto-mode,
claude -p,--allowedTools,--output-formatall detailed in this doc - claude-code-routines — routine framing inherits from the parallel-sessions section
- agent-teams — agent-teams is one of the four parallelization paths in this doc
- claude-code-memory-architecture-comparison — companion view on memory; this doc describes the Anthropic-shipped behavior, the comparison article covers community alternatives (memarch, Hermes)
- anthropic-engineers-four-skill-rules — same source-of-truth lineage; the four-rules video also cites this doc
- whats-new-2026-w17 —
/rewindshipped in W17 - W21 — Rewind “Summarize up to here” shipped in v2.1.141 (W21)
- W20 —
/goalshipped in v2.1.139
Community signals
[X signal — @iam_elias1 2026-05-18] Origin attribution: Cal Rueb at Code with Claude SF, May 22, 2025
Elias Al’s promotional X post (url, 2,777 views at capture) names the talk that produced the published doc this article is built on:
- Speaker: Cal Rueb, Member of Technical Staff at Anthropic
- Event: Code with Claude, San Francisco
- Date: May 22, 2025
- Framing claimed in the post: “everything Anthropic’s own engineers do internally that most users never figure out on their own”
The X post’s section outline of the talk matches the structure of the published doc this article summarizes:
- CLAUDE.md setup so Claude always knows your codebase (see above)
- Why planning before coding matters (see above)
- Context management so performance doesn’t degrade (see above)
- Parallel session workflow Anthropic engineers use daily (see above)
- Security, permissions, and staying in control (see above)
Color anecdote from the post: Cal described becoming “completely addicted” to Claude Code after a weekend install, building a note-taking app, and carrying his laptop everywhere to watch it work.
Quantitative claim — needs verification
The X post asserts: “Anthropic’s internal testing found that unguided attempts succeed roughly 33% of the time. With proper planning and structured workflows, that number climbs dramatically.” ^[inferred]
This is a two-hop citation chain (X post → talk → Anthropic internal data). The number does not appear in the published best-practices doc that this article cites. Surface it cautiously when relying on it; corroborate against the actual talk recording before treating as a primary-source baseline.
Open Questions
/btwdocumentation depth — the doc cites it but only at the summary level. The dedicated/en/interactive-mode#side-questions-with-%2Fbtwpage is referenced but not pulled. Future ai-research pass candidate./en/checkpointingRestore vs. summarizesemantics — the doc points to this page for the full semantics of Rewind directional summarization. Worth pulling as a separate ai-research file./en/sandboxingreference — sandboxing mode is referenced as a permission-mode option; the full sandboxing page wasn’t captured.- Versioning + last-updated date — Anthropic doesn’t surface a “last updated” stamp on the public doc; we don’t know how stale this snapshot is. Re-fetch quarterly + diff would catch drift.
- Cal Rueb talk recording — the X post references a video (“Watch the full talk here”) with the talk recording. Pulling the recording (or its transcript) as a separate ai-research source would corroborate the 33% stat and capture content present in the talk but not in the written doc.
Related
- _index — topic root
- karpathy-techniques-for-claude-code — community-derived patterns sitting on top of this doc
- agent-skills-overview — skills system reference
- subagents — subagent reference
- cli-reference —
claudebinary surface - hooks — hooks reference
- claude-code-routines — routines (inherits from parallel-sessions)
- whats-new-2026-w21 — release-week sourcing of the W21 Reddit-mediated quote
- anthropic-engineers-four-skill-rules — same primary-source ancestry
- claude-code-goal-command-walkthrough —
/goal+ auto-mode pairing per this doc
Try It
- Read the source once end-to-end at code.claude.com/docs/en/best-practices. Cheaper than re-discovering each pattern from scratch.
- Audit your
CLAUDE.mdagainst the ✅/❌ table above. Anthropic’s pruning rule: “For each line, ask: Would removing this cause Claude to make mistakes? If not, cut it.” - Learn the full 8-tool context inventory — most operators only know
/clear+/compact. Add/btwand the two/rewinddirections to your daily moves. - Set CLAUDE.md compaction directives. Add lines like “When compacting, always preserve the full list of modified files and any test commands” so compaction never strips load-bearing context.
- Add verification to your default workflow — tests/screenshots/expected outputs in the initial prompt. Anthropic’s framing: “the single highest-leverage thing you can do.”
- Use subagents for any investigation that would touch 5+ files — keeps main context clean.
- Try the Writer/Reviewer pattern the next time you ship something tricky. Two sessions, two worktrees.
- Use Claude to interview you on the next bigger feature — start with a minimal prompt + the interview meta-prompt above; let it produce a spec; start a fresh session to execute it.
- Skip the plan when scope is one sentence. Anthropic explicitly calls out the over-use of plan mode for trivial fixes.
- For batch migrations, use the fan-out template above with
--allowedToolsscoping. Test on 2-3 files before running at scale.