Skip to content

Chapter 9 · Custom Instructions and Configuration ​

Chapter goals: Master Claude Code's global/project configuration system, learn to选择 the right model (Opus/Sonnet/Haiku), and customize the system prompt and output style.

9.1 Configuration Hierarchy ​

Claude Code's configuration分 three levels, priority从高到低:

text
┌─────────────────────────────────────────────┐
│ Priority 1: CLI arguments (--model etc.)      │
├─────────────────────────────────────────────┤
│ Priority 2: Project config .claude/settings.json │
│   (effective only for the current project,可 committed to Git) │
├─────────────────────────────────────────────┤
│ Priority 3: User config ~/.claude/settings.json  │
│   (default values shared across all projects)      │
└─────────────────────────────────────────────┘

Project-level Config Example ​

json
// .claude/settings.json —可 committed to the repo for team sharing
{
  "permissions": {
    "allow": [
      "Bash(npm run lint:*)",
      "Bash(npx jest:*)",
      "Edit"
    ],
    "deny": ["Read(.env*)"]
  },
  "model": "claude-sonnet-4-20250514",
  "env": {
    "NODE_ENV": "development"
  }
}

Team Collaboration

Commit .claude/settings.json to Git; team members自动获得统一的权限规则和模型偏好 after cloning. Personal API Keys and other sensitive info go in the user-level config.

9.2 Model Selection Strategy ​

Claude Code supports switching between multiple models, each with tradeoffs:

ModelCodenameStrengthCostApplicable Scenario
Opusclaude-opus-4-20250514Strongest reasoning, complex architecture designHighestArchitecture decisions,疑难 bugs
Sonnetclaude-sonnet-4-20250514Balanced performance and costMediumDaily coding, code review
Haikuclaude-haiku-20250306Fastest speed, lowest latencyLowestSimple queries, batch operations

Switching Methods ​

bash
# Method 1: Specify at startup
claude --model claude-sonnet-4-20250514

# Method 2: Switch in interactive mode
> /model

# Method 3: Persistent setting in settings.json
{
  "model": "claude-sonnet-4-20250514"
}

Scenario-based Model Pairing ​

bash
# Daily development with Sonnet
alias cc="claude --model claude-sonnet-4-20250514"

# Escalate to Opus for hard problems
alias cco="claude --model claude-opus-4-20250514"

# Quick queries with Haiku
alias cch="claude --model claude-haiku-20250306 -p"

Cost Optimization Tip

For one-off tasks in -p (non-interactive mode), if the question is simple, using Haiku can save over 90% of costs. Reserve Opus for truly depth-reasoning scenarios.

9.3 System Prompt Customization ​

CLAUDE.md is Claude Code's "system prompt" — it defines the行为 boundaries and working style of Claude in the project.

Structured CLAUDE.md ​

markdown
# Project Overview
This is an e-commerce frontend project based on Next.js 14.

## Tech Stack
- Next.js 14 (App Router)
- TypeScript strict mode
- Tailwind CSS + shadcn/ui
- Prisma ORM + PostgreSQL

## Coding Conventions
- Use functional components and hooks; class components are禁止
- All components must have TypeScript type definitions
- Styles use Tailwind utility classes only; no custom CSS
- Error handling统一 uses Result<T, E> pattern

## Common Commands
- Dev server: pnpm dev
- Tests: pnpm test
- Type check: pnpm type-check
- Lint: pnpm lint

## Things Not to Do
- Do not modify prisma/schema.prisma unless explicitly asked
- Do not install new npm packages unless no existing alternative
- Do not修改 files under .github/workflows/

Layered Instructions ​

Large projects can place additional CLAUDE.md files in subdirectories:

text
project/
├── CLAUDE.md              ← Global conventions
├── src/
│   ├── CLAUDE.md          ← src directory专属 rules
│   ├── api/
│   │   └── CLAUDE.md      ← API layer专属 rules
│   └── components/
│       └── CLAUDE.md      ← Component layer专属 rules
└── docs/
    └── CLAUDE.md          ← Docs directory rules

When Claude works in a subdirectory, it自动 loads the CLAUDE.md for that directory and its parents.

9.4 Output Style Settings ​

Control the format and verbosity of Claude's replies:

bash
# Switch output style in interactive mode
> /output-style

# Available styles:
# default      — standard reply format
# explanatory  — with教学 explanations
# concise      — as brief as possible

Custom Output Style ​

Create .claude/output-styles/custom.md:

markdown
# Code Review Mode

You are a strict code reviewer. Replies follow this format:
1. First list discovered issues (sorted by severity)
2. Each issue with a fix suggestion and code snippet
3. Finally give an overall score (1-10)

Always use English comments.
bash
# Use the custom style
> /output-style custom-code-review

9.5 Environment Variables and Advanced Configuration ​

json
{
  "env": {
    "ANTHROPIC_MODEL": "claude-sonnet-4-20250514",
    "CLAUDE_CODE_MAX_OUTPUT_TOKENS": "8192",
    "DISABLE_TELEMETRY": "1",
    "HTTP_PROXY": "http://proxy.company.com:8080"
  }
}
VariablePurpose
ANTHROPIC_MODELOverride the default model
CLAUDE_CODE_MAX_OUTPUT_TOKENSMax tokens per reply
DISABLE_COST_WARNINGSDisable cost warning prompts
HTTP_PROXY / HTTPS_PROXYCorporate network proxy

Chapter Summary ​

  • Three-level config: CLI args > project settings.json > user settings.json;
  • Three models: Opus (strongest) / Sonnet (balanced) / Haiku (fastest and cheapest); choose per scenario;
  • CLAUDE.md is the core customization点; supports subdirectory layering;
  • /output-style controls reply format; custom review styles etc. are可自定义.

🧪 随堂测验

点击你认为正确的选项。答错时会展示正确答案与原因解析。

1. Among the three config levels, which has the highest priority?

2. Which model should you选择 for daily CRUD coding to get the best cost-performance ratio?

3. When is a CLAUDE.md in a subdirectory loaded?

4. For a team wanting to统一 permission rules and model preferences, what is the best practice?

🛠️ Hands-on Practice ​

  1. Create a structured CLAUDE.md for your project, including tech stack, coding conventions, and common commands.
  2. Complete the same code review task using both Haiku and Sonnet; compare quality and cost.
  3. Create a custom output-style (e.g. "teaching mentor mode") and observe changes in Claude's replies.

After completing the exercises, proceed to Chapter 10: MCP Integration and External Tools.