跳至主要內容
AISeeker

搜尋

搜尋報導、主題、公司…

CH.05教學
閱讀時間 3 分鐘
訊號強度: 4/5重大
已查證 · 6 資料來源

Claude Code 上手:記憶檔、子代理、Hooks 與 Skills

在真實專案中為 Claude Code 建立分層設定:專案記憶、審查子代理、確定性 hooks、按需載入的 skills 與團隊外掛。

關鍵數字

<200
每個 CLAUDE.md 建議行數上限
4 hops
@import 最大巢狀深度

用預設值直接跑 Claude Code,它很容易忘記專案慣例、格式也不一致。本教學在既有程式庫中建立一套小而分層的設定:持久的專案指示、一個唯讀的程式碼審查子代理(subagent)、兩個 hooks、一個 skill,以及可分享的外掛。你需要已安裝 Claude Code、付費的 Claude 方案或 API(Console)帳號、git,hook 範例另需 jq。以下內容均已於 2026 年 11 月下旬對照官方文件確認。

打好基礎

1. 安裝並檢查環境

在 macOS、Linux 與 WSL 上,官方建議使用原生安裝程式,它會在背景自動更新。

bash
curl -fsSL https://claude.ai/install.sh | bash
claude --version
claude doctor   # read-only install and settings diagnostics

2. 給專案一份記憶

在工作階段中執行 /init。Claude 會掃描程式碼,草擬一份包含建置指令與慣例的 CLAUDE.md;若檔案已存在,它會提出改進建議而不是直接覆寫。接著動手刪減。Anthropic 建議每個檔案控制在 200 行以內,因為每一行都會在每次工作階段載入。

檔案範圍要提交嗎?
~/.claude/CLAUDE.md你自己,所有專案否
./CLAUDE.md 或 ./.claude/CLAUDE.md整個團隊是
./CLAUDE.local.md你自己,此專案否(加入 gitignore)

只對部分程式碼有意義的指示,應放進 .claude/rules/。帶有 paths 欄位的規則,只有在 Claude 讀取或編輯符合的檔案時才會載入:

markdown
---
paths:
  - "src/api/**/*.ts"
---

# API rules
- Validate every request body with the shared zod schemas.
- Return errors in the standard { code, message } shape.

你可以用 @path/to/file 匯入其他檔案(路徑相對於匯入它的檔案,最多四層),但匯入只是整理文字,並不會降低 context 成本。如果專案已有給其他代理用的 AGENTS.md,就在 CLAUDE.md 開頭寫上 @AGENTS.md,讓兩邊共用同一份來源。

委派與強制

3. 加入一個專注的子代理

子代理在自己的 context 視窗中執行,冗長的程式碼審查不會塞滿主對話。專案層級的子代理放在 .claude/agents/,只有 name 與 description 是必填。

markdown
---
name: reviewer
description: Reviews a diff for bugs, missing tests and security issues. Use after code changes, before committing.
tools: Read, Grep, Glob, Bash
model: sonnet
---

You are a strict senior reviewer. Run `git diff` to see the change.
Report concrete problems with file and line, ordered by severity.
Do not edit files.

Claude 依 description 判斷何時委派。tools 允許清單就是你的安全邊界;省略它,子代理就會繼承所有工具。

4. 用 hooks 把「非做不可」變成確定行為

指示只是建議,hooks 才是保證。Hooks 是在生命週期事件觸發時執行的 shell 指令,事件內容會以 JSON 從 stdin 傳入。把以下設定加入 .claude/settings.json,即可在 Claude 每次編輯後自動格式化檔案,並阻擋對敏感路徑的寫入:

json
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          { "type": "command", "command": "f=$(jq -r '.tool_input.file_path') && npx prettier --write --ignore-unknown \"$f\"" }
        ]
      }
    ],
    "PreToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          { "type": "command", "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/protect-files.sh" }
        ]
      }
    ]
  }
}

防護腳本讀取目標路徑,以結束碼 2 阻擋該次呼叫;輸出到 stderr 的內容會回傳給 Claude 作為理由。

bash
#!/bin/bash
FILE_PATH=$(jq -r '.tool_input.file_path // empty')
for p in ".env" "package-lock.json" ".git/"; do
  if [[ "$FILE_PATH" == *"$p"* ]]; then
    echo "Blocked: $FILE_PATH is protected" >&2
    exit 2
  fi
done
exit 0

存成 .claude/hooks/protect-files.sh,執行 chmod +x,再於工作階段輸入 /hooks 確認兩個 hooks 都已註冊。

打包與分享

5. 把流程寫成 skills

一個 skill 就是一個含 SKILL.md 的資料夾。在 Claude 判斷需要之前,只有它的描述會佔用 context,所以冗長的檢查清單與參考資料最適合放在這裡。自訂斜線指令已併入 skills:舊的 .claude/commands/*.md 仍可使用,而每個 skill 也都能用 /skill-name 手動執行。

markdown
---
name: release-notes
description: Drafts release notes from merged commits. Use when asked for a changelog or release summary.
disable-model-invocation: true
allowed-tools: Bash(git log *)
---

## Recent commits
!`git log --oneline -30`

Group the commits above into Features, Fixes and Internal. One line each.

存成 .claude/skills/release-notes/SKILL.md。!`command` 那一行會在 Claude 看到 skill 之前先執行;disable-model-invocation: true 則代表只有你輸入 /release-notes 時才會執行。

6. 以外掛形式分享

外掛(plugin)可以打包 skills、子代理、hooks 與 MCP 伺服器。Anthropic 官方 marketplace 會在互動式工作階段中自動註冊:

code
/plugin install commit-commands@claude-plugins-official
/plugin marketplace add your-org/claude-plugins

選擇專案範圍會把外掛寫入 .claude/settings.json,但每位協作者仍須在每個 clone 中安裝一次(claude plugin install <name>@<marketplace> --scope project)。

常見陷阱

  • Context 膨脹。 過長的 CLAUDE.md、大量常駐外掛與匯入的文件,每一輪都在消耗 token。可用 claude plugin details <name> 查看某個外掛增加了多少。
  • AGENTS.md 被悄悄蓋掉。 新版只有在沒有 CLAUDE.md 或 CLAUDE.local.md 時才讀取 AGENTS.md,因此新增一份個人用的 CLAUDE.local.md,可能讓 AGENTS.md 不再載入。明確匯入就能避免意外。
  • Hooks 以你的身分執行。 它們擁有你這個使用者的權限。任何專案或外掛中的 hook 腳本,信任前都要先看過。
  • 互相矛盾。 兩份指示檔若說法衝突,Claude 可能任選其一。請定期檢查。

下一步

第 2 步與第 4 步就能帶來大部分效益。當你發現自己一再重複同樣的要求時,再加入子代理與 skills;等設定穩定、隊友也想用時,再把它升級成外掛。

本文在編輯審閱下,以 AI 輔助進行研究與撰寫。如發現錯誤,請來信 support@aiseeker.dev。

資料來源

本報導綜合以下來源撰寫,並標示一手來源。

  1. [1]
    How Claude remembers your project

    Anthropic (Claude Code Docs)2026.11.28一手來源

  2. [2]
    Create custom subagents

    Anthropic (Claude Code Docs)2026.11.28一手來源

  3. [3]
    Automate actions with hooks

    Anthropic (Claude Code Docs)2026.11.28一手來源

  4. [4]
    Extend Claude with skills

    Anthropic (Claude Code Docs)2026.11.28一手來源

  5. [5]
    Install and manage plugins

    Anthropic (Claude Code Docs)2026.11.28一手來源

  6. [6]
    Advanced setup

    Anthropic (Claude Code Docs)2026.11.28一手來源

返回教學 →