Loading...
    • 開發者指南
    • API 參考
    • MCP
    • 資源
    • 發行說明
    Search...
    ⌘K
    入門
    Claude 簡介快速開始
    模型與定價
    模型概覽選擇模型Claude 4.6 新功能遷移指南模型棄用定價
    使用 Claude 構建
    功能概覽使用 Messages API處理停止原因提示詞最佳實踐
    上下文管理
    上下文視窗壓縮上下文編輯
    功能
    提示詞快取延伸思考自適應思考思考力度串流訊息批次處理引用多語言支援Token 計數嵌入視覺PDF 支援Files API搜尋結果結構化輸出
    工具
    概覽如何實作工具使用細粒度工具串流Bash 工具程式碼執行工具程式化工具呼叫電腦使用工具文字編輯器工具網頁擷取工具網頁搜尋工具記憶工具工具搜尋工具
    Agent Skills
    概覽快速開始最佳實踐企業級 Skills透過 API 使用 Skills
    Agent SDK
    概覽快速開始TypeScript SDKTypeScript V2(預覽版)Python SDK遷移指南
    串流輸入即時串流回應處理停止原因處理權限使用者核准與輸入使用 hooks 控制執行工作階段管理檔案檢查點SDK 中的結構化輸出託管 Agent SDK安全部署 AI 代理修改系統提示詞SDK 中的 MCP自訂工具SDK 中的子代理SDK 中的斜線命令SDK 中的 Agent Skills追蹤成本與用量待辦清單SDK 中的外掛
    API 中的 MCP
    MCP 連接器遠端 MCP 伺服器
    第三方平台上的 Claude
    Amazon BedrockMicrosoft FoundryVertex AI
    提示詞工程
    概覽提示詞產生器使用提示詞範本提示詞改進器清晰直接使用範例(多範例提示)讓 Claude 思考(CoT)使用 XML 標籤賦予 Claude 角色(系統提示詞)串聯複雜提示詞長上下文技巧延伸思考技巧
    測試與評估
    定義成功標準開發測試案例使用評估工具降低延遲
    強化防護機制
    減少幻覺提高輸出一致性防範越獄攻擊串流拒絕減少提示詞洩漏讓 Claude 保持角色
    管理與監控
    Admin API 概覽資料駐留工作區用量與成本 APIClaude Code Analytics API零資料保留
    Console
    Log in
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Partners

    • Amazon Bedrock
    • Google Cloud's Vertex AI

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Help and security

    • Availability
    • Status
    • Support
    • Discord

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    指南

    修改系統提示詞

    了解如何透過三種方法自訂 Claude 的行為:輸出樣式、使用 append 的 systemPrompt,以及自訂系統提示詞。

    Was this page helpful?

    • 方法 1:CLAUDE.md 檔案(專案級指令)
    • 方法 2:輸出樣式(持久性配置)
    • 方法 3:使用帶有 append 的 systemPrompt
    • 方法 4:自訂系統提示詞
    • 何時使用 CLAUDE.md
    • 何時使用帶有 append 的 systemPrompt
    • 何時使用自訂 systemPrompt

    系統提示詞定義了 Claude 的行為、能力和回應風格。Claude Agent SDK 提供三種自訂系統提示詞的方式:使用輸出樣式(持久性、基於檔案的配置)、附加到 Claude Code 的提示詞,或使用完全自訂的提示詞。

    了解系統提示詞

    系統提示詞是塑造 Claude 在整個對話中行為方式的初始指令集。

    預設行為: Agent SDK 預設使用最小系統提示詞。它僅包含基本的工具指令,但省略了 Claude Code 的編碼指南、回應風格和專案上下文。要包含完整的 Claude Code 系統提示詞,請在 TypeScript 中指定 systemPrompt: { preset: "claude_code" },或在 Python 中指定 system_prompt={"type": "preset", "preset": "claude_code"}。

    Claude Code 的系統提示詞包括:

    • 工具使用指令和可用工具
    • 程式碼風格和格式指南
    • 回應語氣和詳細程度設定
    • 安全性和安全指令
    • 關於當前工作目錄和環境的上下文

    修改方法

    方法 1:CLAUDE.md 檔案(專案級指令)

    CLAUDE.md 檔案提供專案特定的上下文和指令,當 Agent SDK 在目錄中執行時會自動讀取。它們作為專案的持久性「記憶」。

    CLAUDE.md 如何與 SDK 配合使用

    位置和發現:

    • 專案級: 工作目錄中的 CLAUDE.md 或 .claude/CLAUDE.md
    • 使用者級: ~/.claude/CLAUDE.md 用於所有專案的全域指令

    重要: SDK 僅在您明確配置 settingSources(TypeScript)或 setting_sources(Python)時才會讀取 CLAUDE.md 檔案:

    • 包含 'project' 以載入專案級 CLAUDE.md
    • 包含 'user' 以載入使用者級 CLAUDE.md(~/.claude/CLAUDE.md)

    claude_code 系統提示詞預設不會自動載入 CLAUDE.md——您還必須指定設定來源。

    內容格式: CLAUDE.md 檔案使用純 markdown,可以包含:

    • 編碼指南和標準
    • 專案特定的上下文
    • 常用命令或工作流程
    • API 慣例
    • 測試需求

    CLAUDE.md 範例

    # Project Guidelines
    
    ## Code Style
    
    - Use TypeScript strict mode
    - Prefer functional components in React
    - Always include JSDoc comments for public APIs
    
    ## Testing
    
    - Run `npm test` before committing
    - Maintain >80% code coverage
    - Use jest for unit tests, playwright for E2E
    
    ## Commands
    
    - Build: `npm run build`
    - Dev server: `npm run dev`
    - Type check: `npm run typecheck`

    在 SDK 中使用 CLAUDE.md

    何時使用 CLAUDE.md

    最適合:

    • 團隊共享上下文 - 每個人都應遵循的指南
    • 專案慣例 - 編碼標準、檔案結構、命名模式
    • 常用命令 - 專案特定的建置、測試、部署命令
    • 長期記憶 - 應在所有工作階段中持續存在的上下文
    • 版本控制的指令 - 提交到 git 以保持團隊同步

    主要特性:

    • ✅ 在專案中的所有工作階段中持續存在
    • ✅ 透過 git 與團隊共享
    • ✅ 自動發現(無需程式碼變更)
    • ⚠️ 需要透過 settingSources 載入設定

    方法 2:輸出樣式(持久性配置)

    輸出樣式是修改 Claude 系統提示詞的已儲存配置。它們以 markdown 檔案形式儲存,可以在不同工作階段和專案中重複使用。

    建立輸出樣式

    使用輸出樣式

    建立後,透過以下方式啟用輸出樣式:

    • CLI:/output-style [style-name]
    • 設定:.claude/settings.local.json
    • 建立新的:/output-style:new [description]

    SDK 使用者注意: 當您在選項中包含 settingSources: ['user'] 或 settingSources: ['project'](TypeScript)/ setting_sources=["user"] 或 setting_sources=["project"](Python)時,輸出樣式會被載入。

    方法 3:使用帶有 append 的 systemPrompt

    您可以使用 Claude Code 預設搭配 append 屬性,在保留所有內建功能的同時添加自訂指令。

    方法 4:自訂系統提示詞

    您可以提供自訂字串作為 systemPrompt,以完全用您自己的指令替換預設值。

    四種方法的比較

    功能CLAUDE.md輸出樣式帶有 append 的 systemPrompt自訂 systemPrompt
    持久性每個專案的檔案儲存為檔案僅限工作階段僅限工作階段
    可重用性每個專案跨專案程式碼重複程式碼重複
    管理方式在檔案系統上CLI + 檔案在程式碼中在程式碼中
    預設工具保留保留保留遺失(除非包含)
    內建安全性維持維持

    注意:「帶有 append」表示在 TypeScript 中使用 systemPrompt: { type: "preset", preset: "claude_code", append: "..." },或在 Python 中使用 system_prompt={"type": "preset", "preset": "claude_code", "append": "..."}。

    使用案例和最佳實踐

    何時使用 CLAUDE.md

    最適合:

    • 專案特定的編碼標準和慣例
    • 記錄專案結構和架構
    • 列出常用命令(建置、測試、部署)
    • 應進行版本控制的團隊共享上下文
    • 適用於專案中所有 SDK 使用的指令

    範例:

    • 「所有 API 端點應使用 async/await 模式」
    • 「提交前執行 npm run lint:fix」
    • 「資料庫遷移位於 migrations/ 目錄中」

    重要: 要載入 CLAUDE.md 檔案,您必須明確設定 settingSources: ['project'](TypeScript)或 setting_sources=["project"](Python)。沒有此設定,claude_code 系統提示詞預設不會自動載入 CLAUDE.md。

    何時使用輸出樣式

    最適合:

    • 跨工作階段的持久性行為變更
    • 團隊共享的配置
    • 專業化助手(程式碼審查員、資料科學家、DevOps)
    • 需要版本控制的複雜提示詞修改

    範例:

    • 建立專用的 SQL 最佳化助手
    • 建構以安全為重點的程式碼審查員
    • 開發具有特定教學法的教學助手

    何時使用帶有 append 的 systemPrompt

    最適合:

    • 添加特定的編碼標準或偏好
    • 自訂輸出格式
    • 添加領域特定的知識
    • 修改回應詳細程度
    • 在不遺失工具指令的情況下增強 Claude Code 的預設行為

    何時使用自訂 systemPrompt

    最適合:

    • 完全控制 Claude 的行為
    • 專業化的單次工作階段任務
    • 測試新的提示詞策略
    • 不需要預設工具的情況
    • 建構具有獨特行為的專業化代理

    組合方法

    您可以組合這些方法以獲得最大的靈活性:

    範例:輸出樣式搭配工作階段特定的補充

    另請參閱

    • 輸出樣式 - 完整的輸出樣式文件
    • TypeScript SDK 指南 - 完整的 SDK 使用指南
    • 配置指南 - 一般配置選項
    import { query } from "@anthropic-ai/claude-agent-sdk";
    
    // 重要:您必須指定 settingSources 以載入 CLAUDE.md
    // 僅使用 claude_code 預設不會載入 CLAUDE.md 檔案
    const messages = [];
    
    for await (const message of query({
      prompt: "Add a new React component for user profiles",
      options: {
        systemPrompt: {
          type: "preset",
          preset: "claude_code", // 使用 Claude Code 的系統提示詞
        },
        settingSources: ["project"], // 從專案載入 CLAUDE.md 所必需
      },
    })) {
      messages.push(message);
    }
    
    // 現在 Claude 可以從 CLAUDE.md 存取您的專案指南
    import { writeFile, mkdir } from "fs/promises";
    import { join } from "path";
    import { homedir } from "os";
    
    async function createOutputStyle(
      name: string,
      description: string,
      prompt: string
    ) {
      // 使用者級:~/.claude/output-styles
      // 專案級:.claude/output-styles
      const outputStylesDir = join(homedir(), ".claude", "output-styles");
    
      await mkdir(outputStylesDir, { recursive: true });
    
      const content = `---
    name: ${name}
    description: ${description}
    ---
    
    ${prompt}`;
    
      const filePath = join(
        outputStylesDir,
        `${name.toLowerCase().replace(/\s+/g, "-")}.md`
      );
      await writeFile(filePath, content, "utf-8");
    }
    
    // 範例:建立程式碼審查專家
    await createOutputStyle(
      "Code Reviewer",
      "Thorough code review assistant",
      `You are an expert code reviewer.
    
    For every code submission:
    1. Check for bugs and security issues
    2. Evaluate performance
    3. Suggest improvements
    4. Rate code quality (1-10)`
    );
    import { query } from "@anthropic-ai/claude-agent-sdk";
    
    const messages = [];
    
    for await (const message of query({
      prompt: "Help me write a Python function to calculate fibonacci numbers",
      options: {
        systemPrompt: {
          type: "preset",
          preset: "claude_code",
          append:
            "Always include detailed docstrings and type hints in Python code.",
        },
      },
    })) {
      messages.push(message);
      if (message.type === "assistant") {
        console.log(message.message.content);
      }
    }
    import { query } from "@anthropic-ai/claude-agent-sdk";
    
    const customPrompt = `You are a Python coding specialist.
    Follow these guidelines:
    - Write clean, well-documented code
    - Use type hints for all functions
    - Include comprehensive docstrings
    - Prefer functional programming patterns when appropriate
    - Always explain your code choices`;
    
    const messages = [];
    
    for await (const message of query({
      prompt: "Create a data processing pipeline",
      options: {
        systemPrompt: customPrompt,
      },
    })) {
      messages.push(message);
      if (message.type === "assistant") {
        console.log(message.message.content);
      }
    }
    維持
    必須自行添加
    環境上下文自動自動自動必須自行提供
    自訂程度僅限添加替換預設僅限添加完全控制
    版本控制隨專案是隨程式碼隨程式碼
    範圍專案特定使用者或專案程式碼工作階段程式碼工作階段
    import { query } from "@anthropic-ai/claude-agent-sdk";
    
    // 假設「Code Reviewer」輸出樣式已啟用(透過 /output-style)
    // 添加工作階段特定的重點領域
    const messages = [];
    
    for await (const message of query({
      prompt: "Review this authentication module",
      options: {
        systemPrompt: {
          type: "preset",
          preset: "claude_code",
          append: `
            For this review, prioritize:
            - OAuth 2.0 compliance
            - Token storage security
            - Session management
          `,
        },
      },
    })) {
      messages.push(message);
    }