Skip to content

Claude Code CLI Reference: Commands, Flags, and Workflows

CoreConceptJuly 31, 202610 min read

The Claude Code CLI has grown from claude plus a few convenience flags into a full control surface: interactive sessions, one-shot SDK-style calls, background agents, remote control, MCP servers, plugins, permission modes, worktrees, structured output, and diagnostics. A flat reference table is accurate, but it does not answer the question developers actually have: which command belongs in which workflow?

This guide reorganizes the official CLI reference into a practical map, current as of July 2026. We will follow one running task, PAY-217: stop duplicate refund webhooks, and choose commands by job: start, automate, resume, isolate, authorize, extend, diagnose, and clean up. For the mechanics inside one turn, see How Claude Code Works; for long-running conversations, pair this with How to Manage Claude Code Sessions.

Claude Code CLI map across sessions, automation, state, permissions, extensions, and operations
Claude Code CLI map across sessions, automation, state, permissions, extensions, and operations

Use the CLI by Job, Not Alphabetically

The top-level claude command has three major personalities. First, it can open an interactive coding session: claude, claude "explain this project", or claude --permission-mode plan. Second, it can run as a non-interactive command with -p/--print, which is what you want for scripts, CI checks, JSON output, and repeatable automation. Third, it can manage the environment around sessions: auth, update/install, background agents, MCP servers, plugins, diagnostics, project state, and remote control.

That separation prevents a common mistake: treating every flag as if it configures the same kind of run. --model, --effort, --permission-mode, --add-dir, and --mcp-config shape a session. --output-format, --json-schema, --max-turns, and --max-budget-usd matter most in print mode. claude agents, attach, logs, stop, respawn, and daemon manage background work. project purge deletes local Claude Code state for a project, so it belongs in cleanup, not normal task flow.

CLI families by job
JobUseExamples
Start workInteractive codingclaude, claude "query", --permission-mode plan
Script workOne-shot outputclaude -p, pipes, --output-format json, --json-schema
Resume workSession lifecycle--continue, --resume, --fork-session, --worktree
Control riskPermissions and tools--allowedTools, --disallowedTools, --tools, --safe-mode
Extend toolsMCP and pluginsclaude mcp, --mcp-config, claude plugin
OperateBackground and diagnosticsclaude agents, attach, doctor, logs, project purge
Knowledge map: choose Claude Code CLI commands by workflow job
Knowledge map: choose Claude Code CLI commands by workflow job

Quick reference

  • Use interactive mode when you expect a conversation, file edits, and approval prompts.
  • Use -p when the caller is a script and needs a bounded response, JSON, stream-json, a turn limit, or a dollar budget.
  • Use background-agent commands when the work should continue after the current terminal is free.
  • Use cleanup commands such as project purge only after previewing with --dry-run; they affect local transcripts and project state.
  • Official docs note that claude --help may not list every supported flag, so treat docs and release notes as the source of truth before scripting.

Remember this

The Claude Code CLI is easier to reason about as six jobs: start, script, resume, restrict, extend, and operate.

Interactive Sessions and Print Mode Are Different Contracts

Interactive mode is optimized for human-in-the-loop coding. Run claude in payments-api, start with claude "fix PAY-217", or pass --permission-mode plan to force a read-and-plan pass before edits. You can adjust the session with flags such as --model, --effort, --advisor, --add-dir, --settings, --mcp-config, --plugin-dir, and --worktree. The output is a terminal experience: prompts, tool summaries, diffs, and approval flows.

Print mode, claude -p, is a different contract. It runs the agentic workflow, prints the answer, then exits. That makes it usable from shell scripts, pipes, and CI: cat logs.txt | claude -p "summarize likely root cause", claude -p --output-format json "inspect this diff", or claude -p --max-turns 3 --max-budget-usd 2.00 "triage failing tests". Print mode is also where structured output flags matter, because another program can consume the result.

Print mode converts a Claude Code run into a scriptable input-output contract
Print mode converts a Claude Code run into a scriptable input-output contract

Quick reference

  • claude -p "query" is the SDK-style one-shot path; it can also process piped stdin.
  • --output-format accepts text, JSON, or stream-json depending on how you want to consume results.
  • --json-schema validates final JSON output in print mode; invalid schemas now fail instead of silently producing unstructured text.
  • --max-turns and --max-budget-usd are useful guardrails for unattended runs, especially when subagents are allowed.
  • Use --bare for faster scripted calls that skip project customizations, or --safe-mode to troubleshoot broken customizations while keeping normal auth/model behavior.
Interactive triage
1claude --permission-mode plan "Investigate PAY-217 duplicate refund webhook"2 3# Human reviews the plan, then allows edits and test commands.
Scripted log summary
1cat stripe-webhook.log | claude -p \2  --output-format json \3  --max-turns 2 \4  "Return duplicate webhook evidence with file/line hints"

Remember this

Interactive mode is a human terminal contract; print mode is an automation contract, so budget, turn limits, and machine-readable output matter more there.

Resume, Fork, Worktree, and Background Agents Control State

Claude Code sessions persist locally, which is why --continue and --resume are real workflow tools rather than conveniences. claude -c continues the most recent conversation in the current directory. claude --resume <id-or-name> resumes a specific session or opens a picker. Add --fork-session when you want the old conversation copied into a new session ID so you can try a risky direction without overwriting the original conversation path.

Filesystem isolation is a separate question. A session fork copies conversation state; it does not create a separate checkout. Use --worktree when the task needs an isolated git worktree under .claude/worktrees/, such as testing a refactor while the main checkout stays untouched. Use --bg/--background, /background, claude agents, attach, logs, stop, respawn, and rm when the work should continue outside the foreground terminal. The important distinction is conversation state, file state, and process state: they are related, but not the same thing.

Session lifecycle separates conversation state, file state, and background process state
Session lifecycle separates conversation state, file state, and background process state

Quick reference

  • --continue resumes the latest relevant conversation; --resume targets a session by ID/name or opens the picker.
  • --fork-session creates a new conversation identity from an existing conversation; the working directory is still shared unless you also use a worktree.
  • --worktree gives file isolation by creating a separate git checkout; it is the safer option for competing fixes or experiments.
  • claude agents --json is the scriptable view of active background sessions; attach <id> brings one back into the terminal.
  • respawn <id> restarts a background session with its conversation intact; rm <id> removes it from the active list while leaving the transcript on disk.

Remember this

Resume controls conversation state, worktrees control file state, and background-agent commands control process state.

Permissions Are Flags, Rules, and Modes Working Together

Permission flags decide which tool requests execute without a prompt, which are blocked, and which tools the model even sees. --allowedTools/--allowed-tools pre-approves matching calls. --disallowedTools/--disallowed-tools denies matching calls; a bare tool name can remove that tool from the context, while scoped patterns such as Bash(rm *) deny only matching inputs. --tools restricts built-in tools, and the docs are explicit that it does not affect MCP tools; use a deny rule such as mcp__* or strict MCP config when you need to remove those too.

Modes decide the fallback behavior. --permission-mode plan starts read-only planning. --permission-mode auto uses auto mode. --permission-mode bypassPermissions or --dangerously-skip-permissions removes prompts and should only be used inside a real sandbox. --allow-dangerously-skip-permissions adds bypass mode to the Shift+Tab cycle without starting there. For PAY-217, a reasonable first pass is plan mode plus deny rules for secrets: let Claude read code and propose a route, then approve edits after the plan is visible.

Permission detail: a Claude Code tool request must pass catalog, allow/deny rules, and session mode
Permission detail: a Claude Code tool request must pass catalog, allow/deny rules, and session mode

Quick reference

  • Allow rules reduce prompts; deny rules enforce boundaries; --tools changes the built-in tool catalog.
  • MCP tools are separate from built-in tools, so a restricted built-in list does not automatically remove external MCP tools.
  • --permission-prompt-tool lets a non-interactive run delegate approval to an MCP tool, but it cannot approve MCP tools that require user interaction.
  • Plan mode is safer than hoping the model will avoid edits; the harness blocks side effects.
  • Bypass mode is an execution policy, not a trust signal; use it only in disposable environments.
First pass on unknown repo
1claude \2  --permission-mode plan \3  --disallowedTools "Read(.env*)" "Bash(git push *)" \4  "Investigate PAY-217 and propose a fix"
Narrow scripted check
1claude -p \2  --tools "Read,Grep,Glob" \3  --disallowedTools "mcp__*" \4  "Find refund webhook handlers without editing files"

Remember this

Tool restriction is not one switch: allow rules reduce friction, deny rules block risk, modes set fallback behavior, and MCP tools need their own boundary.

MCP, Plugins, and Settings Extend the Session

Claude Code can load extra capability at launch or manage it with subcommands. claude mcp configures MCP servers; claude mcp login <name> and logout <name> handle OAuth for configured servers from the command line. --mcp-config loads MCP server definitions from JSON files or strings, while --strict-mcp-config ignores every other MCP configuration and uses only what the current launch supplied. That strict mode is useful for reproducible automation because it avoids invisible tools from user or project settings.

Plugins are managed separately with claude plugin or claude plugins, and session-only plugin loading is available with --plugin-dir or --plugin-url. Settings can come from user, project, local, or explicit --settings; --setting-sources chooses which setting layers load. For a stable PAY-217 review script, prefer explicit settings, strict MCP config, and a narrow tool list. For daily interactive work, use project settings, plugins, and MCP servers so the session feels integrated with the repo.

MCP, plugins, settings, and added directories extend a Claude Code session in different ways
MCP, plugins, settings, and added directories extend a Claude Code session in different ways

Quick reference

  • claude mcp login <server> supports OAuth without opening the interactive /mcp panel; --no-browser helps over SSH.
  • --strict-mcp-config --mcp-config ./mcp.json makes the active MCP set explicit for the current launch.
  • --plugin-dir and --plugin-url load plugins for the session only; plugin management is handled by claude plugin/plugins.
  • --settings can point to a JSON file or inline JSON and overrides matching keys from settings files for that launch.
  • --add-dir grants file access to extra directories, but most .claude/ configuration is not discovered from those added directories.

Remember this

Use strict MCP config and explicit settings for reproducible automation; use project configuration and plugins for everyday interactive ergonomics.

Diagnostics and Cleanup Commands Are Operational Tools

The CLI also owns health and lifecycle operations. claude auth login, logout, and status handle account state; claude update and claude install [version|stable|latest] handle the binary. claude doctor prints read-only installation and settings diagnostics without starting a session. claude daemon status and daemon stop --any inspect or stop the background-session supervisor. claude logs <id> gives recent output from a background session; claude stop <id> or kill <id> stops one.

Cleanup needs more caution. claude project purge [path] can delete local Claude Code state for a project, including transcripts, task lists, debug logs, file-edit history, prompt history, and the project entry in ~/.claude.json. It has --dry-run, --interactive, --all, and --yes options. That means it is the right tool when you need to clear sensitive local state or reset a broken project record, but the wrong tool for ordinary context cleanup; use /clear, /compact, or a new session when you only want a fresh conversation.

Operational CLI flow for auth, install health, background supervisor, logs, and project cleanup
Operational CLI flow for auth, install health, background supervisor, logs, and project cleanup

Quick reference

  • claude auth status has machine-friendly behavior: it exits 0 when logged in and 1 when not, with JSON output by default.
  • claude doctor is read-only from the terminal; the in-session /doctor can diagnose and offer fixes.
  • daemon stop --any --keep-workers can stop the supervisor while leaving background sessions for the next supervisor to reconnect.
  • project purge --dry-run should come before any destructive cleanup, especially on shared machines.
  • setup-token prints a long-lived OAuth token for CI/scripts and does not save it; treat the terminal output as sensitive.

Remember this

Operational commands fix auth, installs, daemons, logs, and local state; use purge only when you mean to delete project-local Claude Code data.

Key takeaway

The shortest mental model is this: claude starts a session, claude -p gives automation a bounded result, resume flags recover conversation state, worktrees isolate file state, permission flags define the safety boundary, MCP/plugins extend capability, and doctor/daemon/project commands operate the installation around the agent.

Practice (25 min): In a scratch repo, run one task three ways. First: claude --permission-mode plan "inspect this repo". Second: claude -p --output-format json --max-turns 2 "summarize test commands". Third: claude --worktree cli-practice "make a harmless README change". Intentional failure: rerun the print command with --tools "" and confirm the result cannot inspect files. Recovery: allow only Read,Grep,Glob and rerun. Pass when you can explain which boundary changed: conversation mode, output contract, file isolation, or tool catalog. Official source checked July 2026: code.claude.com/docs/en/cli-reference, plus the official Commands, Settings, and MCP reference pages.

Share:

Related Articles

Close a Claude Code session mid-task and the natural fear is that you just lost an afternoon of debugging context — the

Read

Open a chat with an LLM and ask it to fix a bug, and the best you get back is a suggested diff you copy into your editor

Read

Modern coding agents are no longer just chat boxes beside your editor. The useful power comes from controls around the l

Read

Explore this topic

Keep learning

Follow a structured path or browse all courses to go deeper.