system prompt

System Prompt 是 Claude Code 的“操作系统”,定义了 AI 的身份、能力和行为规范。

设计原则

1. 静态与动态分离

const systemPrompt = [
  // 静态部分 (可缓存)
  IDENTITY,
  CAPABILITIES,
  RULES,
  RESPONSE_STYLE,
  
  // 动态部分 (每次构建)
  buildToolsSection(tools),
  buildSkillsSection(skills),
  buildContextSection(context),
].join('\n\n');

优势:

  • 静态部分可以利用 Anthropic 的 prompt caching

  • 动态部分根据当前状态生成

  • 减少 token 消耗

2. 缓存边界标记

Anthropic API 会缓存边界之前的内容。

Prompt 结构

Identity (身份)

Capabilities (能力)

Rules (规则)

Response Style (响应风格)

定义了 AI 的语气和风格:

  • 知识渊博但不说教

  • 果断、精确、清晰

  • 支持性而非权威性

  • 温暖友好,偶尔开玩笑

  • 简洁直接,避免冗长

动态内容

Tools Section

Skills Section

Context Section

优化策略

1. Prompt Caching

利用 Anthropic 的 prompt caching 功能:

2. 延迟加载

Skills 和 Steering 按需加载:

3. 内容裁剪

根据相关性裁剪 Prompt 内容:

Token 预算管理

预算分配

动态调整

下一步