System prompt mendefinisikan perilaku, kemampuan, dan gaya respons Claude. Claude Agent SDK menyediakan tiga cara untuk menyesuaikan system prompt: menggunakan output styles (konfigurasi persisten berbasis file), menambahkan ke prompt Claude Code, atau menggunakan prompt yang sepenuhnya kustom.
System prompt adalah set instruksi awal yang membentuk bagaimana Claude berperilaku sepanjang percakapan.
Perilaku default: Agent SDK menggunakan system prompt kosong secara default untuk fleksibilitas maksimum. Untuk menggunakan system prompt Claude Code (instruksi tool, panduan kode, dll.), tentukan systemPrompt: { preset: "claude_code" } di TypeScript atau system_prompt="claude_code" di Python.
System prompt Claude Code mencakup:
File CLAUDE.md menyediakan konteks dan instruksi spesifik proyek yang secara otomatis dibaca oleh Agent SDK ketika berjalan di sebuah direktori. Mereka berfungsi sebagai "memori" persisten untuk proyek Anda.
Lokasi dan penemuan:
CLAUDE.md atau .claude/CLAUDE.md di direktori kerja Anda~/.claude/CLAUDE.md untuk instruksi global di semua proyekPENTING: SDK hanya membaca file CLAUDE.md ketika Anda secara eksplisit mengkonfigurasi settingSources (TypeScript) atau setting_sources (Python):
'project' untuk memuat CLAUDE.md tingkat proyek'user' untuk memuat CLAUDE.md tingkat pengguna (~/.claude/CLAUDE.md)Preset system prompt claude_code TIDAK secara otomatis memuat CLAUDE.md - Anda juga harus menentukan setting sources.
Format konten: File CLAUDE.md menggunakan markdown biasa dan dapat berisi:
# 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";
// PENTING: Anda harus menentukan settingSources untuk memuat CLAUDE.md
// Preset claude_code saja TIDAK memuat file 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", // Gunakan system prompt Claude Code
},
settingSources: ["project"], // Diperlukan untuk memuat CLAUDE.md dari proyek
},
})) {
messages.push(message);
}
// Sekarang Claude memiliki akses ke panduan proyek Anda dari CLAUDE.mdTerbaik untuk:
Karakteristik kunci:
settingSourcesOutput styles adalah konfigurasi tersimpan yang memodifikasi system prompt Claude. Mereka disimpan sebagai file markdown dan dapat digunakan kembali di berbagai sesi dan proyek.
import { writeFile, mkdir } from "fs/promises";
import { join } from "path";
import { homedir } from "os";
async function createOutputStyle(
name: string,
description: string,
prompt: string
) {
// Tingkat pengguna: ~/.claude/output-styles
// Tingkat proyek: .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");
}
// Contoh: Buat spesialis code review
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)`
);Setelah dibuat, aktifkan output styles melalui:
/output-style [style-name].claude/settings.local.json/output-style:new [description]Catatan untuk pengguna SDK: Output styles dimuat ketika Anda menyertakan settingSources: ['user'] atau settingSources: ['project'] (TypeScript) / setting_sources=["user"] atau setting_sources=["project"] (Python) dalam opsi Anda.
systemPrompt dengan appendAnda dapat menggunakan preset Claude Code dengan properti append untuk menambahkan instruksi kustom Anda sambil mempertahankan semua fungsionalitas bawaan.
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);
}
}Anda dapat menyediakan string kustom sebagai systemPrompt untuk mengganti default sepenuhnya dengan instruksi Anda sendiri.
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);
}
}| Fitur | CLAUDE.md | Output Styles | systemPrompt dengan append | Custom systemPrompt |
|---|---|---|---|---|
| Persistensi | File per-proyek | Disimpan sebagai file | Hanya sesi | Hanya sesi |
| Reusabilitas | Per-proyek | Lintas proyek | Duplikasi kode | Duplikasi kode |
| Manajemen | Di filesystem | CLI + file | Dalam kode | Dalam kode |
| Tool default | Dipertahankan | Dipertahankan | Dipertahankan | Hilang (kecuali disertakan) |
| Keamanan bawaan | Dipertahankan | Dipertahankan | Dipertahankan | Harus ditambahkan |
| Konteks lingkungan | Otomatis | Otomatis | Otomatis | Harus disediakan |
| Level kustomisasi | Hanya penambahan | Ganti default | Hanya penambahan | Kontrol penuh |
| Kontrol versi | Dengan proyek | Ya | Dengan kode | Dengan kode |
| Cakupan | Spesifik proyek | Pengguna atau proyek | Sesi kode | Sesi kode |
Catatan: "Dengan append" berarti menggunakan systemPrompt: { type: "preset", preset: "claude_code", append: "..." } di TypeScript atau system_prompt={"type": "preset", "preset": "claude_code", "append": "..."} di Python.
Terbaik untuk:
Contoh:
npm run lint:fix sebelum commit"migrations/"Penting: Untuk memuat file CLAUDE.md, Anda harus secara eksplisit mengatur settingSources: ['project'] (TypeScript) atau setting_sources=["project"] (Python). Preset system prompt claude_code TIDAK secara otomatis memuat CLAUDE.md tanpa pengaturan ini.
Terbaik untuk:
Contoh:
systemPrompt dengan appendTerbaik untuk:
systemPromptTerbaik untuk:
Anda dapat menggabungkan metode ini untuk fleksibilitas maksimum:
import { query } from "@anthropic-ai/claude-agent-sdk";
// Dengan asumsi output style "Code Reviewer" aktif (melalui /output-style)
// Tambahkan area fokus spesifik sesi
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);
}