Customize behavior
Modifying system prompts
Modifying system prompts 这一页讲的,就是 Modifying system prompts 这件事在 Claude Code 里到底怎么用。
页面信息
这页不是官方原文,而是顺着官方文档结构做的中文解释版。命令、参数、配置名这些硬东西尽量保留,解释部分则尽量讲成人能照着做的话。
如果你碰到特别敏感的配置、权限或企业环境差异,最好顺手点上面的“查看原始文档”再核一遍。
这一页先讲明白
这页主要讲 Modifying system prompts:Learn how to customize Claude's behavior by modifying system prompts using three approaches - output styles, systemPrompt with append, and custom system prompts.
你可以把它当成"Customize behavior"这块里专门管这一摊事的说明书。
你可以把"Modifying system prompts"理解成 Customize behavior 这一栏里的一把专门工具。这页不是让你背书,而是教你什么时候该把这把工具拿出来。
原文这页大多会按 Understanding system prompts、Methods of modification、Method 1: CLAUDE.md files (project-level instructions)、How CLAUDE.md works with the SDK 这些环节往下讲。
翻成人话,大概就是:Understanding system prompts
第一,先别一上来全开全配。先按最小一步试通,确认没跑偏,再继续往下加。
第二,命令、配置名、参数名这些硬东西尽量保留原样。人话解释是帮你听懂,不是帮你改关键字。
第三,照着原文这几个环节挨个过:Understanding system prompts -> Methods of modification -> Method 1: CLAUDE.md files (project-level instructions) -> How CLAUDE.md works with the SDK。像下地先看水路、再试机器、再正式开干,一步一步最稳。
原页关键片段:Method 1: CLAUDE.md files (project-level instructions) 1
真到动手的时候了,下面这条直接敲一遍,看它回什么。
# 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` 原页关键片段:Method 1: CLAUDE.md files (project-level instructions) 2
真到动手的时候了,下面这条直接敲一遍,看它回什么。
import { query } from "@anthropic-ai/claude-agent-sdk";
const messages = [];
for await (const message of query({
prompt: "Add a new React component for user profiles",
options: {
systemPrompt: {
type: "preset",
preset: "claude_code" // Use Claude Code's system prompt
},
settingSources: ["project"] // Loads CLAUDE.md from project
}
})) {
messages.push(message);
}
// Now Claude has access to your project guidelines from CLAUDE.md 原页关键片段:Method 2: Output styles (persistent configurations)
这一段不是只让你理解意思,下面这条命令就是现在要跑的。
import { writeFile, mkdir } from "fs/promises";
import { join } from "path";
import { homedir } from "os";
async function createOutputStyle(name: string, description: string, prompt: string) {
// User-level: ~/.claude/output-styles
// Project-level: .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");
}
// Example: Create a code review specialist
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)`
); 原页关键片段:Method 3: Using systemPrompt with append 1
这一段不是只让你理解意思,下面这条命令就是现在要跑的。
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);
}
} 原页关键片段:Method 3: Using systemPrompt with append 2
这一段不是只让你理解意思,下面这条命令就是现在要跑的。
import { query } from "@anthropic-ai/claude-agent-sdk";
for await (const message of query({
prompt: "Triage the open issues in this repo",
options: {
systemPrompt: {
type: "preset",
preset: "claude_code",
append: "You operate Acme's internal triage workflow. Label issues by component and severity.",
excludeDynamicSections: true
}
}
})) {
// ...
} 原页关键片段:Method 4: Custom system prompts
先别急着往下翻,下面这条命令跑完,心里才有底。
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);
}
} 原页关键片段:Example: Output style with session-specific additions
真到动手的时候了,下面这条直接敲一遍,看它回什么。
import { query } from "@anthropic-ai/claude-agent-sdk";
// Assuming "Code Reviewer" output style is active (via /output-style)
// Add session-specific focus areas
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);
} Documentation Index
这里不是让你背"Documentation Index"这个词,而是让你看它真干活时怎么使。
Understanding system prompts
这一段主要是在把"Understanding system prompts"讲实,不是只摆个标题给你看。
这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。
Methods of modification
看到这里,就把"Methods of modification"当成一件真要上手的活来看。
Method 1: CLAUDE.md files (project-level instructions)
这里不是让你背"Method 1: CLAUDE.md files (project-level instructions)"这个词,而是让你看它真干活时怎么使。
这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。
Method 1: CLAUDE.md files (project-level instructions) 1
真到动手的时候了,下面这条直接敲一遍,看它回什么。
# 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` Method 1: CLAUDE.md files (project-level instructions) 2
真到动手的时候了,下面这条直接敲一遍,看它回什么。
import { query } from "@anthropic-ai/claude-agent-sdk";
const messages = [];
for await (const message of query({
prompt: "Add a new React component for user profiles",
options: {
systemPrompt: {
type: "preset",
preset: "claude_code" // Use Claude Code's system prompt
},
settingSources: ["project"] // Loads CLAUDE.md from project
}
})) {
messages.push(message);
}
// Now Claude has access to your project guidelines from CLAUDE.md Method 2: Output styles (persistent configurations)
看到这里,就把"Method 2: Output styles (persistent configurations)"当成一件真要上手的活来看。
这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。
Method 2: Output styles (persistent configurations)
这一段不是只让你理解意思,下面这条命令就是现在要跑的。
import { writeFile, mkdir } from "fs/promises";
import { join } from "path";
import { homedir } from "os";
async function createOutputStyle(name: string, description: string, prompt: string) {
// User-level: ~/.claude/output-styles
// Project-level: .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");
}
// Example: Create a code review specialist
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)`
); Method 3: Using systemPrompt with append
看到这里,就把"Method 3: Using systemPrompt with append"当成一件真要上手的活来看。
这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。
Method 3: Using systemPrompt with append 1
这一段不是只让你理解意思,下面这条命令就是现在要跑的。
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);
}
} Method 3: Using systemPrompt with append 2
这一段不是只让你理解意思,下面这条命令就是现在要跑的。
import { query } from "@anthropic-ai/claude-agent-sdk";
for await (const message of query({
prompt: "Triage the open issues in this repo",
options: {
systemPrompt: {
type: "preset",
preset: "claude_code",
append: "You operate Acme's internal triage workflow. Label issues by component and severity.",
excludeDynamicSections: true
}
}
})) {
// ...
} Method 4: Custom system prompts
这一段主要是在把"Method 4: Custom system prompts"讲实,不是只摆个标题给你看。
Method 4: Custom system prompts
先别急着往下翻,下面这条命令跑完,心里才有底。
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);
}
} Comparison of all four approaches
看到这里,就把"Comparison of all four approaches"当成一件真要上手的活来看。
Use cases and best practices
这段算经验活,听劝通常能省事。
When to use CLAUDE.md
这里不是催你全用上,而是在说 When to use CLAUDE.md 什么时候用才划算。
这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。
When to use output styles
When to use output styles 到底什么时候值得上,这一段就是在算这笔账。
这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。
When to use systemPrompt with append
When to use systemPrompt with append 到底什么时候值得上,这一段就是在算这笔账。
看这段时要特别盯工具和权限边界,别为了省事一把全开。
When to use custom systemPrompt
When to use custom systemPrompt 到底什么时候值得上,这一段就是在算这笔账。
这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。
Combining approaches
这一段主要是在把"Combining approaches"讲实,不是只摆个标题给你看。
Example: Output style with session-specific additions
这里不是让你背"Example: Output style with session-specific additions"这个词,而是让你看它真干活时怎么使。
Example: Output style with session-specific additions
真到动手的时候了,下面这条直接敲一遍,看它回什么。
import { query } from "@anthropic-ai/claude-agent-sdk";
// Assuming "Code Reviewer" output style is active (via /output-style)
// Add session-specific focus areas
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);
} See also
这一段主要是在把"See also"讲实,不是只摆个标题给你看。
照着做一遍
如果你不想来回翻,就先照这几步顺着做。
每做完一步就看一下结果,再决定要不要继续往下。
第 1 步:Method 1: CLAUDE.md files (project-level instructions) 1
真到动手的时候了,下面这条直接敲一遍,看它回什么。
# 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` 第 2 步:Method 1: CLAUDE.md files (project-level instructions) 2
真到动手的时候了,下面这条直接敲一遍,看它回什么。
import { query } from "@anthropic-ai/claude-agent-sdk";
const messages = [];
for await (const message of query({
prompt: "Add a new React component for user profiles",
options: {
systemPrompt: {
type: "preset",
preset: "claude_code" // Use Claude Code's system prompt
},
settingSources: ["project"] // Loads CLAUDE.md from project
}
})) {
messages.push(message);
}
// Now Claude has access to your project guidelines from CLAUDE.md 第 3 步:Method 2: Output styles (persistent configurations)
这一段不是只让你理解意思,下面这条命令就是现在要跑的。
import { writeFile, mkdir } from "fs/promises";
import { join } from "path";
import { homedir } from "os";
async function createOutputStyle(name: string, description: string, prompt: string) {
// User-level: ~/.claude/output-styles
// Project-level: .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");
}
// Example: Create a code review specialist
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)`
); 第 4 步:Method 3: Using systemPrompt with append 1
这一段不是只让你理解意思,下面这条命令就是现在要跑的。
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);
}
} 一眼看懂这一页
先把这页到底在讲什么看明白,再去碰具体命令和配置,最不容易绕晕。
Modifying system prompts
|
v
这是 Customize behavior 里的一摊要紧活
|
v
先弄懂,再下手 文末提醒
这站会按官方 docs 的导航和内容变化继续重生成,原站加页、删页、改页时,这里会跟着更新。
人话解释会尽量顺着原页往下讲,但命令、参数名、配置名这些硬东西还是保留原样,免得你抄过去跑不起来。