prompt-engineering-patterns
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePrompt Engineering Patterns
提示词工程模式
Master advanced prompt engineering techniques to maximize LLM performance, reliability, and controllability.
掌握高级提示词工程技术,最大化LLM的性能、可靠性和可控性。
When to Use This Skill
适用场景
- Designing complex prompts for production LLM applications
- Optimizing prompt performance and consistency
- Implementing structured reasoning patterns (chain-of-thought, tree-of-thought)
- Building few-shot learning systems with dynamic example selection
- Creating reusable prompt templates with variable interpolation
- Debugging and refining prompts that produce inconsistent outputs
- Implementing system prompts for specialized AI assistants
- Using structured outputs (JSON mode) for reliable parsing
- 为生产级LLM应用设计复杂提示词
- 优化提示词的性能与一致性
- 实现结构化推理模式(思维链、思维树)
- 构建具备动态示例选择功能的少样本学习系统
- 创建支持变量插值的可复用提示词模板
- 调试和优化输出结果不一致的提示词
- 为专业AI助手设计系统提示词
- 使用结构化输出(JSON模式)实现可靠解析
Core Capabilities
核心能力
1. Few-Shot Learning
1. 少样本学习
- Example selection strategies (semantic similarity, diversity sampling)
- Balancing example count with context window constraints
- Constructing effective demonstrations with input-output pairs
- Dynamic example retrieval from knowledge bases
- Handling edge cases through strategic example selection
- 示例选择策略(语义相似度、多样性采样)
- 在示例数量与上下文窗口限制之间取得平衡
- 构建包含输入输出对的有效演示示例
- 从知识库中动态检索示例
- 通过策略性示例选择处理边缘情况
2. Chain-of-Thought Prompting
2. 思维链提示
- Step-by-step reasoning elicitation
- Zero-shot CoT with "Let's think step by step"
- Few-shot CoT with reasoning traces
- Self-consistency techniques (sampling multiple reasoning paths)
- Verification and validation steps
- 逐步引导推理过程
- 使用“Let's think step by step”实现零样本CoT
- 包含推理轨迹的少样本CoT
- 自一致性技术(采样多条推理路径)
- 验证与确认步骤
3. Structured Outputs
3. 结构化输出
- JSON mode for reliable parsing
- Pydantic schema enforcement
- Type-safe response handling
- Error handling for malformed outputs
- 用于可靠解析的JSON模式
- Pydantic schema 强制约束
- 类型安全的响应处理
- 格式错误输出的异常处理
4. Prompt Optimization
4. 提示词优化
- Iterative refinement workflows
- A/B testing prompt variations
- Measuring prompt performance metrics (accuracy, consistency, latency)
- Reducing token usage while maintaining quality
- Handling edge cases and failure modes
- 迭代优化工作流
- 提示词变体的A/B测试
- 衡量提示词性能指标(准确性、一致性、延迟)
- 在保持质量的同时减少Token使用量
- 处理边缘情况和故障模式
5. Template Systems
5. 模板系统
- Variable interpolation and formatting
- Conditional prompt sections
- Multi-turn conversation templates
- Role-based prompt composition
- Modular prompt components
- 变量插值与格式化
- 条件提示词片段
- 多轮对话模板
- 基于角色的提示词组合
- 模块化提示词组件
6. System Prompt Design
6. 系统提示词设计
- Setting model behavior and constraints
- Defining output formats and structure
- Establishing role and expertise
- Safety guidelines and content policies
- Context setting and background information
- 设置模型行为与约束
- 定义输出格式与结构
- 确立角色与专业领域
- 安全准则与内容政策
- 上下文设定与背景信息
Quick Start
快速开始
python
from langchain_anthropic import ChatAnthropic
from langchain_core.prompts import ChatPromptTemplate
from pydantic import BaseModel, Fieldpython
from langchain_anthropic import ChatAnthropic
from langchain_core.prompts import ChatPromptTemplate
from pydantic import BaseModel, FieldDefine structured output schema
Define structured output schema
class SQLQuery(BaseModel):
query: str = Field(description="The SQL query")
explanation: str = Field(description="Brief explanation of what the query does")
tables_used: list[str] = Field(description="List of tables referenced")
class SQLQuery(BaseModel):
query: str = Field(description="The SQL query")
explanation: str = Field(description="Brief explanation of what the query does")
tables_used: list[str] = Field(description="List of tables referenced")
Initialize model with structured output
Initialize model with structured output
llm = ChatAnthropic(model="claude-sonnet-4-5")
structured_llm = llm.with_structured_output(SQLQuery)
llm = ChatAnthropic(model="claude-sonnet-4-5")
structured_llm = llm.with_structured_output(SQLQuery)
Create prompt template
Create prompt template
prompt = ChatPromptTemplate.from_messages([
("system", """You are an expert SQL developer. Generate efficient, secure SQL queries.
Always use parameterized queries to prevent SQL injection.
Explain your reasoning briefly."""),
("user", "Convert this to SQL: {query}")
])
prompt = ChatPromptTemplate.from_messages([
("system", """You are an expert SQL developer. Generate efficient, secure SQL queries.
Always use parameterized queries to prevent SQL injection.
Explain your reasoning briefly."""),
("user", "Convert this to SQL: {query}")
])
Create chain
Create chain
chain = prompt | structured_llm
chain = prompt | structured_llm
Use
Use
result = await chain.ainvoke({
"query": "Find all users who registered in the last 30 days"
})
print(result.query)
print(result.explanation)
undefinedresult = await chain.ainvoke({
"query": "Find all users who registered in the last 30 days"
})
print(result.query)
print(result.explanation)
undefinedKey Patterns
关键模式
Pattern 1: Structured Output with Pydantic
模式1:基于Pydantic的结构化输出
python
from anthropic import Anthropic
from pydantic import BaseModel, Field
from typing import Literal
import json
class SentimentAnalysis(BaseModel):
sentiment: Literal["positive", "negative", "neutral"]
confidence: float = Field(ge=0, le=1)
key_phrases: list[str]
reasoning: str
async def analyze_sentiment(text: str) -> SentimentAnalysis:
"""Analyze sentiment with structured output."""
client = Anthropic()
message = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=500,
messages=[{
"role": "user",
"content": f"""Analyze the sentiment of this text.
Text: {text}
Respond with JSON matching this schema:
{{
"sentiment": "positive" | "negative" | "neutral",
"confidence": 0.0-1.0,
"key_phrases": ["phrase1", "phrase2"],
"reasoning": "brief explanation"
}}"""
}]
)
return SentimentAnalysis(**json.loads(message.content[0].text))python
from anthropic import Anthropic
from pydantic import BaseModel, Field
from typing import Literal
import json
class SentimentAnalysis(BaseModel):
sentiment: Literal["positive", "negative", "neutral"]
confidence: float = Field(ge=0, le=1)
key_phrases: list[str]
reasoning: str
async def analyze_sentiment(text: str) -> SentimentAnalysis:
"""Analyze sentiment with structured output."""
client = Anthropic()
message = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=500,
messages=[{
"role": "user",
"content": f"""Analyze the sentiment of this text.
Text: {text}
Respond with JSON matching this schema:
{{
"sentiment": "positive" | "negative" | "neutral",
"confidence": 0.0-1.0,
"key_phrases": ["phrase1", "phrase2"],
"reasoning": "brief explanation"
}}"""
}]
)
return SentimentAnalysis(**json.loads(message.content[0].text))Pattern 2: Chain-of-Thought with Self-Verification
模式2:带自验证的思维链
python
from langchain_core.prompts import ChatPromptTemplate
cot_prompt = ChatPromptTemplate.from_template("""
Solve this problem step by step.
Problem: {problem}
Instructions:
1. Break down the problem into clear steps
2. Work through each step showing your reasoning
3. State your final answer
4. Verify your answer by checking it against the original problem
Format your response as:python
from langchain_core.prompts import ChatPromptTemplate
cot_prompt = ChatPromptTemplate.from_template("""
Solve this problem step by step.
Problem: {problem}
Instructions:
1. Break down the problem into clear steps
2. Work through each step showing your reasoning
3. State your final answer
4. Verify your answer by checking it against the original problem
Format your response as:Steps
Steps
[Your step-by-step reasoning]
[Your step-by-step reasoning]
Answer
Answer
[Your final answer]
[Your final answer]
Verification
Verification
[Check that your answer is correct]
""")
undefined[Check that your answer is correct]
""")
undefinedPattern 3: Few-Shot with Dynamic Example Selection
模式3:带动态示例选择的少样本学习
python
from langchain_voyageai import VoyageAIEmbeddings
from langchain_core.example_selectors import SemanticSimilarityExampleSelector
from langchain_chroma import Chromapython
from langchain_voyageai import VoyageAIEmbeddings
from langchain_core.example_selectors import SemanticSimilarityExampleSelector
from langchain_chroma import ChromaCreate example selector with semantic similarity
Create example selector with semantic similarity
example_selector = SemanticSimilarityExampleSelector.from_examples(
examples=[
{"input": "How do I reset my password?", "output": "Go to Settings > Security > Reset Password"},
{"input": "Where can I see my order history?", "output": "Navigate to Account > Orders"},
{"input": "How do I contact support?", "output": "Click Help > Contact Us or email support@example.com"},
],
embeddings=VoyageAIEmbeddings(model="voyage-3-large"),
vectorstore_cls=Chroma,
k=2 # Select 2 most similar examples
)
async def get_few_shot_prompt(query: str) -> str:
"""Build prompt with dynamically selected examples."""
examples = await example_selector.aselect_examples({"input": query})
examples_text = "\n".join(
f"User: {ex['input']}\nAssistant: {ex['output']}"
for ex in examples
)
return f"""You are a helpful customer support assistant.Here are some example interactions:
{examples_text}
Now respond to this query:
User: {query}
Assistant:"""
undefinedexample_selector = SemanticSimilarityExampleSelector.from_examples(
examples=[
{"input": "How do I reset my password?", "output": "Go to Settings > Security > Reset Password"},
{"input": "Where can I see my order history?", "output": "Navigate to Account > Orders"},
{"input": "How do I contact support?", "output": "Click Help > Contact Us or email support@example.com"},
],
embeddings=VoyageAIEmbeddings(model="voyage-3-large"),
vectorstore_cls=Chroma,
k=2 # Select 2 most similar examples
)
async def get_few_shot_prompt(query: str) -> str:
"""Build prompt with dynamically selected examples."""
examples = await example_selector.aselect_examples({"input": query})
examples_text = "\n".join(
f"User: {ex['input']}\nAssistant: {ex['output']}"
for ex in examples
)
return f"""You are a helpful customer support assistant.Here are some example interactions:
{examples_text}
Now respond to this query:
User: {query}
Assistant:"""
undefinedPattern 4: Progressive Disclosure
模式4:渐进式披露
Start with simple prompts, add complexity only when needed:
python
PROMPT_LEVELS = {
# Level 1: Direct instruction
"simple": "Summarize this article: {text}",
# Level 2: Add constraints
"constrained": """Summarize this article in 3 bullet points, focusing on:
- Key findings
- Main conclusions
- Practical implications
Article: {text}""",
# Level 3: Add reasoning
"reasoning": """Read this article carefully.
1. First, identify the main topic and thesis
2. Then, extract the key supporting points
3. Finally, summarize in 3 bullet points
Article: {text}
Summary:""",
# Level 4: Add examples
"few_shot": """Read articles and provide concise summaries.
Example:
Article: "New research shows that regular exercise can reduce anxiety by up to 40%..."
Summary:
• Regular exercise reduces anxiety by up to 40%
• 30 minutes of moderate activity 3x/week is sufficient
• Benefits appear within 2 weeks of starting
Now summarize this article:
Article: {text}
Summary:"""
}从简单提示词开始,仅在需要时增加复杂度:
python
PROMPT_LEVELS = {
# Level 1: Direct instruction
"simple": "Summarize this article: {text}",
# Level 2: Add constraints
"constrained": """Summarize this article in 3 bullet points, focusing on:
- Key findings
- Main conclusions
- Practical implications
Article: {text}""",
# Level 3: Add reasoning
"reasoning": """Read this article carefully.
1. First, identify the main topic and thesis
2. Then, extract the key supporting points
3. Finally, summarize in 3 bullet points
Article: {text}
Summary:""",
# Level 4: Add examples
"few_shot": """Read articles and provide concise summaries.
Example:
Article: "New research shows that regular exercise can reduce anxiety by up to 40%..."
Summary:
• Regular exercise reduces anxiety by up to 40%
• 30 minutes of moderate activity 3x/week is sufficient
• Benefits appear within 2 weeks of starting
Now summarize this article:
Article: {text}
Summary:"""
}Pattern 5: Error Recovery and Fallback
模式5:错误恢复与回退
python
from pydantic import BaseModel, ValidationError
import json
class ResponseWithConfidence(BaseModel):
answer: str
confidence: float
sources: list[str]
alternative_interpretations: list[str] = []
ERROR_RECOVERY_PROMPT = """
Answer the question based on the context provided.
Context: {context}
Question: {question}
Instructions:
1. If you can answer confidently (>0.8), provide a direct answer
2. If you're somewhat confident (0.5-0.8), provide your best answer with caveats
3. If you're uncertain (<0.5), explain what information is missing
4. Always provide alternative interpretations if the question is ambiguous
Respond in JSON:
{{
"answer": "your answer or 'I cannot determine this from the context'",
"confidence": 0.0-1.0,
"sources": ["relevant context excerpts"],
"alternative_interpretations": ["if question is ambiguous"]
}}
"""
async def answer_with_fallback(
context: str,
question: str,
llm
) -> ResponseWithConfidence:
"""Answer with error recovery and fallback."""
prompt = ERROR_RECOVERY_PROMPT.format(context=context, question=question)
try:
response = await llm.ainvoke(prompt)
return ResponseWithConfidence(**json.loads(response.content))
except (json.JSONDecodeError, ValidationError) as e:
# Fallback: try to extract answer without structure
simple_prompt = f"Based on: {context}\n\nAnswer: {question}"
simple_response = await llm.ainvoke(simple_prompt)
return ResponseWithConfidence(
answer=simple_response.content,
confidence=0.5,
sources=["fallback extraction"],
alternative_interpretations=[]
)python
from pydantic import BaseModel, ValidationError
import json
class ResponseWithConfidence(BaseModel):
answer: str
confidence: float
sources: list[str]
alternative_interpretations: list[str] = []
ERROR_RECOVERY_PROMPT = """
Answer the question based on the context provided.
Context: {context}
Question: {question}
Instructions:
1. If you can answer confidently (>0.8), provide a direct answer
2. If you're somewhat confident (0.5-0.8), provide your best answer with caveats
3. If you're uncertain (<0.5), explain what information is missing
4. Always provide alternative interpretations if the question is ambiguous
Respond in JSON:
{{
"answer": "your answer or 'I cannot determine this from the context'",
"confidence": 0.0-1.0,
"sources": ["relevant context excerpts"],
"alternative_interpretations": ["if question is ambiguous"]
}}
"""
async def answer_with_fallback(
context: str,
question: str,
llm
) -> ResponseWithConfidence:
"""Answer with error recovery and fallback."""
prompt = ERROR_RECOVERY_PROMPT.format(context=context, question=question)
try:
response = await llm.ainvoke(prompt)
return ResponseWithConfidence(**json.loads(response.content))
except (json.JSONDecodeError, ValidationError) as e:
# Fallback: try to extract answer without structure
simple_prompt = f"Based on: {context}\n\nAnswer: {question}"
simple_response = await llm.ainvoke(simple_prompt)
return ResponseWithConfidence(
answer=simple_response.content,
confidence=0.5,
sources=["fallback extraction"],
alternative_interpretations=[]
)Pattern 6: Role-Based System Prompts
模式6:基于角色的系统提示词
python
SYSTEM_PROMPTS = {
"analyst": """You are a senior data analyst with expertise in SQL, Python, and business intelligence.
Your responsibilities:
- Write efficient, well-documented queries
- Explain your analysis methodology
- Highlight key insights and recommendations
- Flag any data quality concerns
Communication style:
- Be precise and technical when discussing methodology
- Translate technical findings into business impact
- Use clear visualizations when helpful""",
"assistant": """You are a helpful AI assistant focused on accuracy and clarity.
Core principles:
- Always cite sources when making factual claims
- Acknowledge uncertainty rather than guessing
- Ask clarifying questions when the request is ambiguous
- Provide step-by-step explanations for complex topics
Constraints:
- Do not provide medical, legal, or financial advice
- Redirect harmful requests appropriately
- Protect user privacy""",
"code_reviewer": """You are a senior software engineer conducting code reviews.
Review criteria:
- Correctness: Does the code work as intended?
- Security: Are there any vulnerabilities?
- Performance: Are there efficiency concerns?
- Maintainability: Is the code readable and well-structured?
- Best practices: Does it follow language idioms?
Output format:
1. Summary assessment (approve/request changes)
2. Critical issues (must fix)
3. Suggestions (nice to have)
4. Positive feedback (what's done well)"""
}python
SYSTEM_PROMPTS = {
"analyst": """You are a senior data analyst with expertise in SQL, Python, and business intelligence.
Your responsibilities:
- Write efficient, well-documented queries
- Explain your analysis methodology
- Highlight key insights and recommendations
- Flag any data quality concerns
Communication style:
- Be precise and technical when discussing methodology
- Translate technical findings into business impact
- Use clear visualizations when helpful""",
"assistant": """You are a helpful AI assistant focused on accuracy and clarity.
Core principles:
- Always cite sources when making factual claims
- Acknowledge uncertainty rather than guessing
- Ask clarifying questions when the request is ambiguous
- Provide step-by-step explanations for complex topics
Constraints:
- Do not provide medical, legal, or financial advice
- Redirect harmful requests appropriately
- Protect user privacy""",
"code_reviewer": """You are a senior software engineer conducting code reviews.
Review criteria:
- Correctness: Does the code work as intended?
- Security: Are there any vulnerabilities?
- Performance: Are there efficiency concerns?
- Maintainability: Is the code readable and well-structured?
- Best practices: Does it follow language idioms?
Output format:
1. Summary assessment (approve/request changes)
2. Critical issues (must fix)
3. Suggestions (nice to have)
4. Positive feedback (what's done well)"""
}Integration Patterns
集成模式
With RAG Systems
与RAG系统集成
python
RAG_PROMPT = """You are a knowledgeable assistant that answers questions based on provided context.
Context (retrieved from knowledge base):
{context}
Instructions:
1. Answer ONLY based on the provided context
2. If the context doesn't contain the answer, say "I don't have information about that in my knowledge base"
3. Cite specific passages using [1], [2] notation
4. If the question is ambiguous, ask for clarification
Question: {question}
Answer:"""python
RAG_PROMPT = """You are a knowledgeable assistant that answers questions based on provided context.
Context (retrieved from knowledge base):
{context}
Instructions:
1. Answer ONLY based on the provided context
2. If the context doesn't contain the answer, say "I don't have information about that in my knowledge base"
3. Cite specific passages using [1], [2] notation
4. If the question is ambiguous, ask for clarification
Question: {question}
Answer:"""With Validation and Verification
与验证机制集成
python
VALIDATED_PROMPT = """Complete the following task:
Task: {task}
After generating your response, verify it meets ALL these criteria:
✓ Directly addresses the original request
✓ Contains no factual errors
✓ Is appropriately detailed (not too brief, not too verbose)
✓ Uses proper formatting
✓ Is safe and appropriate
If verification fails on any criterion, revise before responding.
Response:"""python
VALIDATED_PROMPT = """Complete the following task:
Task: {task}
After generating your response, verify it meets ALL these criteria:
✓ Directly addresses the original request
✓ Contains no factual errors
✓ Is appropriately detailed (not too brief, not too verbose)
✓ Uses proper formatting
✓ Is safe and appropriate
If verification fails on any criterion, revise before responding.
Response:"""Performance Optimization
性能优化
Token Efficiency
Token效率
python
undefinedpython
undefinedBefore: Verbose prompt (150+ tokens)
Before: Verbose prompt (150+ tokens)
verbose_prompt = """
I would like you to please take the following text and provide me with a comprehensive
summary of the main points. The summary should capture the key ideas and important details
while being concise and easy to understand.
"""
verbose_prompt = """
I would like you to please take the following text and provide me with a comprehensive
summary of the main points. The summary should capture the key ideas and important details
while being concise and easy to understand.
"""
After: Concise prompt (30 tokens)
After: Concise prompt (30 tokens)
concise_prompt = """Summarize the key points concisely:
{text}
Summary:"""
undefinedconcise_prompt = """Summarize the key points concisely:
{text}
Summary:"""
undefinedCaching Common Prefixes
通用前缀缓存
python
from anthropic import Anthropic
client = Anthropic()python
from anthropic import Anthropic
client = Anthropic()Use prompt caching for repeated system prompts
Use prompt caching for repeated system prompts
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1000,
system=[
{
"type": "text",
"text": LONG_SYSTEM_PROMPT,
"cache_control": {"type": "ephemeral"}
}
],
messages=[{"role": "user", "content": user_query}]
)
undefinedresponse = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1000,
system=[
{
"type": "text",
"text": LONG_SYSTEM_PROMPT,
"cache_control": {"type": "ephemeral"}
}
],
messages=[{"role": "user", "content": user_query}]
)
undefinedBest Practices
最佳实践
- Be Specific: Vague prompts produce inconsistent results
- Show, Don't Tell: Examples are more effective than descriptions
- Use Structured Outputs: Enforce schemas with Pydantic for reliability
- Test Extensively: Evaluate on diverse, representative inputs
- Iterate Rapidly: Small changes can have large impacts
- Monitor Performance: Track metrics in production
- Version Control: Treat prompts as code with proper versioning
- Document Intent: Explain why prompts are structured as they are
- 明确具体:模糊的提示词会导致结果不一致
- 示例优先:示例比描述更有效
- 使用结构化输出:通过Pydantic强制约束Schema以确保可靠性
- 充分测试:使用多样化、有代表性的输入进行评估
- 快速迭代:小改动可能带来大影响
- 监控性能:在生产环境中跟踪指标
- 版本控制:将提示词视为代码进行妥善版本管理
- 记录意图:解释提示词结构设计的原因
Common Pitfalls
常见陷阱
- Over-engineering: Starting with complex prompts before trying simple ones
- Example pollution: Using examples that don't match the target task
- Context overflow: Exceeding token limits with excessive examples
- Ambiguous instructions: Leaving room for multiple interpretations
- Ignoring edge cases: Not testing on unusual or boundary inputs
- No error handling: Assuming outputs will always be well-formed
- Hardcoded values: Not parameterizing prompts for reuse
- 过度设计:在尝试简单提示词之前就使用复杂提示词
- 示例污染:使用与目标任务不匹配的示例
- 上下文溢出:示例过多导致超出Token限制
- 指令模糊:留下多种解释空间
- 忽略边缘情况:未针对异常或边界输入进行测试
- 无错误处理:假设输出始终格式正确
- 硬编码值:未对提示词进行参数化以实现复用
Success Metrics
成功指标
Track these KPIs for your prompts:
- Accuracy: Correctness of outputs
- Consistency: Reproducibility across similar inputs
- Latency: Response time (P50, P95, P99)
- Token Usage: Average tokens per request
- Success Rate: Percentage of valid, parseable outputs
- User Satisfaction: Ratings and feedback
跟踪以下提示词关键绩效指标:
- 准确性:输出结果的正确性
- 一致性:相似输入下的结果可重复性
- 延迟:响应时间(P50、P95、P99)
- Token使用量:每次请求的平均Token数
- 成功率:有效、可解析输出的百分比
- 用户满意度:评分与反馈