AI Prompt Engineer's Guide — 12 Techniques to Double AI Output Quality

Prompt 是 2026 年最值钱的技能之一。同样的 AI 模型好的 prompt 能让输出质量翻倍差的 prompt 则浪费 token。本文分享 12 个经过实战验证的 prompt 技巧覆盖基础结构高级模式输出控制。每个技巧都有对比示例。

Prompt 工程的重要性

Prompt 决定了 AI 输出的质量。一个研究案例同一个任务写产品文案差的 prompt 平均质量 3.2 分好的 prompt 8.7 分 2.7 倍差距。这意味着用对 prompt 相当于免费升级到更强的模型。2026 年 Prompt Engineer 已经成为正式职位硅谷大厂年薪 20 万美元以上。

技巧 1-3 基础结构

技巧 1 明确角色。你是一位资深营销文案专家擅长科技产品。让 AI 进入角色输出风格立刻专业。技巧 2 给具体任务。不要说写一篇文章要说写一篇 800 字的微信公众号文章主题是 X 面向 25 到 35 岁互联网从业者。技巧 3 输出格式。用 markdown 格式输出包含 3 个 H2 小标题每段不超过 100 字。格式约束让结果可直接用。

技巧 4-6 高级模式

技巧 4 Chain of Thought。Let us think step by step。让 AI 显式推理数学逻辑题准确率提升 30 percent 以上。技巧 5 Few-shot 示例。给两到三个 input output 示例 AI 学会模仿比抽象描述有效 10 倍。技巧 6 角色扮演。扮演我的产品经理给我的 app 提改进建议。让 AI 从特定视角思考避免泛泛而谈。

技巧 7-9 输出控制

技巧 7 温度控制。创意写作 temperature 等于 1.0 事实问答 0.3 代码生成 0。DrAI API 支持 temperature 参数。技巧 8 长度约束。不超过 200 字或详细展开 2000 字。AI 容易啰嗦明确长度很重要。技巧 9 负面约束。不要用术语不要超过 3 个 bullet。明确告诉 AI 不要做什么。

技巧 10-12 质量提升

技巧 10 自我批评。先让 AI 生成再让它作为 critic 审视上面的输出找出 3 个问题并改进。两轮迭代质量大幅提升。技巧 11 多视角生成。从用户产品技术三个角度分析这个功能。多视角比单角度全面。技巧 12 知识注入。在 prompt 里粘贴相关资料产品文档用户调研让 AI 基于你的上下文回答。这是 RAG 的本质。

不同模型的 Prompt 差异

GPT-5.6 对结构化 prompt 友好喜欢明确的格式指令。Claude Opus 4 擅长长文分析给系统 prompt 时效果更好。DeepSeek R1 中文优化中文 prompt 效果比英文好。Qwen 3 对中文成语俗语理解到位。通用建议是用模型擅长的语言写 prompt 中文模型用中文 prompt 英文模型用英文。

Prompt 模板库

积累常用的 prompt 模板大幅提升效率。邮件写作你是商务沟通专家给收件人写一封关于主题的邮件语气正式或友好要点列表不超过 200 字。代码 review 你是资深工程师 Review 以下代码从可读性性能安全可维护性四个角度点评给出具体改进建议。翻译润色翻译为目标语言保持专业语气本地化术语。总结提炼用 3 个 bullet 总结以下内容的核心观点。

常见错误

错误 1 模糊指令帮我写个好文章改为写一篇关于 X 的 800 字文章面向 Y。错误 2 信息过载一个 prompt 里塞 10 个要求拆成多次对话。错误 3 假设 AI 知道不提供背景永远先给 context。错误 4 不迭代一次不满意就放弃让 AI 改进上一版。错误 5 忽略 system prompt 把所有要求放 user message 用 system message 设定角色和约束。

总结

掌握 prompt 工程让你的 AI 使用效率翻倍。DrAI 平台支持所有主流模型按量付费。

每个技巧的 Before/After 实例

抽象描述不够直观。以下是每个技巧的真实对比示例,让你看到质量差距:

技巧 1(明确角色)对比

:写一段产品介绍

你是一位有 10 年经验的科技产品文案专家,
擅长把复杂技术转化为用户易懂的利益点。

任务:为 DrAI(AI API 平台)写产品介绍
受众:中小企业的技术负责人
字数:150 字以内
风格:专业但不枯燥,要有具体数字

差距:差的输出是泛泛的"我们提供先进的 AI 服务"。好的输出会写"DrAI 让你用一个 API 调用 40+ 顶级模型,成本降低 70%"。质量差 3 倍。

技巧 4(Chain of Thought)对比

:一个商店打八折后卖 80 元,原价多少?

:一个商店打八折后卖 80 元,原价多少?请一步步思考。

差距:不加 CoT,部分模型直接猜"100"但推理错误;加 CoT 后,模型输出"八折 = 0.8,80÷0.8 = 100"——不仅答案对,推理过程可审计。复杂数学题准确率提升 30%+。

技巧 5(Few-shot)对比

:把以下评论分类为正面或负面:"手机续航很差"

对用户评论进行情感分类。

示例:
输入:"拍照很清楚,满意" → 正面
输入:"用了一周就坏了" → 负面
输入:"性价比不错,推荐" → 正面

现在分类:
输入:"手机续航很差" →

差距:Few-shot 让模型明确知道你的分类标准,准确率从 75% 到 95%。

结构化输出:让 AI 输出可直接用

2026 年最重要的 Prompt 技巧之一。让 AI 输出 JSON 而非自然语言,便于程序处理:

JSON 模式

从以下产品评论中提取信息,输出 JSON 格式:

{
  "sentiment": "正面/负面/中性",
  "aspects": ["提及的产品方面,如电池、屏幕"],
  "score": 1-5,
  "summary": "一句话总结"
}

评论:"手机拍照很清楚,但电池续航一天都撑不到,有点失望。"

AI 返回:

{
  "sentiment": "负面",
  "aspects": ["拍照", "电池"],
  "score": 3,
  "summary": "拍照优秀但续航严重不足"
}

Function Calling 实现结构化

tools = [{
    "type": "function",
    "function": {
        "name": "extract_product_info",
        "parameters": {
            "type": "object",
            "properties": {
                "name": {"type": "string"},
                "price": {"type": "number"},
                "currency": {"type": "string", "enum": ["CNY", "USD"]}
            }
        }
    }
}]

response = client.chat.completions.create(
    model="gpt-5.6",
    messages=[{"role": "user", "content": "iPhone 16 Pro 256GB 售价 9999 元"}],
    tools=tools,
    tool_choice={"type": "function", "function": {"name": "extract_product_info"}}
)
# 100% 返回结构化 JSON,无需解析自然语言

高级技巧:Prompt 组合拳

组合 1:CoT + Few-shot + 角色

你是一位资深数据分析师。(角色)

分析以下销售数据并找出异常。(任务)

思考步骤:1. 计算环比 2. 对比历史均值 3. 标记偏差>20%的项。(CoT)

参考示例:(Few-shot)
输入:[1月100, 2月105, 3月180] → 异常:3月(环比+71%)
输入:[1月200, 2月195, 3月198] → 无明显异常

数据:[10月500, 11月520, 12月800]

组合 2:自我批评循环

第一轮:
"写一封求职信,应聘高级前端工程师"

第二轮(同一对话):
"作为资深 HR,审视上面的求职信:
1. 有哪些可以改进的地方?
2. 哪些表述不够有说服力?
3. 列出 3 个具体改进建议。"

第三轮:
"根据你的建议,重写这封求职信。"

三轮迭代后,质量比第一轮提升 40-60%。这就是为什么 Claude Code、Cursor 等 Agent 工具都内置了"自我审查"循环。

组合 3:多视角 + 决策

分别从以下 3 个视角分析"是否应该用 microservices 重构这个单体应用":

1. CTO 视角:关注技术债、团队能力、长期演进
2. 运维视角:关注部署复杂度、监控成本、故障排查
3. 业务视角:关注交付速度、功能迭代、市场响应

每个视角给出 2 个支持、2 个反对的理由。
最后,综合三个视角,给出你的建议(实施/不实施/暂缓)及理由。

不同任务的 Prompt 模板库

代码生成模板

角色:你是一位{语言}资深工程师,代码规范遵循 Google Style。

任务:实现{功能描述}

要求:
1. 添加类型标注和 docstring
2. 处理边界条件和异常
3. 时间复杂度不超过 O(n log n)
4. 附带 3 个单元测试用例

技术栈:{框架版本}
特殊约束:{如有}

代码审查模板

角色:资深 Code Reviewer

任务:审查以下代码,从 4 个维度点评:
1. 可读性(命名、注释、结构)
2. 性能(是否有 N+1、不必要计算)
3. 安全(注入、XSS、权限)
4. 可维护性(耦合度、扩展性)

输出格式:每个维度打分(1-5)+ 具体改进建议

代码:
{粘贴代码}

翻译润色模板

任务:将以下{源语言}翻译为{目标语言}

要求:
- 保持专业语气,适合{场景:正式/休闲/学术}
- 本地化术语,不直译(如"cloud computing"不译为"云彩计算")
- 保留原文的格式(列表、加粗、链接)
- 遇到歧义保留原文并在[]中标注

原文:
{粘贴文本}

会议纪要模板

从以下会议录音转写中提取:

1. 讨论的核心议题(不超过 3 个)
2. 每个议题的关键观点(正方/反方)
3. 达成的共识
4. 待办事项(负责人 + 截止日期)
5. 遗留争议

格式:Markdown,每项一句话,重点加粗。

转写内容:
{粘贴文本}

SQL 生成模板

角色:资深数据工程师

表结构:
- users(id, name, email, created_at, plan)
- orders(id, user_id, amount, status, created_at)

任务:查询{需求描述}

要求:
1. 用标准 SQL(PostgreSQL 方言)
2. 添加注释解释关键逻辑
3. 考虑性能(索引、JOIN 优化)
4. 提供 EXPLAIN 分析建议

Prompt 工程的常见陷阱

陷阱表现解决方案
指令冲突同时说"详细"和"简短"一次只给一个主指令,分多轮
假设 AI 知道背景"帮我改 bug"不给代码永远提供完整 context
负面指令太弱"不要啰嗦"AI 照样啰嗦明确字数上限:"不超过100字"
示例不一致Few-shot 示例格式混乱示例格式严格统一
忽略 system prompt把角色放 user message角色和约束放 system message
一次问太多一个 Prompt 问 10 个问题拆成多轮对话
过度约束约束太多 AI 无所适从核心约束不超过 5 条

不同模型的 Prompt 策略差异

模型偏好避免最佳实践
GPT-5.6结构化、明确格式指令过长 system prompt用 markdown 分节,明确输出格式
Claude Opus 4详细 system prompt、XML 标签过于简短的指令用 <task><context> 标签组织
DeepSeek R1中文指令、CoT英文 prompt 翻译成中文直接用中文写,用"逐步思考"
Qwen 3中文文化语境、成语过于西化的表达用中文自然表达,利用文化常识
Gemini 2.5 Pro多模态输入、搜索增强纯文本长描述配图说明,用搜索获取最新信息

Prompt 调试方法论

Prompt 不是一次写好,是调试出来的。建议的调试流程:

步骤 1:建立测试集

准备 10-20 个典型输入(覆盖你期望的场景和边界情况)。每次改 Prompt 都在这套测试集上验证。

步骤 2:A/B 对比

用 Prompt A 和 Prompt B 分别跑同一批输入,盲评哪个输出更好。避免"改了感觉变好"的主观偏差。

步骤 3:失败分析

对于输出不合格的案例,问:是指令不清?示例不够?还是任务本身超出模型能力?针对性改进。

步骤 4:渐进复杂化

从最简 Prompt 开始,逐步加约束。不要一上来写 500 字 Prompt——每加一条约束,验证它确实提升了输出。

步骤 5:版本管理

把 Prompt 当代码管理。Git 版本控制,每次改动记录效果变化。生产环境用版本化的 Prompt,便于回滚。

Prompt 工程的进阶方向

RAG(检索增强生成)

Prompt 塞不下所有知识?用 RAG:先从你的知识库检索相关文档,再把文档作为 context 注入 Prompt。这是企业 AI 应用的标配。详见 Function Calling 指南

Agent Prompting

让 AI 自主规划多步骤任务。核心 Prompt 模式:ReAct(Reason + Act)——思考 → 行动 → 观察结果 → 再思考。

Prompt 编译与优化

2026 出现的工具:DSPy、Promptfoo。它们自动优化 Prompt——你定义评估指标,工具自动搜索最优 Prompt 表述。

2026 年 Prompt 工程趋势

Advanced Prompt Engineering Techniques

Beyond basic prompt construction, advanced techniques unlock significantly better model performance. These methods, developed through extensive experimentation and validated across thousands of production deployments, represent the state of the art in human-AI communication.

Chain-of-Thought Prompting

Chain-of-thought (CoT) prompting asks the model to reason through problems step-by-step before producing a final answer. This technique improves accuracy on complex reasoning tasks by 15-40%. The simplest implementation adds the phrase "Let's think step by step" to your prompt. More sophisticated CoT prompts provide a reasoning template: "First, identify the key variables. Second, list the constraints. Third, evaluate each option against the constraints. Finally, recommend the best option." This structured approach prevents the model from jumping to conclusions and reduces hallucination on multi-step problems. For mathematical reasoning, CoT is particularly powerful — models that fail on direct answers succeed when asked to show their work.

Self-Consistency Sampling

Self-consistency is an extension of chain-of-thought that generates multiple independent reasoning paths and selects the most common answer. Instead of asking the model once, you ask it 5-10 times with temperature 0.7 (to encourage diverse reasoning paths), then take a majority vote on the final answer. This technique improves accuracy by 8-15% on reasoning benchmarks at the cost of additional API calls. For high-stakes decisions where accuracy is paramount, the cost is easily justified. The key insight is that correct reasoning paths are more likely to converge on the same answer, while incorrect paths diverge randomly.

Tree of Thoughts

Tree of Thoughts (ToT) extends chain-of-thought into a search problem. Instead of a single reasoning chain, the model explores multiple branches of reasoning, evaluates each branch's promise, and prunes unpromising paths. This technique excels on problems with large solution spaces — puzzle solving, strategic planning, creative tasks with multiple valid approaches. Implementation requires multiple API calls per branch point, making it expensive. However, for problems where a single correct answer is critical, ToT can find solutions that elude simpler prompting strategies. A practical compromise is "best-of-N" sampling, where you generate N reasoning chains and select the best one based on a scoring function.

ReAct (Reasoning + Acting)

The ReAct pattern interleaves reasoning steps with actions — typically API calls or tool usage. The model reasons about what information it needs, takes an action to obtain that information (e.g., searching a database, calling an API), then reasons about the results before taking the next action. This pattern is foundational for AI agent development. A typical ReAct cycle: Thought ("I need to check the user's order status") → Action (call order API) → Observation (order is delayed) → Thought ("I should explain the delay and offer compensation") → Action (compose response). ReAct prompts must clearly define available actions and expected observation formats to prevent the model from hallucinating tool outputs.

Few-Shot Example Patterns

Few-shot prompting provides the model with examples of desired input-output pairs before the actual query. This technique is one of the most reliable ways to improve output quality, format consistency, and task adherence.

Selecting Effective Examples

The quality of few-shot examples matters more than quantity. Three well-chosen examples typically outperform ten mediocre ones. Effective examples should: cover the range of expected input variations (easy, medium, hard cases), demonstrate the desired output format precisely, include edge cases that test model understanding, and avoid biasing the model toward specific answer patterns. A common mistake is using examples that are too similar to each other — diversity in examples helps the model generalize the pattern rather than memorize specific mappings.

Example Formatting Strategies

The format of few-shot examples significantly impacts results. Using clear delimiters between examples helps the model distinguish examples from the actual query. XML-style tags (, , ) provide structure that models parse reliably. Numbered examples work well for sequential tasks. For classification tasks, presenting examples as input-label pairs with consistent formatting improves accuracy by 10-20% compared to free-form examples. Always maintain identical formatting between examples and the actual query — inconsistent formatting confuses the model and degrades performance.

Dynamic Example Selection

Instead of static few-shot examples, dynamic selection chooses the most relevant examples for each specific query. This technique, called retrieval-augmented few-shot prompting, embeds a pool of examples and the current query, then selects the K most similar examples based on embedding cosine similarity. This approach consistently outperforms static examples by 5-12% because it provides the model with context most relevant to the current input. The trade-off is additional latency for embedding computation and similarity search. For production systems, caching embeddings and using approximate nearest neighbor search (ANN) keeps overhead manageable.

Negative Examples

Showing the model what not to do is as important as showing correct behavior. Negative examples — inputs with explicitly incorrect outputs labeled as "wrong" — help the model avoid common failure modes. For example, in a summarization task, including an example where the summary contains hallucinated facts (labeled as incorrect) teaches the model to stick to source material. Negative examples are particularly effective for reducing specific, recurring errors. The format should clearly contrast correct and incorrect approaches so the model learns the distinguishing criteria, not just the surface pattern.

Chain-of-Thought Patterns Library

Mathematical Problem Solving

For mathematical problems, structured CoT prompts that enforce a specific problem-solving sequence yield the best results. The pattern: (1) Restate the problem in your own words. (2) Identify given information and what needs to be found. (3) List relevant formulas or principles. (4) Substitute known values. (5) Solve step by step. (6) Verify the answer by substitution or estimation. This pattern reduces arithmetic errors and helps the model catch its own mistakes during the verification step.

Code Debugging

For debugging tasks, CoT prompts should guide the model through systematic investigation: (1) Describe what the code should do. (2) Trace through the code with the given input. (3) Identify where the actual behavior diverges from expected. (4) Hypothesize the root cause. (5) Propose a fix. (6) Explain why the fix addresses the root cause without introducing new issues. This methodical approach catches bugs that direct "find the bug" prompts miss, particularly subtle logic errors.

Analytical Writing

For analytical writing tasks — reports, essays, evaluations — CoT prompts improve structure and depth: (1) Identify the core question or thesis. (2) List key arguments for and against. (3) Evaluate the strength of each argument. (4) Synthesize findings into a conclusion. (5) Identify limitations and counterarguments. This produces more balanced, well-reasoned output than open-ended "write an analysis" prompts.

Debug Workflow for Prompt Engineering

When model output doesn't meet expectations, a systematic debugging workflow identifies and fixes the root cause efficiently.

Step 1: Isolate the Problem

Determine whether the issue is with the prompt, the model, or the evaluation criteria. Test the same prompt with a different model — if the output improves, the original model may lack capability for the task. Test a simplified version of the prompt — if the simplified version works, the issue is likely prompt complexity or ambiguity. Test with a different evaluation criterion — sometimes the output is correct but the evaluation rubric is too strict or misaligned with actual goals.

Step 2: Gradient Analysis

Borrowing from machine learning, "prompt gradient analysis" examines how small prompt changes affect output. Change one element at a time — wording, example count, temperature, system prompt — and measure the effect. This methodical approach identifies which prompt components are contributing to poor performance. Document your experiments systematically: prompt version, changed component, output quality score, and observations. This creates a knowledge base for future prompt optimization.

Step 3: Error Categorization

Classify output errors into categories: format errors (wrong structure, missing fields), content errors (factual mistakes, hallucinations), tone errors (wrong style or register), and completeness errors (missing sections or points). Each category has different fixes: format errors benefit from stricter output schemas and more few-shot examples. Content errors require additional context or retrieval augmentation. Tone errors need better system prompts and style examples. Completeness errors benefit from explicit checklists in the prompt.

Step 4: Iterative Refinement

Prompt optimization is iterative. Make targeted changes based on error analysis, test with a diverse set of inputs, measure results, and repeat. Maintain a version history of prompts with associated quality metrics. Use A/B testing in production to validate improvements — deploy new prompts to a small percentage of traffic and compare quality metrics against the baseline. Roll back if quality degrades. Over time, this process transforms an adequate prompt into an excellent one, with measurable quality improvements at each iteration.

Step 5: Regression Testing

As prompts evolve, previously working cases may break. Maintain a regression test suite — a collection of input-output pairs that should continue producing correct results. Run this suite after every prompt change. Automated regression testing catches unintended side effects before they reach production. For mission-critical applications, implement continuous evaluation where a sample of production traffic is automatically scored for quality, alerting the team if quality drops below thresholds.

📚 Related Reading

Preventing AI Hallucinations: 7 Proven Techniques for LLM R…Seven battle-tested techniques to prevent AI hallucinations in production LLM apps: RAG... Prompt Injection Defense: Securing LLM Apps in ProductionPrompt injection is OWASP's #1 LLM vulnerability. This guide covers input isolation... Smart AI Model Routing: Auto-Select the Best LLM per QueryLearn how to build an intelligent AI model routing system that auto-selects the cheapest... Token Optimization Techniques: Cut LLM Costs Without Losing…Practical token optimization techniques for LLM applications. Learn prompt compression...
🌐 English