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

理解系統提示

系統提示是塑造 Claude 在整個對話中行為的初始指令集。Claude Code 的預設系統提示包括:
  • 工具使用說明和可用工具
  • 程式碼風格和格式指南
  • 回應語調和詳細程度設定
  • 安全和保護說明
  • 關於當前工作目錄和環境的上下文

修改方法

方法 1:輸出樣式(持久化配置)

輸出樣式是修改 Claude 系統提示的已儲存配置。它們以 markdown 檔案形式儲存,可以在會話和專案間重複使用。

建立輸出樣式

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)`
)

使用輸出樣式

建立後,透過以下方式啟用輸出樣式:
  • CLI/output-style [style-name]
  • 設定.claude/settings.local.json
  • 建立新的/output-style:new [description]

方法 2:使用 appendSystemPrompt

appendSystemPrompt 選項將您的自訂指令添加到預設系統提示中,同時保留所有內建功能。
import { query } from "@anthropic-ai/claude-code"

const messages = []

for await (const message of query({
  prompt: "Help me write a Python function to calculate fibonacci numbers",
  options: {
    appendSystemPrompt: "Always include detailed docstrings and type hints in Python code."
  }
})) {
  messages.push(message)
  if (message.type === 'assistant') {
    console.log(message.message.content)
  }
}

方法 3:使用 customSystemPrompt

customSystemPrompt 選項用您的自訂指令完全替換整個預設系統提示。
import { query } from "@anthropic-ai/claude-code"

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: {
    customSystemPrompt: customPrompt
  }
})) {
  messages.push(message)
  if (message.type === 'assistant') {
    console.log(message.message.content)
  }
}

三種方法的比較

功能輸出樣式appendSystemPromptcustomSystemPrompt
持久性✅ 儲存為檔案❌ 僅限會話❌ 僅限會話
可重複使用性✅ 跨專案❌ 程式碼重複❌ 程式碼重複
管理✅ CLI + 檔案⚠️ 在程式碼中⚠️ 在程式碼中
預設工具✅ 保留✅ 保留❌ 遺失(除非包含)
內建安全性✅ 維持✅ 維持❌ 必須添加
環境上下文✅ 自動✅ 自動❌ 必須提供
自訂層級⚠️ 替換預設⚠️ 僅添加✅ 完全控制
版本控制✅ 是✅ 與程式碼一起✅ 與程式碼一起
發現性/output-style❌ 不可發現❌ 不可發現

使用案例和最佳實踐

何時使用輸出樣式

最適合:
  • 跨會話的持久行為變更
  • 團隊共享配置
  • 專門助手(程式碼審查員、資料科學家、DevOps)
  • 需要版本控制的複雜提示修改
範例:
  • 建立專門的 SQL 最佳化助手
  • 建立專注於安全的程式碼審查員
  • 開發具有特定教學法的教學助手

何時使用 appendSystemPrompt

最適合:
  • 添加特定的程式碼標準或偏好
  • 自訂輸出格式
  • 添加領域特定知識
  • 修改回應詳細程度

何時使用 customSystemPrompt

最適合:
  • 完全控制 Claude 的行為
  • 專門的單次會話任務
  • 測試新的提示策略
  • 不需要預設工具的情況

結合方法

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

範例:輸出樣式與會話特定添加

import { query } from "@anthropic-ai/claude-code"

// 假設「Code Reviewer」輸出樣式已啟用(透過 /output-style)
// 添加會話特定的重點領域
const messages = []

for await (const message of query({
  prompt: "Review this authentication module",
  options: {
    appendSystemPrompt: `
      For this review, prioritize:
      - OAuth 2.0 compliance
      - Token storage security
      - Session management
    `
  }
})) {
  messages.push(message)
}

另請參閱