Claude Code CLI Reference: Commands, Flags, and Workflows
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.
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.
| Job | Use | Examples |
|---|---|---|
| Start work | Interactive coding | claude, claude "query", --permission-mode plan |
| Script work | One-shot output | claude -p, pipes, --output-format json, --json-schema |
| Resume work | Session lifecycle | --continue, --resume, --fork-session, --worktree |
| Control risk | Permissions and tools | --allowedTools, --disallowedTools, --tools, --safe-mode |
| Extend tools | MCP and plugins | claude mcp, --mcp-config, claude plugin |
| Operate | Background and diagnostics | claude agents, attach, doctor, logs, project purge |
Quick reference
- Use interactive mode when you expect a conversation, file edits, and approval prompts.
- Use
-pwhen 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 purgeonly after previewing with--dry-run; they affect local transcripts and project state. - Official docs note that
claude --helpmay 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.
Quick reference
claude -p "query"is the SDK-style one-shot path; it can also process piped stdin.--output-formataccepts text, JSON, or stream-json depending on how you want to consume results.--json-schemavalidates final JSON output in print mode; invalid schemas now fail instead of silently producing unstructured text.--max-turnsand--max-budget-usdare useful guardrails for unattended runs, especially when subagents are allowed.- Use
--barefor faster scripted calls that skip project customizations, or--safe-modeto troubleshoot broken customizations while keeping normal auth/model behavior.
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.
Quick reference
--continueresumes the latest relevant conversation;--resumetargets a session by ID/name or opens the picker.--fork-sessioncreates a new conversation identity from an existing conversation; the working directory is still shared unless you also use a worktree.--worktreegives file isolation by creating a separate git checkout; it is the safer option for competing fixes or experiments.claude agents --jsonis 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.
Quick reference
- Allow rules reduce prompts; deny rules enforce boundaries;
--toolschanges 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-toollets 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.
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.
Quick reference
claude mcp login <server>supports OAuth without opening the interactive/mcppanel;--no-browserhelps over SSH.--strict-mcp-config --mcp-config ./mcp.jsonmakes the active MCP set explicit for the current launch.--plugin-dirand--plugin-urlload plugins for the session only; plugin management is handled byclaude plugin/plugins.--settingscan point to a JSON file or inline JSON and overrides matching keys from settings files for that launch.--add-dirgrants 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.
Quick reference
claude auth statushas machine-friendly behavior: it exits 0 when logged in and 1 when not, with JSON output by default.claude doctoris read-only from the terminal; the in-session/doctorcan diagnose and offer fixes.daemon stop --any --keep-workerscan stop the supervisor while leaving background sessions for the next supervisor to reconnect.project purge --dry-runshould come before any destructive cleanup, especially on shared machines.setup-tokenprints 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.
Related Articles
Explore this topic