prompt-refiner-claude

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Claude Prompt Refiner

Claude提示词优化指南

Refine prompts to get better results from Claude models by applying Anthropic's recommended patterns.
通过应用Anthropic推荐的模式优化提示词,从而从Claude模型中获得更优结果。

When to Use

适用场景

Invoke this skill when you have a task for Claude that:
  • Involves multiple steps or files
  • Requires specific output formatting
  • Needs careful reasoning or analysis
  • Would benefit from structured context
当你有以下类型的Claude任务时,可使用此方法:
  • 涉及多个步骤或文件
  • 要求特定的输出格式
  • 需要严谨的推理或分析
  • 能从结构化语境中获益

Refinement Process

优化流程

Step 1: Analyze the Draft Prompt

步骤1:分析草稿提示词

Review the user's prompt for:
  • Clear outcome definition
  • Sufficient context
  • Explicit constraints
  • Success criteria
Ask clarifying questions if any of these are missing.
检查用户的提示词是否包含以下要素:
  • 清晰的结果定义
  • 足够的语境信息
  • 明确的约束条件
  • 成功标准
如有缺失,提出澄清问题。

Step 2: Apply Claude-Specific Patterns

步骤2:应用Claude专属模式

Structure with XML tags: Claude responds exceptionally well to XML-style tags for organizing complex prompts:
  • <context>
    - Background information, codebase state, environment
  • <task>
    - The specific action to take
  • <requirements>
    - Must-have criteria
  • <constraints>
    - Limitations and boundaries
  • <examples>
    - Sample inputs/outputs if helpful
  • <output_format>
    - How to structure the response
Ordering matters:
  1. Context first (what exists)
  2. Task second (what to do)
  3. Requirements third (how to do it)
  4. Examples last (clarifying edge cases)
使用XML标签结构化: Claude对用于组织复杂提示词的XML风格标签响应极佳:
  • <context>
    - 背景信息、代码库状态、环境
  • <task>
    - 具体要执行的操作
  • <requirements>
    - 必须满足的标准
  • <constraints>
    - 限制条件与边界
  • <examples>
    - 如有帮助可提供输入/输出示例
  • <output_format>
    - 响应的结构要求
顺序至关重要:
  1. 先放语境(现有信息)
  2. 再放任务(要做什么)
  3. 接着是要求(怎么做)
  4. 最后是示例(澄清边缘情况)

Step 3: Enhance for Reasoning

步骤3:增强推理能力

For complex tasks, add thinking prompts:
  • "Think through the approach before implementing"
  • "Consider these edge cases: ..."
  • "Explain your reasoning for key decisions"
对于复杂任务,添加引导推理的提示:
  • "先思考实现方法再执行"
  • "考虑以下边缘情况:..."
  • "解释关键决策的推理过程"

Step 4: Output the Refined Prompt

步骤4:输出优化后的提示词

Present the improved prompt with:
  • Clear section headers
  • XML tags where beneficial
  • Specific, measurable criteria
  • An explanation of what changed and why
优化后的提示词需包含以下元素:
  • 清晰的章节标题
  • 必要时使用XML标签
  • 具体、可衡量的标准
  • 说明修改内容及原因

Example Transformations

示例转换

Example 1: Vague Task

示例1:模糊任务

Before:
Add caching to the API
After:
<context>
The /api/products endpoint currently queries the database on every request.
Average response time is 200ms. We use Redis for other caching in the app.
</context>

<task>
Add Redis caching to the /api/products endpoint to reduce database load.
</task>

<requirements>
- Cache TTL of 5 minutes
- Cache invalidation when products are updated
- Graceful fallback to database if Redis is unavailable
- Add cache hit/miss metrics logging
</requirements>

<constraints>
- Don't change the response format
- Must pass existing integration tests
- Use our existing Redis connection from src/lib/redis.ts
</constraints>
优化前:
Add caching to the API
优化后:
<context>
/api/products端点目前每次请求都会查询数据库。平均响应时间为200ms。我们在应用中使用Redis处理其他缓存需求。
</context>

<task>
为/api/products端点添加Redis缓存以降低数据库负载。
</task>

<requirements>
- 缓存TTL为5分钟
- 产品更新时自动失效缓存
- Redis不可用时优雅降级到数据库查询
- 添加缓存命中/未命中指标日志
</requirements>

<constraints>
- 不得修改响应格式
- 必须通过现有集成测试
- 使用src/lib/redis.ts中已有的Redis连接
</constraints>

Example 2: Code Review Request

示例2:代码审查请求

Before:
Review this PR
After:
<context>
This PR adds user authentication to our Next.js application.
Our stack: Next.js 14, TypeScript, Prisma, PostgreSQL.
Security is critical - this handles user sessions and passwords.
</context>

<task>
Review the changes in this PR for security issues, code quality, and adherence to our patterns.
</task>

<requirements>
Focus on:
1. Security vulnerabilities (auth bypass, injection, etc.)
2. Error handling and edge cases
3. TypeScript type safety
4. Test coverage for critical paths
</requirements>

<output_format>
Organize your review as:
优化前:
Review this PR
优化后:
<context>
此PR为我们的Next.js应用添加了用户认证功能。
技术栈:Next.js 14、TypeScript、Prisma、PostgreSQL。
安全性至关重要 - 此模块处理用户会话和密码。
</context>

<task>
审查此PR中的变更,检查是否存在安全问题、代码质量是否达标以及是否符合我们的开发规范。
</task>

<requirements>
重点关注:
1. 安全漏洞(认证绕过、注入攻击等)
2. 错误处理与边缘情况
3. TypeScript类型安全性
4. 关键路径的测试覆盖率
</requirements>

<output_format>
按以下结构组织审查结果:

Critical Issues (must fix before merge)

严重问题(合并前必须修复)

Recommendations (should consider)

建议项(应考虑修复)

Minor Suggestions (nice to have)

小建议(可选优化)

What Looks Good (positive feedback)

优秀实践(正面反馈)

</output_format>
undefined
</output_format>
undefined

Example 3: Feature Implementation

示例3:功能实现请求

Before:
Add dark mode
After:
<context>
React application using Tailwind CSS for styling.
Currently only has light mode. Design tokens are in tailwind.config.js.
User preference should persist across sessions.
</context>

<task>
Implement dark mode toggle with system preference detection and persistence.
</task>

<requirements>
- Toggle component in the header
- Detect system preference on first visit
- Persist user choice in localStorage
- Smooth transition between modes
- Update all existing components to support both modes
</requirements>

<constraints>
- Use Tailwind's built-in dark mode support
- Don't add new dependencies
- Ensure WCAG AA contrast ratios in both modes
</constraints>

<examples>
Current light mode colors:
- Background: bg-white
- Text: text-gray-900
- Primary: text-blue-600

Expected dark mode equivalents:
- Background: dark:bg-gray-900
- Text: dark:text-gray-100
- Primary: dark:text-blue-400
</examples>
优化前:
Add dark mode
优化后:
<context>
使用Tailwind CSS进行样式开发的React应用。
目前仅支持浅色模式。设计令牌存于tailwind.config.js中。
用户偏好应在会话间持久化。
</context>

<task>
实现深色模式切换功能,支持系统偏好检测与持久化存储。
</task>

<requirements>
- 在头部添加切换组件
- 首次访问时检测系统偏好
- 将用户选择持久化到localStorage
- 模式切换时使用平滑过渡
- 更新所有现有组件以支持两种模式
</requirements>

<constraints>
- 使用Tailwind内置的深色模式支持
- 不得添加新依赖
- 确保两种模式均符合WCAG AA对比度标准
</constraints>

<examples>
当前浅色模式颜色:
- 背景:bg-white
- 文本:text-gray-900
- 主色:text-blue-600

预期深色模式对应颜色:
- 背景:dark:bg-gray-900
- 文本:dark:text-gray-100
- 主色:dark:text-blue-400
</examples>

Tips for Best Results

最佳实践小贴士

  1. Be specific about scope - "the auth module" → "src/auth/session.ts"
  2. Include file paths when relevant
  3. Reference existing patterns - "follow the pattern in UserService.ts"
  4. State what NOT to do - constraints prevent unwanted changes
  5. Define done - what does success look like?
  1. 明确范围 - "认证模块" → "src/auth/session.ts"
  2. 相关时包含文件路径
  3. 参考现有模式 - "遵循UserService.ts中的模式"
  4. 说明禁止操作 - 约束条件可避免不必要的修改
  5. 定义完成标准 - 成功的标准是什么?