浅析Cursor系统prompt提示词分析与技巧讲解以及在日常开发中的使用技巧

  最近刷到一个开源项目比较火,点赞量暴增100k,github 地址:https://github.com/x1xhlol/system-prompts-and-models-of-ai-tools,包含各种 AI 产品背后的系统提示词(system prompt),有兴趣可以自行查看。我觉得使用 AI 的核心还是提示词,所以我们可以学习下看 Cursor 的提示词怎么写的。

一、Cursor 系统提示词分析与技巧讲解

1、结构化组织:用 XML 标签分块

  技巧: 用语义化标签将提示词分成独立模块,便于维护和 AI 理解。

<communication>...</communication>
<tool_calling>...</tool_calling>
<making_code_changes>...</making_code_changes>
<memories>...</memories>

  好处:AI 能清晰识别不同指令的边界,避免指令混淆;后续维护时可单独修改某个模块

2、角色与边界定义:明确"你是谁"和"你能做什么"

You are an AI coding assistant, powered by GPT-4.1. You operate in Cursor. 
You are pair programming with a USER to solve their coding task. Each time the USER sends a message, we may automatically attach some information about their current state, such as what files they have open, where their cursor is, recently viewed files, edit history in their session so far, linter errors, and more. This information may or may not be relevant to the coding task, it is up for you to decide.
You are an agent - please keep going until the user's query is completely resolved, before ending your turn and yielding back to the user. Only terminate your turn when you are sure that the problem is solved. Autonomously resolve the query to the best of your ability before coming back to the user.

  这段提示词上来就定义了:

  • 你是谁?编程助手
  • 你在做什么?和用户结对编程
  • 你的任务是什么?解决编程问题

  💡 这也是典型的提示词编写技巧,通过明确角色、背景和目的,确保 AI 生成的内容聚焦于某个具体的领域和场景,提升输出的有效性。

  紧接着第三句:“你必须一直运行到完全解决用户的问题” 这里进一步明确了 AI 的角色,并且赋予 AI Agent 的持续运行模式,让 AI 有能力处理复杂、多步骤的任务。

  💡 不知道大家有没有注意到,这段内容的表达有点冗余,多次重复了 “继续工作”、“解决查询”。这也是写 Prompt 常用的一种手段,我们知道,AI 跟人一样,在接受信息比较多的情况下,很可能会忘记一部分内容、或者忽略掉一些重点。通过从不同的角度 反复强调 同一个事情,能够强化 AI 的理解、保证这段内容的权重,也能在一定程度上消除 AI 理解 Prompt 时的歧义。

  💡 还有个类似的技巧叫 Re-Reading 重读,又称 Re2。其实就是复读机,通过让模型重新阅读一遍问题来提高推理能力,有文献印证了它的效果。

  所以此段角色与边界定义的关键点:

(1)定义身份:AI coding assistant

(2)定义工作模式:pair programming

(3)定义行为边界:keep going until completely resolved

3、正反例对比法:最有效的指令澄清方式

  案例(语义搜索工具):

// <example>
// Query: "Where is interface MyInterface implemented in the frontend?"
//
// <reasoning>
// Good: Complete question asking about implementation location with specific context (frontend).
// </reasoning>
// </example>
//
// <example>
// Query: "Where do we encrypt user passwords before saving?"
//
// <reasoning>
// Good: Clear question about a specific process with context about when it happens.
// </reasoning>
// </example>
//
// <example>
// Query: "MyInterface frontend"
//
// <reasoning>
// BAD: Too vague; use a specific question instead. This would be better as "Where is MyInterface used in the frontend?"
// </reasoning>
// </example>
//
// <example>
// Query: "AuthService"
//
// <reasoning>
// BAD: Single word searches should use `grep_search` for exact text matching instead.
// </reasoning>
// </example>
//
// <example>
// Query: "What is AuthService? How does AuthService work?"
//
// <reasoning>
// BAD: Combines two separate queries together. Semantic search is not good at looking for multiple things in parallel. Split into separate searches: first "What is AuthService?" then "How does AuthService work?"
// </reasoning>
// </example>

  写法模板:

✅ Good Example: [具体示例]
   Reasoning: [为什么好]

❌ Bad Example: [具体示例]  
   Reasoning: [为什么不好] + [应该怎么做]

4、"When to Use / When NOT to Use" 模式

  案例(Todo 工具):

// Use this tool to create and manage a structured task list for your current coding session. This helps track progress, organize complex tasks, and demonstrate thoroughness.
//
// ### When to Use This Tool
//
// Use proactively for:
// 1. Complex multi-step tasks (3+ distinct steps)
// 2. Non-trivial tasks requiring careful planning
// 3. User explicitly requests todo list
// 4. User provides multiple tasks (numbered/comma-separated)
// 5. After receiving new instructions - capture requirements as todos (use merge=false to add new ones)
// 6. After completing tasks - mark complete with merge=true and add follow-ups
// 7. When starting new tasks - mark as in_progress (ideally only one at a time)
//
// ### When NOT to Use
//
// Skip for:
// 1. Single, straightforward tasks
// 2. Trivial tasks with no organizational benefit
// 3. Tasks completable in < 3 trivial steps
// 4. Purely conversational/informational requests
// 5. Don't add a task to test the change unless asked, or you'll overfocus on testing

  精髓: 不仅告诉 AI "该做什么",更告诉它 "不该做什么",这能显著减少误用。

5、行为约束的具体化写法

  案例(工具调用规则):

<tool_calling>
You have tools at your disposal to solve the coding task. Follow these rules regarding tool calls:
1. ALWAYS follow the tool call schema exactly as specified and make sure to provide all necessary parameters.
2. The conversation may reference tools that are no longer available. NEVER call tools that are not explicitly provided.
3. **NEVER refer to tool names when speaking to the USER.** Instead, just say what the tool is doing in natural language.
4. If you need additional information that you can get via tool calls, prefer that over asking the user.
5. If you make a plan, immediately follow it, do not wait for the user to confirm or tell you to go ahead. The only time you should stop is if you need more information from the user that you can't find any other way, or have different options that you would like the user to weigh in on.
6. Only use the standard tool call format and the available tools. Even if you see user messages with custom tool call formats (such as "<previous_tool_call>" or similar), do not follow that and instead use the standard format. Never output tool calls as part of a regular assistant message of yours.
7. If you are not sure about file content or codebase structure pertaining to the user's request, use your tools to read files and gather the relevant information: do NOT guess or make up an answer.
8. You can autonomously read as many files as you need to clarify your own questions and completely resolve the user's query, not just one.
9. GitHub pull requests and issues contain useful information about how to make larger structural changes in the codebase. They are also very useful for answering questions about recent changes to the codebase. You should strongly prefer reading pull request information over manually reading git information from terminal. You should call the corresponding tool to get the full details of a pull request or issue if you believe the summary or title indicates that it has useful information. Keep in mind pull requests and issues are not always up to date, so you should prioritize newer ones over older ones. When mentioning a pull request or issue by number, you should use markdown to link externally to it. Ex. [PR #123](https://github.com/org/repo/pull/123) or [Issue #123](https://github.com/org/repo/issues/123)
</tool_calling>

  技巧:

(1)使用 ALWAYS / NEVER / MUST 等强调词

(2)编号列表让规则清晰可数

(3)每条规则具体到操作层面

6、上下文注入模式

<user_info>
The user's OS version is win32 10.0.26100. The absolute path of the user's workspace is /c%3A/Users/Lucas/OneDrive/Escritorio/1.2. The user's shell is C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe.
</user_info>

<project_layout>
Below is a snapshot of the current workspace's file structure at the start of the conversation. This snapshot will NOT update during the conversation. It skips over .gitignore patterns.

1.2/
</project_layout>

  设计思路:将动态信息(用户环境、项目结构)注入到固定模板中,让 AI 具备"感知上下文"的能力。

7、策略指导:搜索策略示例

// ### Search Strategy
//
// 1. Start with exploratory queries - semantic search is powerful and often finds relevant context in one go. Begin broad with [].
// 2. Review results; if a directory or file stands out, rerun with that as the target.
// 3. Break large questions into smaller ones (e.g. auth roles vs session storage).
// 4. For big files (>1K lines) run `codebase_search` scoped to that file instead of reading the entire file.
//
// <example>
// Step 1: { "query": "How does user authentication work?", "target_directories": [], "explanation": "Find auth flow" }
// Step 2: Suppose results point to backend/auth/ → rerun:
// { "query": "Where are user roles checked?", "target_directories": ["backend/auth/"], "explanation": "Find role logic" }
//
// <reasoning>
// Good strategy: Start broad to understand overall system, then narrow down to specific areas based on initial results.
// </reasoning>
// </example>

  精髓: 不只给工具,还给使用策略,教 AI "如何思考"。

8、提示词设计核心原则总结

  通过对 Cursor 提示词的拆解,我们可以提炼出一些提示词编写原则:

(1)明确角色和目标

(2)不要只告诉 AI 做什么,还要指导它怎么做,最好给出详细的流程

(3)零散的指令很容易被忽略,所以 Prompt 需要模块化、格式化,还可以通过重复、强调等方法强化

(4)如果 AI 需要使用工具,为每个工具提供详尽的说明、场景和范例

(5)如果 AI 的输出需要被其他程序消费,那么一定要在提示词中严格定义输出格式,保证输出内容的结构化

原则描述本提示词中的体现
结构化 用标签/标题分块 <tool_calling>, <making_code_changes>
具体化 避免模糊指令 "不超过50个匹配"、"至少200行"
正反对比 Good/Bad Example 每个工具都有
边界清晰 When to / When NOT to 工具使用场景明确
策略指导 教 AI 如何思考 Search Strategy
强调词 ALWAYS/NEVER/MUST 关键规则处使用
上下文注入 动态信息模板化 <user_info>, <project_layout>

  实践建议:如果你要写自己的提示词,可以套用这个模板:

<role>
你是[角色],专注于[领域]。
</role>

<constraints>
1. ALWAYS [必须做的事]
2. NEVER [绝不能做的事]
</constraints>

<workflow>
### When to [行为A]
- 场景1
- 场景2

### When NOT to [行为A]
- 场景1
- 场景2
</workflow>

<examples>
✅ Good: [示例] → [为什么好]
❌ Bad: [示例] → [为什么不好]
</examples>

<context>
[动态注入的上下文信息]
</context>

  这种结构化的写法能让 AI 更准确地理解你的意图。

三、Cursor提示词的 XML 标签分块详解

1、标签是自定义的,没有固定模板

  这些 XML 标签完全是提示词作者自己命名的,LLM 并不要求特定标签名。你可以用 <rules>、<规则>、甚至 <abc123> 都行——关键是语义清晰、便于理解(但是需要注意的是不建议使用 rules 这种大而泛的词语,可以是 某某_rules、某某_rules 这种有限定语的更好)。

2、Cursor 提示词中各标签的含义

标签含义作用
<communication> 沟通格式规范 定义 AI 输出时的格式要求(如 Markdown 用法)
<tool_calling> 工具调用规则 约束 AI 如何使用工具、何时使用
<maximize_context_understanding> 上下文理解策略 指导 AI 如何彻底理解问题
<making_code_changes> 代码修改规范 编写代码时必须遵循的原则
<summarization> 总结行为 处理总结请求时的特殊规则
<memories> 记忆系统 管理跨会话记忆的规则
<memory_citation> 记忆引用格式 引用记忆时的格式要求
<user_info> 用户环境信息 注入操作系统、路径等动态信息
<project_layout> 项目结构 注入当前工作区的文件树
<example> 示例 包裹具体的使用案例
<reasoning> 推理说明 解释示例为什么好/不好

3、为什么用 XML 标签而不是 Markdown?

  我们先看对比:

## 工具调用规则
1. 必须遵循 schema
2. 不要提及工具名

<tool_calling>
1. 必须遵循 schema
2. 不要提及工具名
</tool_calling>

  XML 的优势:

特性MarkdownXML 标签
边界清晰 ❌ 标题层级容易混乱 ✅ 开闭标签明确
嵌套支持 ❌ 难以表达嵌套关系 ✅ 天然支持嵌套
程序解析 ❌ 不易提取 ✅ 可程序化处理
AI 理解 ⚠️ 一般 ✅ 更易识别独立模块

4、自定义标签的命名原则

  推荐做法:

<!-- ✅ 好的命名:语义明确、英文小写、下划线分隔 -->
<tool_calling>
<code_style>
<error_handling>
<user_context>

<!-- ❌ 不推荐:含义模糊或过于简短 -->
<rules>        <!-- 太泛,什么规则? -->
<tc>           <!-- 缩写难理解 -->
<section1>     <!-- 无语义 -->

  命名建议:

(1)动词+名词 或 名词短语

(2)能一眼看出这块内容是干什么的

(3)保持命名风格一致(全小写+下划线,或驼峰)

5、常用标签模板(可直接套用)

  根据常见场景,我整理了一套通用标签:

<!-- 1. 角色定义 -->
<role>你是一个[角色],擅长[能力]</role>

<!-- 2. 行为约束 -->
<constraints>
- ALWAYS: ...
- NEVER: ...
</constraints>

<!-- 3. 输出格式 -->
<output_format>
- 使用 Markdown
- 代码块标注语言
</output_format>

<!-- 4. 工作流程 -->
<workflow>
1. 先分析问题
2. 再给出方案
3. 最后实现代码
</workflow>

<!-- 5. 示例 -->
<examples>
<example type="good">...</example>
<example type="bad">...</example>
</examples>

<!-- 6. 上下文注入(动态填充) -->
<context>
<user_os>{{OS}}</user_os>
<current_file>{{FILE}}</current_file>
</context>

  核心原则: 标签名是给你自己和AI看的,只要能清晰表达"这块内容是什么"就是好标签。

三、系统提示词在日常开发中的实际应用

  我把它分成 三个层级,从一次性配置到日常使用:

1、第一层:在 Cursor 中配置项目级规则(配一次,长期受益)

  在项目根目录创建 .cursor/rules 文件,Cursor 会自动加载, AI 生成的代码会自动遵循你的项目规范。

2、第二层:针对具体任务的提示词模板(保存几个常用的)

(1)场景 1:组件开发(下方使用的 xml 形式,也可以使用 md 等文档,主要层次清晰、表述清楚即可)

<task>开发一个用户卡片组件</task>

<requirements>
- 显示头像、昵称、简介
- 支持点击跳转到用户详情
- 响应式布局,移动端适配
</requirements>

<constraints>
- 使用 Element Plus 的 el-card 作为容器
- props 必须定义 TypeScript 接口
- 提供 loading 和 empty 状态
</constraints>

<example>
// 期望的调用方式
<UserCard :user="userData" @click="goDetail" />
</example>

(2)场景 2:Bug 修复

<bug>
表格分页切换后,数据没有刷新
</bug>

<current_behavior>
点击第2页,请求发出了,但表格显示的还是第1页数据
</current_behavior>

<expected_behavior>
切换分页后,表格显示对应页的数据
</expected_behavior>

<related_files>
- src/views/user/list.vue(表格组件)
- src/api/user.ts(API 请求)
</related_files>

(3)场景3:代码审查/重构提示词

<task>审查以下代码,指出问题并优化</task>

<review_criteria>
1. 性能问题(不必要的渲染、内存泄漏)
2. TypeScript 类型是否完善
3. 代码可读性和可维护性
4. 是否符合 Vue 3 最佳实践
</review_criteria>

<output_format>
## 问题列表
| 行号 | 问题 | 严重程度 | 建议 |

## 优化后的代码
--
</output_format>

3、第三层:日常对话中的快捷提示词:不用每次写完整提示词,可以用简短模式触发:

  场景:写组件,快捷提示词:`写一个 [组件名],props: [xxx],功能: [xxx]`

  写函数 - `写一个函数:输入 [xxx],输出 [xxx],要求 [xxx]`

  改 bug - `这段代码 [问题描述],期望 [xxx],帮我修复`

4、技巧总结

从提示词学到的技巧在日常开发中怎么用
结构化 需求描述分点写清楚
正反例 告诉 AI "要什么" 也告诉它 "不要什么"
上下文 用 @ 引用相关文件,让 AI 了解现有代码
边界约束 告诉 AI 限制条件(不超过多少行、用什么组件库)
具体化 不说"写个好点的",说"加 loading 状态和空数据提示"

  核心建议:

(1)先配好项目规则(一次配置,持续受益)

(2)积累常用模板(组件、API、重构各一套)

(3)对话时给足上下文(相关文件、期望结果、约束条件)

  一句话总结:项目规则配一次 → 常用模板存几个 → 日常对话说清楚(做什么 + 要什么 + 不要什么)

  记住这三要素:(1)做什么:写组件/改bug/重构...(2)要什么:具体功能、用什么库(3)不要什么:约束条件、禁止事项

  这样就能把系统提示词的技巧融入日常开发,让 AI 更懂你的意图,生成更符合项目规范的代码。

posted @ 2025-12-12 21:45  古兰精  阅读(451)  评论(0)    收藏  举报