Skip to content

Claude Code — Tips & Tricks

Efficiency reference for the Claude Code CLI. Sources: Claude Code Docs, verified July 2026.

CLAUDE.md (project memory)

  • Keep it under ~200 lines. Split large rule sets into .claude/rules/*.md with glob-scoped sections instead of one giant file.
  • Content: build/test/deploy commands, coding standards, directory layout, "don't touch" zones. Not a chat log substitute.
  • Supports imports — reference other files instead of duplicating.
  • Add explicit compaction guidance, e.g. When compacting, always preserve deployment steps and known gotchas, so critical context survives summarization.
  • Levels: repo root (project-wide) or per-subdirectory (module-specific).

Custom slash commands

  • User-level: ~/.claude/commands/. Project-level (shared via git): .claude/commands/.
  • Markdown file with YAML frontmatter (name, description); body is the instruction template.
  • Can accept arguments and chain into subagents via the Task tool.
  • Use for repeated multi-step procedures (e.g. /deploy-check, /log-triage).

Subagents (Task tool)

  • Defined in .claude/agents/ (shared) or ~/.claude/agents/ (personal); markdown + YAML frontmatter (name, description, tools).
  • Each subagent runs an isolated context window — no memory of the parent conversation unless you pass it explicitly in the prompt.
  • Use for delegable, self-contained work: security review, log analysis, doc generation, parallel investigation across services.
  • ExitPlanMode isn't available inside a subagent unless its own permission mode is plan.
  • Launch independent subagents in a single message to run them concurrently.

Hooks

  • Config: .mcp.json (project) or ~/.claude/hooks.json (user). Fire on lifecycle events: PreToolUse, PostToolUse, PostToolUseFailure, PermissionRequest, PermissionDenied.
  • Guaranteed to execute (unlike prompted behavior) — use for mandatory gates: linting, formatting, audit logging, blocking dangerous commands (e.g. rm -rf, iptables -F).
  • MCP tools referenced in hook payloads as mcp__<server>__<tool>.

Plan mode

  • Start with --permission-mode plan or /mode plan. Claude researches and proposes a plan without executing changes.
  • Apply with the ExitPlanMode approval, or iterate on the plan first.
  • Use before any high-blast-radius change: prod config edits, DNS cutovers, bulk file operations, firewall rule changes.

MCP servers

  • Config: .mcp.json (project root) or ~/.claude/.mcp.json (user default). JSON array: type (stdio/http), command, env, tools.
  • Tools surface as mcp__<server>__<tool>.
  • --bare ignores all MCP/hook/skill config — use for reproducible CI runs.
  • Project-level config takes precedence over user-level.

Context management

  • /clear — fresh session (old one still resumable by ID). Use when switching to an unrelated task.
  • /compact [focus] — manual summarization with optional focus area; respects CLAUDE.md preservation notes.
  • Auto-compaction triggers at the context limit: drops old tool outputs first, then summarizes conversation.
  • Keep CLAUDE.md and tool output verbosity low to delay compaction and reduce token spend.

Extended thinking / reasoning effort

  • Effort levels: low, medium, high (default), xhigh, max. Set with /effort <level> or in model settings.
  • Thinking tokens bill as output tokens — drop effort for routine tasks, raise it for architecture/security decisions.
  • Cap spend with MAX_THINKING_TOKENS env var if needed.

Permission modes

  • --dangerously-skip-permissions (bypassPermissions): no prompts, everything auto-executes. Only in isolated/offline containers — Claude Code refuses to run as root/sudo, and filesystem root + home dir stay protected even in bypass mode.
  • Allowlist specific tools/commands via /permissions or settings.json instead of full bypass when possible.
  • Default to plan mode for anything touching production.

CLI flags & shortcuts worth knowing

  • -p "prompt" / --print — headless mode, reads stdin, prints to stdout. Good for piping logs: cat error.log | claude -p "root-cause this" > analysis.txt.
  • --bare — ignore local/user config for CI reproducibility.
  • --system-prompt / --system-prompt-file — override system prompt (mutually exclusive with each other).
  • --resume <session-id> — reattach to a prior session; /sessions lists IDs.
  • Option/Alt+Enter — newline without submitting. Option/Alt+P — switch models mid-session.
  • /keybindings to customize, stored in ~/.claude/keybindings.json.

Workflow patterns

  • Parallel investigation: git worktrees + separate Claude Code sessions per branch/server, each with its own context — avoids cross-contamination on multi-host incidents.
  • CI usage: claude -p in a pipeline step, --bare flag, exit code 0/non-zero for pass/fail assertions.
  • Cost control: check /monitoring for token spend before large batch runs; lower reasoning effort for repetitive/simple tasks.
  • Stdin cap: 10MB limit on piped input — pass a file path instead of catting huge logs directly for very large inputs.