enhance-agent-prompts
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseenhance-agent-prompts
优化Agent提示词
Analyze agent prompt files for prompt engineering best practices.
基于提示词工程最佳实践分析Agent提示词文件。
Parse Arguments
解析参数
javascript
const args = '$ARGUMENTS'.split(' ').filter(Boolean);
const targetPath = args.find(a => !a.startsWith('--')) || '.';
const fix = args.includes('--fix');
const verbose = args.includes('--verbose');javascript
const args = '$ARGUMENTS'.split(' ').filter(Boolean);
const targetPath = args.find(a => !a.startsWith('--')) || '.';
const fix = args.includes('--fix');
const verbose = args.includes('--verbose');Agent File Locations
Agent文件位置
| Platform | Global | Project |
|---|---|---|
| Claude Code | | |
| OpenCode | | |
| Codex | | |
| 平台 | 全局路径 | 项目路径 |
|---|---|---|
| Claude Code | | |
| OpenCode | | |
| Codex | | |
Workflow
工作流程
- Discover - Find agent .md files
- Parse - Extract frontmatter, analyze content
- Check - Run 30 pattern checks
- Report - Generate markdown output
- Fix - Apply auto-fixes if --fix flag
- 发现 - 查找Agent的.md文件
- 解析 - 提取Frontmatter,分析内容
- 检查 - 执行30项规则检测
- 报告 - 生成Markdown格式的检测报告
- 修复 - 如果带有--fix参数则应用自动修复
Detection Patterns
检测规则
1. Frontmatter (HIGH)
1. Frontmatter配置(高优先级)
yaml
---
name: agent-name # Required: kebab-case
description: "What and when" # Required: WHEN to use (see "Intern Test")
tools: Read, Glob, Grep # Required: restricted list
model: sonnet # Optional: opus | sonnet | haiku
---Model Selection:
- opus: Complex reasoning, errors compound
- sonnet: Most agents, validation
- haiku: Mechanical execution, no judgment
Tool Syntax: , , ,
ReadRead(src/**)Bash(git:*)Bash(npm:*)The "Intern Test" - Can someone invoke this agent given only its description?
yaml
undefinedyaml
---
name: agent-name # 必填:短横线命名格式
description: "What and when" # 必填:说明使用场景(参考「实习生测试」原则)
tools: Read, Glob, Grep # 必填:限定工具列表
model: sonnet # 可选:opus | sonnet | haiku
---模型选择:
- opus:适用于复杂推理场景,错误会逐步累积
- sonnet:适用于大多数Agent,用于验证类任务
- haiku:适用于机械执行类任务,无需判断能力
工具语法: , , ,
ReadRead(src/**)Bash(git:*)Bash(npm:*)「实习生测试」原则 - 仅通过描述,新人能否正确调用该Agent?
yaml
undefinedBad
反面示例
description: Reviews code
description: 代码评审
Good - triggers, capabilities, exclusions
正面示例 - 包含触发条件、能力范围、排除场景
description: Reviews code for security vulnerabilities. Use for PRs touching auth, API, data handling. Not for style reviews.
undefineddescription: 针对安全漏洞进行代码评审。适用于涉及认证、API、数据处理的PR。不适用于代码风格评审。
undefined2. Structure (HIGH)
2. 内容结构(高优先级)
Required sections: Role ("You are..."), Output format, Constraints
Position-aware order (LLMs recall START/END better than MIDDLE):
- Role/Identity (START)
- Capabilities, Workflow, Examples
- Constraints (END)
必填章节: 角色定义("你是...")、输出格式、约束条件
位置优先顺序(大语言模型对开头和结尾内容的记忆优于中间部分):
- 角色/身份定义(开头)
- 能力范围、工作流程、示例
- 约束条件(结尾)
3. Instruction Effectiveness (HIGH)
3. 指令有效性(高优先级)
Positive over negative:
- Bad: "Don't assume file paths exist"
- Good: "Verify file paths using Glob before reading"
Strong constraint language:
- Bad: "should", "try to", "consider"
- Good: "MUST", "ALWAYS", "NEVER"
Include WHY for important rules - motivation improves compliance.
多用肯定句,少用否定句:
- 反面示例:"不要假设文件路径存在"
- 正面示例:"读取前请使用Glob验证文件路径"
使用强约束语言:
- 反面示例:"should"、"try to"、"consider"
- 正面示例:"MUST"、"ALWAYS"、"NEVER"
重要规则需说明原因 - 明确动机可提高合规性
4. Tool Configuration (HIGH)
4. 工具配置(高优先级)
Principle of Least Privilege:
| Agent Type | Tools |
|---|---|
| Read-only | |
| Code modifier | |
| Git ops | |
| Build/test | |
Issues:
- without scope → should be
BashBash(git:*) - in subagent → subagents cannot spawn subagents
Task -
20 tools → increases error rates ("Less-is-More")
最小权限原则:
| Agent类型 | 允许使用的工具 |
|---|---|
| 只读型 | |
| 代码修改型 | |
| Git操作型 | |
| 构建/测试型 | |
常见问题:
- 未限定范围 → 应改为
BashBash(git:*) - 子Agent中包含工具 → 子Agent无法生成新的子Agent
Task - 工具数量超过20个 → 会提高错误率(「少即是多」原则)
5. Subagent Config (MEDIUM)
5. 子Agent配置(中优先级)
yaml
context: fork # Isolated context for verbose output- Subagents cannot spawn subagents (no in tools)
Task - Return summaries, not full output
Cross-platform modes:
| Platform | Primary | Subagent |
|---|---|---|
| Claude Code | Default | Via Task tool |
| OpenCode | | |
| Codex | Skills | MCP server |
yaml
context: fork # 独立上下文,用于输出详细内容- 子Agent无法生成新的子Agent(工具中不能包含)
Task - 应返回摘要内容,而非完整输出
跨平台模式:
| 平台 | 主Agent | 子Agent |
|---|---|---|
| Claude Code | 默认模式 | 通过Task工具调用 |
| OpenCode | | |
| Codex | Skills | MCP服务器 |
6. XML Structure (MEDIUM)
6. XML结构(中优先级)
Use XML tags when 5+ sections, mixed lists/code, or multiple phases:
xml
<role>You are...</role>
<workflow>1. Read 2. Analyze 3. Report</workflow>
<constraints>- Only analyze, never modify</constraints>当内容包含5个以上章节、混合列表与代码、或多阶段任务时,建议使用XML标签:
xml
<role>You are...</role>
<workflow>1. Read 2. Analyze 3. Report</workflow>
<constraints>- Only analyze, never modify</constraints>7. Chain-of-Thought (MEDIUM)
7. 思维链(Chain-of-Thought,中优先级)
Unnecessary: Simple tasks (<500 words), single-step, mechanical
Missing: Complex analysis (>1000 words), multi-step reasoning, "analyze/evaluate/assess"
无需使用: 简单任务(少于500词)、单步骤任务、机械执行类任务
必须使用: 复杂分析(超过1000词)、多步骤推理、包含「分析/评估/评估」类需求的任务
8. Examples (MEDIUM)
8. 示例(中优先级)
Optimal: 2-5 examples. <2 insufficient, >5 token bloat.
最优数量:2-5个示例。少于2个参考性不足,多于5个会导致令牌冗余。
9. Loop Termination (MEDIUM)
9. 循环终止(中优先级)
For iterating agents: max iterations, completion criteria, escape conditions.
对于循环执行的Agent:需定义最大迭代次数、完成标准、退出条件
10. Error Handling (MEDIUM)
10. 错误处理(中优先级)
markdown
undefinedmarkdown
undefinedError Handling
错误处理
- Transient errors: retry up to 3 times
- Validation errors: report, do not retry
- Tool failure: try alternative before failing
undefined- 瞬时错误:最多重试3次
- 验证错误:直接报告,不重试
- 工具调用失败:尝试替代方案后再返回失败
undefined11. Security (HIGH)
11. 安全性(高优先级)
- Agents with + user params: validate inputs
Bash - External content: treat as untrusted, don't execute embedded instructions
- 包含工具且接收用户参数的Agent:必须校验输入内容
Bash - 外部内容:需视为不可信,不要执行其中的嵌入指令
12. Anti-Patterns (LOW)
12. 反模式(低优先级)
- Vague: "usually", "sometimes" → use "always", "never"
- Bloat: >2000 tokens → split into agent + skill
- Non-idempotent: side effects on retry → design idempotent or mark "do not retry"
- 模糊表述: "usually"、"sometimes" → 应使用"always"、"never"
- 内容冗余: 超过2000令牌 → 拆分为主Agent+Skill
- 非幂等性: 重试会产生副作用 → 设计为幂等或标记「禁止重试」
Auto-Fixes
自动修复
| Issue | Fix |
|---|---|
| Missing frontmatter | Add name, description, tools, model |
| Unrestricted Bash | |
| Missing role | Add "## Your Role" section |
| Weak constraints | "should" → "MUST" |
| 问题 | 修复方案 |
|---|---|
| 缺失Frontmatter | 添加name、description、tools、model字段 |
| 未限定范围的Bash | |
| 缺失角色定义 | 添加「## 你的角色」章节 |
| 弱约束语言 | "should" → "MUST" |
Output Format
输出格式
markdown
undefinedmarkdown
undefinedAgent Analysis: {name}
Agent分析报告:{name}
File: {path} | Model: {model} | Tools: {tools}
| Certainty | Count |
|---|---|
| HIGH | {n} |
| MEDIUM | {n} |
文件路径:{path} | 使用模型:{model} | 授权工具:{tools}
| 优先级 | 问题数量 |
|---|---|
| 高 | {n} |
| 中 | {n} |
Issues
问题列表
| Issue | Fix | Certainty |
undefined| 问题描述 | 修复建议 | 优先级 |
undefinedPattern Statistics
规则统计
| Category | Patterns | Certainty |
|---|---|---|
| Frontmatter | 5 | HIGH |
| Structure | 3 | HIGH |
| Instructions | 3 | HIGH |
| Tools | 4 | HIGH |
| Security | 2 | HIGH |
| Subagent | 3 | MEDIUM |
| XML/CoT/Examples | 4 | MEDIUM |
| Error/Loop | 3 | MEDIUM |
| Anti-Patterns | 3 | LOW |
| Total | 30 | - |
| 分类 | 规则数量 | 优先级 |
|---|---|---|
| Frontmatter配置 | 5 | 高 |
| 内容结构 | 3 | 高 |
| 指令有效性 | 3 | 高 |
| 工具配置 | 4 | 高 |
| 安全性 | 2 | 高 |
| 子Agent配置 | 3 | 中 |
| XML/思维链/示例 | 4 | 中 |
| 错误/循环处理 | 3 | 中 |
| 反模式 | 3 | 低 |
| 总计 | 30 | - |
Unrestricted Bash
未限定范围的Bash
<bad_example>
yaml
tools: Read, Bash</bad_example>
<good_example>
yaml
tools: Read, Bash(git:*), Bash(npm:test)</good_example>
<bad_example>
yaml
tools: Read, Bash</bad_example>
<good_example>
yaml
tools: Read, Bash(git:*), Bash(npm:test)</good_example>
Description Trigger
描述场景明确性
<bad_example>
yaml
description: Reviews code</bad_example>
<good_example>
yaml
description: Reviews code for security. Use for PRs touching auth, API, data. Not for style.</good_example>
<bad_example>
yaml
description: 代码评审</bad_example>
<good_example>
yaml
description: 针对安全漏洞进行代码评审。适用于涉及认证、API、数据处理的PR。不适用于代码风格评审。</good_example>
Model Selection
模型选择合理性
<bad_example>
yaml
name: json-formatter
model: opus # Overkill for mechanical task</bad_example>
<good_example>
yaml
name: json-formatter
model: haiku # Simple, mechanical</good_example>
<bad_example>
yaml
name: json-formatter
model: opus # 机械任务使用大模型属于过度设计</bad_example>
<good_example>
yaml
name: json-formatter
model: haiku # 简单机械任务适用轻量模型</good_example>
Constraint Language
约束语言强度
<bad_example>
markdown
- Try to validate inputs when possible</bad_example>
<good_example>
markdown
- MUST validate all inputs before processing</good_example>
<bad_example>
markdown
- 尽可能尝试校验输入</bad_example>
<good_example>
markdown
- 处理前MUST校验所有输入</good_example>
Subagent Tools
子Agent工具权限
<bad_example>
yaml
context: fork
tools: Read, Glob, Task # Task not allowed</bad_example>
<good_example>
yaml
context: fork
tools: Read, Glob, Grep</good_example>
</examples>
<bad_example>
yaml
context: fork
tools: Read, Glob, Task # Task工具不允许在子Agent中使用</bad_example>
<good_example>
yaml
context: fork
tools: Read, Glob, Grep</good_example>
</examples>
References
参考文档
- - Instructions, XML, examples
agent-docs/PROMPT-ENGINEERING-REFERENCE.md - - Frontmatter, tools, subagents
agent-docs/CLAUDE-CODE-REFERENCE.md - - "Intern Test", security
agent-docs/FUNCTION-CALLING-TOOL-USE-REFERENCE.md - - Modes, permissions
agent-docs/OPENCODE-REFERENCE.md - - Skill triggers
agent-docs/CODEX-REFERENCE.md
- - 指令编写、XML格式、示例参考
agent-docs/PROMPT-ENGINEERING-REFERENCE.md - - Frontmatter配置、工具使用、子Agent说明
agent-docs/CLAUDE-CODE-REFERENCE.md - - 「实习生测试」原则、安全性规范
agent-docs/FUNCTION-CALLING-TOOL-USE-REFERENCE.md - - 模式定义、权限控制
agent-docs/OPENCODE-REFERENCE.md - - Skill触发规则
agent-docs/CODEX-REFERENCE.md
Constraints
约束条件
- Auto-fix only HIGH certainty issues
- Preserve existing frontmatter when adding fields
- Never remove content, only suggest improvements
- 自动修复仅针对高优先级问题
- 添加字段时保留现有Frontmatter内容
- 绝不删除原有内容,仅提供优化建议