0Pricing
Claude Architect · 课时

交互式与无头模式

对话式会话与脚本化的非交互式运行。

交互式与无头模式 是 CoddyKit 上的免费 Claude Architect 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Claude Architect 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Claude Architect 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Two Ways to Run Claude Code

Claude Code runs in two fundamentally different modes, and choosing the right one is a real architectural decision.

  • Interactive: a conversational session. You type, Claude responds, you steer, it asks for approvals. A human is in the loop.
  • Headless (non-interactive): a single scripted invocation that runs to completion with no human present. Built for pipelines and automation.

Same engine, very different control model. This lesson shows when each one wins.

The Headless Flag

You switch into headless mode with -p (also written --print). It runs the prompt once, prints the result, and exits. No back-and-forth, no TTY required.

This is the required form for CI/CD pipelines: there is no human to answer prompts inside a build job, so an interactive session would simply hang.

# Interactive: opens a conversational session
claude

# Headless: runs once and exits (required in pipelines)
claude -p "Review the staged diff and flag correctness bugs"

Parseable Output

Interactive output is for humans to read. Headless output is for machines to parse. Use --output-format json so a downstream step can act on the result programmatically.

Pair it with a JSON Schema so the structure is guaranteed and your pipeline never breaks on free-form prose.

claude -p "List failing test files and the root cause for each" \
  --output-format json \
  --output-schema ./schemas/review.json > review.json

# A later CI step reads review.json and decides pass/fail

Who Drives the Decisions?

The deepest difference is who steers.

  • Interactive: the human course-corrects mid-task, approves edits, and resolves ambiguity by being asked.
  • Headless: no one is there to ask. The prompt must be self-contained, the success criteria explicit, and ambiguous input handled by the prompt itself, not by a follow-up question.

If your task genuinely needs a human judgment call partway through, headless is the wrong choice.

Plan Mode Is Interactive

Plan mode belongs to the interactive world. You use it for large changes, multiple possible approaches, or architectural decisions: Claude explores and proposes a plan, then waits for your approval before editing.

That approval gate is a human checkpoint. It has no meaning in a headless run, where there is no one to approve. For single-file fixes or a clear stack trace, you skip plan mode and execute directly anyway.

Headless in CI: Review on Every PR

A classic headless use case: an automated code review on every pull request. The job runs -p, emits JSON, and a script turns findings into comments or a gate.

Crucially, run this review in an isolated session so it is not biased by any generation context. Fresh-instance review beats same-session self-review, where the author keeps its own reasoning and won't challenge itself.

# .github/workflows/review.yml (step)
- name: Claude review
  run: |
    claude -p "$(cat .ci/review-prompt.md)" \
      --output-format json > findings.json
    node .ci/post-comments.js findings.json

Minimize False Positives

A headless reviewer that cries wolf gets muted. Give it explicit criteria instead of vague instructions: "flag a comment only when it contradicts the code" beats "be more precise".

When re-running on a later push, include the prior results and report only new or unfixed issues, so the same noise isn't repeated on every commit.

claude -p "Flag an issue ONLY when it changes runtime behavior or breaks a test. \
Here are last run's findings: $(cat prev-findings.json). \
Report only NEW or still-unfixed issues." \
  --output-format json

Headless Is Not the Batch API

Don't confuse a headless blocking check with the Message Batches API. They solve different problems.

  • Headless -p in CI: synchronous, returns now, gates a merge.
  • Batch API: 50% cheaper, up to a 24h window, no latency SLA, and no multi-turn tool calling.

Use Batch for non-blocking overnight jobs like reports or audits, never for a pre-merge or time-sensitive check that must answer immediately.

Sessions Belong to Interactive Work

Interactive work is conversational and resumable. --resume <name> continues a named session; fork_session branches from a shared point to try alternatives.

One caution: resumed tool results can be stale if the codebase changed since. Sometimes a fresh session seeded with a structured summary is better than resuming an out-of-date one.

# Continue a named conversation later
claude --resume refactor-auth

# Branch from a shared point to explore an alternative
# (fork_session) without disturbing the original thread

Configuration Shared by Both Modes

Both modes read the same project configuration, so a well-set-up repo behaves consistently whether a human or a pipeline invokes it.

  • ./CLAUDE.md (project-level, shared via VCS) carries standards into every run.
  • .claude/skills/ and commands give reusable, scoped behaviors.

Note: user-level ~/.claude/CLAUDE.md is personal and NOT shared via VCS, so your CI runner won't have it. Put anything the headless job depends on in the project scope.

# Headless CI relies on project-scope config, not your machine's:
#   ./CLAUDE.md          -> shared via VCS  (CI sees it)
#   .claude/skills/      -> shared via VCS  (CI sees it)
#   ~/.claude/CLAUDE.md  -> personal only   (CI does NOT see it)

A Decision Checklist

When picking a mode, ask:

  • Is a human present to approve and steer? Yes -> interactive. No -> headless.
  • Does a script need to parse the result? Yes -> headless with --output-format json.
  • Is it a blocking, time-sensitive check? Use synchronous headless, not Batch.
  • Large or architectural change needing approval first? Interactive plan mode.
  • Automated PR review? Headless, isolated session, explicit criteria.

Match the mode to who is in the loop and what consumes the output.

Quick Check

Apply what you've learned to a realistic pipeline decision.

Recap: Interactive vs Headless

Key takeaways:

  • Interactive = conversational, human-in-the-loop; supports plan mode approvals and resumable/forkable sessions.
  • Headless = -p/--print, runs once and exits; required for CI/CD where no human is present.
  • Add --output-format json (plus a schema) for machine-parseable results.
  • Automated review should run in an isolated session with explicit criteria, reporting only new/unfixed issues.
  • Both modes share project-scope config (./CLAUDE.md, .claude/), but not personal ~/.claude config.
  • Headless blocking checks are NOT the Batch API: Batch is cheaper but has no latency SLA and no multi-turn tool calling.

常见问题解答

「交互式与无头模式」课时是免费的吗?

是的 — 「交互式与无头模式」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Claude Architect 课程的其余内容,请升级到 CoddyKit PRO。 Claude Architect 课程共包含 4 节课。

「交互式与无头模式」这节课中我会学到什么?

对话式会话与脚本化的非交互式运行。 你通过在浏览器中直接运行的动手代码来练习 Claude Architect,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Claude Architect 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Claude Architect 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「交互式与无头模式」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Claude Architect 课中编写并运行代码吗?

能。每节 Claude Architect 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 什么是 Claude Code
  2. 交互式与无头模式
  3. Read / Edit / Write 循环
  4. 记忆与压缩命令
← 返回 Claude Architect