システムプロンプトは、Claudeの動作、機能、応答スタイルを定義します。Claude Agent SDKは、システムプロンプトをカスタマイズする3つの方法を提供します:出力スタイル(永続的なファイルベースの設定)の使用、Claude Codeのプロンプトへの追加、または完全なカスタムプロンプトの使用です。
システムプロンプトは、会話全体を通じてClaudeの動作を形作る初期の指示セットです。
デフォルトの動作: Agent SDKはデフォルトで最小限のシステムプロンプトを使用します。必須のツール指示のみが含まれ、Claude Codeのコーディングガイドライン、応答スタイル、プロジェクトコンテキストは省略されます。完全なClaude Codeシステムプロンプトを含めるには、TypeScriptではsystemPrompt: { preset: "claude_code" }、Pythonではsystem_prompt={"type": "preset", "preset": "claude_code"}を指定してください。
Claude Codeのシステムプロンプトには以下が含まれます:
CLAUDE.mdファイルは、Agent SDKがディレクトリで実行される際に自動的に読み込まれるプロジェクト固有のコンテキストと指示を提供します。プロジェクトの永続的な「メモリ」として機能します。
場所と検出:
CLAUDE.mdまたは.claude/CLAUDE.md~/.claude/CLAUDE.md重要: SDKは、settingSources(TypeScript)またはsetting_sources(Python)を明示的に設定した場合にのみCLAUDE.mdファイルを読み込みます:
'project'を含めます~/.claude/CLAUDE.md)を読み込むには'user'を含めますclaude_codeシステムプロンプトプリセットはCLAUDE.mdを自動的に読み込みません - 設定ソースも指定する必要があります。
コンテンツ形式: 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`import { query } from "@anthropic-ai/claude-agent-sdk";
// 重要:CLAUDE.mdを読み込むにはsettingSourcesを指定する必要があります
// 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からプロジェクトガイドラインにアクセスできます最適な用途:
主な特徴:
settingSources経由での設定読み込みが必要出力スタイルは、Claudeのシステムプロンプトを変更する保存された設定です。マークダウンファイルとして保存され、セッションやプロジェクトをまたいで再利用できます。
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)`
);作成後、以下の方法で出力スタイルを有効化します:
/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)を含めた場合に読み込まれます。
systemPromptとappendの使用Claude Codeプリセットにappendプロパティを使用して、すべての組み込み機能を保持しながらカスタム指示を追加できます。
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);
}
}systemPromptにカスタム文字列を提供して、デフォルトを完全に独自の指示に置き換えることができます。
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);
}
}| 機能 | CLAUDE.md | 出力スタイル | appendを使用したsystemPrompt | カスタムsystemPrompt |
|---|---|---|---|---|
| 永続性 | プロジェクトごとのファイル | ファイルとして保存 | セッションのみ | セッションのみ |
| 再利用性 | プロジェクトごと | プロジェクト横断 | コードの重複 | コードの重複 |
| 管理 | ファイルシステム上 | CLI + ファイル | コード内 | コード内 |
| デフォルトツール | 保持 | 保持 | 保持 | 失われる(含めない限り) |
| 組み込みの安全性 | 維持 | 維持 | 維持 | 追加が必要 |
| 環境コンテキスト | 自動 | 自動 | 自動 | 提供が必要 |
| カスタマイズレベル | 追加のみ | デフォルトを置換 | 追加のみ | 完全な制御 |
| バージョン管理 | プロジェクトと共に | はい | コードと共に | コードと共に |
| スコープ | プロジェクト固有 | ユーザーまたはプロジェクト | コードセッション | コードセッション |
注意: 「appendを使用」とは、TypeScriptではsystemPrompt: { type: "preset", preset: "claude_code", append: "..." }、Pythonではsystem_prompt={"type": "preset", "preset": "claude_code", "append": "..."}を使用することを意味します。
最適な用途:
例:
npm run lint:fixを実行すること」migrations/ディレクトリにあります」重要: CLAUDE.mdファイルを読み込むには、settingSources: ['project'](TypeScript)またはsetting_sources=["project"](Python)を明示的に設定する必要があります。claude_codeシステムプロンプトプリセットは、この設定なしではCLAUDE.mdを自動的に読み込みません。
最適な用途:
例:
systemPromptとappendを使用するタイミング最適な用途:
systemPromptを使用するタイミング最適な用途:
最大限の柔軟性を得るために、これらの方法を組み合わせることができます:
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);
}Was this page helpful?