Claude Code: Interactive Mode and Operation Guide

Last updated: 2026-08-31

Claude Code offers multiple interaction modes, from terminal conversations to pipeline automation, each suited for different scenarios.

💡 Tip: Interactive mode is for conversing with Claude Code; pipeline mode is for scripts calling Claude Code; headless mode is for UI-free automation.

📋 Prerequisites: Chapter 6 - Project Initialization and Structure

1. What You'll Learn


2. Three Interaction Modes

(1) Mode Overview

Mode Launch Method Use Case Human Interaction
Interactive claude Daily dev, exploratory coding Full interaction
Pipeline echo "..." | claude Quick one-off tasks Semi-interactive
Headless claude --headless CI/CD, automation scripts No interaction

(2) Mode Selection Decision

100%
graph TD
    A[Need Claude Code] --> B{Need interaction?}
    B -->|Yes| C{Single or multi-turn?}
    B -->|No| D[Headless mode]
    C -->|Multi-turn| E[Interactive]
    C -->|Single| F[Pipeline mode]
    D --> D1[CI/CD]
    D --> D2[Batch scripts]
    E --> E1[Daily development]
    E --> E2[Complex refactoring]
    F --> F1[Quick query]
    F --> F2[Single file modification]

3. Interactive Mode

(1) Launch and Operations

BASH
# Standard launch
claude

# Launch with initial instruction
claude "Analyze project structure"

# Resume last session
claude --resume

# Specify model
claude --model claude-opus-4-20250514

(2) In-Session Operation Control

Operation Key/Command Description
Submit instruction Enter Send current input
New line Shift+Enter Multi-line input
Interrupt Ctrl+C Stop current operation
Exit Ctrl+D or /exit Exit Claude Code
Clear context /clear Clear conversation history
View help /help Show available commands

▶ Example 1: Interactive Session Flow

TEXT 📖 Display only
$ claude

> Show me the utility functions under src/utils/

Claude: Reading src/utils/...
  - date.ts      formatDate(), relativeTime()
  - string.ts    capitalize(), truncate()
  - validation.ts isValidEmail(), isValidPhone()

> Add a slugify function to string.ts

Claude: Adding slugify() to src/utils/string.ts...
  → Reading src/utils/string.ts
  → Adding slugify function with tests
  → Running: npm test -- string.test.ts
  ✅ All tests passed

> Also add a camelToSnake conversion function

Claude: Adding camelToSnake() to src/utils/string.ts...
  → Reading src/utils/string.ts
  → Adding camelToSnake with tests
  → Running: npm test -- string.test.ts
  ✅ All tests passed

4. Pipeline Mode

(1) Basic Usage

BASH
# Pass instruction from stdin
echo "Explain this function's purpose" | claude

# Pass file content to Claude Code
cat src/auth.ts | claude "Review this code's security"

# Combine usage
git diff | claude "Review these changes, identify potential issues"

(2) Output Control

BASH
# Plain text output (no interactive confirmation)
claude -p "List all TODO comments" --output text

# JSON format output
claude -p "Analyze project dependencies" --output json

# Quiet mode (output results only)
echo "Run tests" | claude --quiet

▶ Example 2: Pipeline Mode Practical Applications

BASH
# Code review automation
git diff main..feature | claude "Review changes:
1. Security vulnerabilities
2. Code style compliance
3. Missing tests"

# Batch documentation generation
for file in src/*.ts; do
  echo "Generate JSDoc comments for $file" | claude --quiet >> docs/api.md
done

# Quick query
echo "What outdated dependencies are in package.json?" | claude

5. Headless Mode

(1) Launch Method

BASH
# No UI (auto-approve operations)
claude --headless "Run full tests and fix all failing cases"

# Specify output format
claude --headless --output json "Analyze code quality"

# With CI/CD
claude --headless --allow-full-auto "Code review and auto-fix lint issues"

(2) Security Considerations

Parameter Risk Level Description
--headless Medium Auto-runs but needs pre-approved permissions
--allow-full-auto High Fully automatic, all operations without confirmation
--allowed-tools Low Only allows specified tools

▶ Example 3: Headless Mode in CI/CD

YAML
# GitHub Actions config
name: AI Code Review
on: [pull_request]
jobs:
  review:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install Claude Code
        run: npm install -g @anthropic-ai/claude-code
      - name: Run AI Review
        env:
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
        run: |
          claude --headless --output json \
            "Review this PR's changes, check:
            1. Security vulnerabilities
            2. Performance issues
            3. Code style
            Output JSON format report"

6. Multi-Session Management

(1) Session Resume

BASH
# List historical sessions
claude --list-sessions

# Resume specific session
claude --resume <session-id>

# Resume most recent session
claude --resume

(2) Switching Between Sessions

TEXT 📖 Display only
# Alice handling multiple tasks simultaneously

# Task 1: Frontend refactoring
cd frontend && claude
> Refactor component structure...

# Ctrl+D to exit

# Task 2: Backend API
cd backend && claude
> Add new endpoint...

# Resume task 1
cd frontend && claude --resume

7. Comprehensive Example: Multi-Mode Collaboration

BASH
# Alice's daily development workflow

# Morning: Interactive mode exploration
claude "Check yesterday's TODOs, help me handle each one"

# Noon: Pipeline mode quick fix
git diff | claude "Fix these lint errors" | git apply

# Afternoon: Interactive mode deep development
claude "Implement RBAC model for user permission system"

# Evening: Headless mode full check
claude --headless --allowed-tools Read,Write,Bash \
  "Run full tests, fix all failing cases, don't modify test assertions"

# CI: Headless mode auto review
claude --headless --output json "Code review"

❓ FAQ

Q Can I switch between interactive and pipeline modes?
A No. Mode is selected at launch and cannot be switched during runtime. Exit and relaunch for a different mode.
Q Is headless mode safe?
A --headless only removes the UI; operations still require permission control. But --allow-full-auto auto-approves all operations — high risk, only recommended in CI environments.
Q How to interrupt Claude Code execution?
A Press Ctrl+C to interrupt current operation without losing existing modifications. Ctrl+D exits the entire session.
Q How long is session history retained?
A Session history is saved locally, retaining the last 30 days by default. View with --list-sessions.
Q Does pipeline mode support multi-turn conversation?
A No. Pipeline mode is single input/output. Use interactive mode for multi-turn.
Q How to determine if Claude Code executed successfully in a script?
A Check exit codes: 0=success, 1=failure, 2=interrupted.

📖 Summary


📝 Exercises

  1. Basic (⭐): Complete the same task using both interactive and pipeline modes, compare operation differences.
  2. Intermediate (⭐⭐): Implement automated code review flow with git diff | claude "code review".
  3. Advanced (⭐⭐⭐): Configure a GitHub Actions workflow to auto-run Claude Code code review on PRs.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏