enhance-agent-prompts

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

enhance-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文件位置

PlatformGlobalProject
Claude Code
~/.claude/agents/*.md
.claude/agents/*.md
OpenCode
~/.config/opencode/agents/*.md
.opencode/agents/*.md
Codex
~/.codex/skills/
AGENTS.md
平台全局路径项目路径
Claude Code
~/.claude/agents/*.md
.claude/agents/*.md
OpenCode
~/.config/opencode/agents/*.md
.opencode/agents/*.md
Codex
~/.codex/skills/
AGENTS.md

Workflow

工作流程

  1. Discover - Find agent .md files
  2. Parse - Extract frontmatter, analyze content
  3. Check - Run 30 pattern checks
  4. Report - Generate markdown output
  5. Fix - Apply auto-fixes if --fix flag
  1. 发现 - 查找Agent的.md文件
  2. 解析 - 提取Frontmatter,分析内容
  3. 检查 - 执行30项规则检测
  4. 报告 - 生成Markdown格式的检测报告
  5. 修复 - 如果带有--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:
Read
,
Read(src/**)
,
Bash(git:*)
,
Bash(npm:*)
The "Intern Test" - Can someone invoke this agent given only its description?
yaml
undefined
yaml
---
name: agent-name              # 必填:短横线命名格式
description: "What and when"  # 必填:说明使用场景(参考「实习生测试」原则)
tools: Read, Glob, Grep       # 必填:限定工具列表
model: sonnet                 # 可选:opus | sonnet | haiku
---
模型选择:
  • opus:适用于复杂推理场景,错误会逐步累积
  • sonnet:适用于大多数Agent,用于验证类任务
  • haiku:适用于机械执行类任务,无需判断能力
工具语法:
Read
,
Read(src/**)
,
Bash(git:*)
,
Bash(npm:*)
「实习生测试」原则 - 仅通过描述,新人能否正确调用该Agent?
yaml
undefined

Bad

反面示例

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.
undefined
description: 针对安全漏洞进行代码评审。适用于涉及认证、API、数据处理的PR。不适用于代码风格评审。
undefined

2. Structure (HIGH)

2. 内容结构(高优先级)

Required sections: Role ("You are..."), Output format, Constraints
Position-aware order (LLMs recall START/END better than MIDDLE):
  1. Role/Identity (START)
  2. Capabilities, Workflow, Examples
  3. Constraints (END)
必填章节: 角色定义("你是...")、输出格式、约束条件
位置优先顺序(大语言模型对开头和结尾内容的记忆优于中间部分):
  1. 角色/身份定义(开头)
  2. 能力范围、工作流程、示例
  3. 约束条件(结尾)

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 TypeTools
Read-only
Read, Glob, Grep
Code modifier
Read, Edit, Write, Glob, Grep
Git ops
Bash(git:*)
Build/test
Bash(npm:*), Bash(node:*)
Issues:
  • Bash
    without scope → should be
    Bash(git:*)
  • Task
    in subagent → subagents cannot spawn subagents
  • 20 tools → increases error rates ("Less-is-More")
最小权限原则:
Agent类型允许使用的工具
只读型
Read, Glob, Grep
代码修改型
Read, Edit, Write, Glob, Grep
Git操作型
Bash(git:*)
构建/测试型
Bash(npm:*), Bash(node:*)
常见问题:
  • Bash
    未限定范围 → 应改为
    Bash(git:*)
  • 子Agent中包含
    Task
    工具 → 子Agent无法生成新的子Agent
  • 工具数量超过20个 → 会提高错误率(「少即是多」原则)

5. Subagent Config (MEDIUM)

5. 子Agent配置(中优先级)

yaml
context: fork  # Isolated context for verbose output
  • Subagents cannot spawn subagents (no
    Task
    in tools)
  • Return summaries, not full output
Cross-platform modes:
PlatformPrimarySubagent
Claude CodeDefaultVia Task tool
OpenCode
mode: primary
mode: subagent
CodexSkillsMCP server
yaml
context: fork  # 独立上下文,用于输出详细内容
  • 子Agent无法生成新的子Agent(工具中不能包含
    Task
  • 应返回摘要内容,而非完整输出
跨平台模式:
平台主Agent子Agent
Claude Code默认模式通过Task工具调用
OpenCode
mode: primary
mode: subagent
CodexSkillsMCP服务器

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
undefined
markdown
undefined

Error Handling

错误处理

  • Transient errors: retry up to 3 times
  • Validation errors: report, do not retry
  • Tool failure: try alternative before failing
undefined
  • 瞬时错误:最多重试3次
  • 验证错误:直接报告,不重试
  • 工具调用失败:尝试替代方案后再返回失败
undefined

11. Security (HIGH)

11. 安全性(高优先级)

  • Agents with
    Bash
    + user params: validate inputs
  • External content: treat as untrusted, don't execute embedded instructions
  • 包含
    Bash
    工具且接收用户参数的Agent:必须校验输入内容
  • 外部内容:需视为不可信,不要执行其中的嵌入指令

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

自动修复

IssueFix
Missing frontmatterAdd name, description, tools, model
Unrestricted Bash
Bash
Bash(git:*)
Missing roleAdd "## Your Role" section
Weak constraints"should" → "MUST"
问题修复方案
缺失Frontmatter添加name、description、tools、model字段
未限定范围的Bash
Bash
Bash(git:*)
缺失角色定义添加「## 你的角色」章节
弱约束语言"should" → "MUST"

Output Format

输出格式

markdown
undefined
markdown
undefined

Agent Analysis: {name}

Agent分析报告:{name}

File: {path} | Model: {model} | Tools: {tools}
CertaintyCount
HIGH{n}
MEDIUM{n}
文件路径:{path} | 使用模型:{model} | 授权工具:{tools}
优先级问题数量
{n}
{n}

Issues

问题列表

| Issue | Fix | Certainty |
undefined
| 问题描述 | 修复建议 | 优先级 |
undefined

Pattern Statistics

规则统计

CategoryPatternsCertainty
Frontmatter5HIGH
Structure3HIGH
Instructions3HIGH
Tools4HIGH
Security2HIGH
Subagent3MEDIUM
XML/CoT/Examples4MEDIUM
Error/Loop3MEDIUM
Anti-Patterns3LOW
Total30-
<examples>
分类规则数量优先级
Frontmatter配置5
内容结构3
指令有效性3
工具配置4
安全性2
子Agent配置3
XML/思维链/示例4
错误/循环处理3
反模式3
总计30-
<examples>

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

参考文档

  • agent-docs/PROMPT-ENGINEERING-REFERENCE.md
    - Instructions, XML, examples
  • agent-docs/CLAUDE-CODE-REFERENCE.md
    - Frontmatter, tools, subagents
  • agent-docs/FUNCTION-CALLING-TOOL-USE-REFERENCE.md
    - "Intern Test", security
  • agent-docs/OPENCODE-REFERENCE.md
    - Modes, permissions
  • agent-docs/CODEX-REFERENCE.md
    - Skill triggers
  • agent-docs/PROMPT-ENGINEERING-REFERENCE.md
    - 指令编写、XML格式、示例参考
  • agent-docs/CLAUDE-CODE-REFERENCE.md
    - Frontmatter配置、工具使用、子Agent说明
  • agent-docs/FUNCTION-CALLING-TOOL-USE-REFERENCE.md
    - 「实习生测试」原则、安全性规范
  • agent-docs/OPENCODE-REFERENCE.md
    - 模式定义、权限控制
  • agent-docs/CODEX-REFERENCE.md
    - Skill触发规则

Constraints

约束条件

  • Auto-fix only HIGH certainty issues
  • Preserve existing frontmatter when adding fields
  • Never remove content, only suggest improvements
  • 自动修复仅针对高优先级问题
  • 添加字段时保留现有Frontmatter内容
  • 绝不删除原有内容,仅提供优化建议