来源:https://github.com/anthropics/courses/blob/master/real_world_prompting/03_prompt_engineering.ipynb 爬取日期:2026-03-22
Lesson 3: Prompt engineering
In the first lesson, we quickly reviewed some key prompting tips. In the second lesson, we wrote a prompt that “blindly” applied all of those tips to a single prompt. Understanding these tips is critical, but it’s equally important to understand the prompt engineering workflow and decision making framework.
What is prompt engineering?
Prompt engineering is the art and science of crafting effective instructions for large language models like Claude to produce desired outputs. At its core, prompt engineering involves designing, refining, and optimizing the text inputs (prompts) given to models to elicit accurate, relevant, and useful responses. It’s about “communicating” with Claude in a way that maximizes the model’s understanding and performance on a given task. The importance of prompt engineering cannot be overstated:
- Enhancing AI capabilities: Well-engineered prompts can dramatically improve an AI’s performance, enabling it to tackle complex tasks with greater accuracy and efficiency.
- Bridging the gap between human intent and AI output: Prompt engineering helps translate human objectives into language that AI models can effectively interpret and act upon.
- Optimizing resource usage: Skilled prompt engineering can reduce token usage, lowering costs and improving response times in production environments.
How is it different from “basic prompting”?
Let’s define “basic prompting” as simply asking an AI model a question or giving it a straightforward instruction. Prompt engineering, on the other hand, is a more sophisticated and deliberate process.
- Complexity: Basic prompting often involves single-turn interactions with simple queries. Prompt engineering, on the other hand, may involve multi-turn conversations, complex instructions, and carefully structured inputs and outputs.
- Precision: Basic prompts might be vague or ambiguous, leading to inconsistent results. This might be fine in one-off prompt situations but won’t scale to production use cases. Engineered prompts are precise, leaving little room for misinterpretation by the model.
- Iterative refinement: Unlike basic prompting, which might be a one-off activity, prompt engineering involves systematic testing, analysis, and improvement of prompts over time.
- Scalability: Prompt engineering aims to create prompts that can handle a wide range of inputs and use cases, making them suitable for production environments.
In essence, prompt engineering elevates the interaction with a model from a casual conversation to a carefully orchestrated exchange designed to maximize the model’s potential in solving real-world problems repeatably. As we progress through this course, you’ll learn the techniques and mindset needed to master this crucial skill.
The Prompt Engineering Lifecycle
Prompt engineering follows a structured lifecycle that helps you iteratively improve your prompts and achieve better results:
1. Plan
Start by clearly defining your task and success criteria. Ask yourself:
- What is the exact task I want the model to perform?
- What are the inputs and expected outputs?
- What does success look like? How will I measure it?
- Are there edge cases I need to handle?
2. Draft
Create your initial prompt. Incorporate the best practices we learned in Lesson 1:
- Be direct
- Include role/persona
- Provide context
- Give examples
- Specify the format
- Ask for reasoning
- Use clear markers
3. Evaluate
Test your prompt against sample inputs and evaluate the outputs:
- Does it produce the expected format?
- Is the quality consistent?
- Does it handle edge cases well?
- Are there failure modes?
4. Refine
Based on your evaluation, make targeted improvements:
- Clarify ambiguous instructions
- Add more examples for complex tasks
- Adjust the tone or detail level
- Test again
5. Deploy
Once satisfied, integrate your prompt into your application and monitor real-world performance.
This cycle repeats as you encounter new patterns or requirements.
Key Decision Points in Prompt Engineering
When engineering a prompt, you’ll need to make several critical decisions:
Decision 1: What role should Claude play?
Should Claude be:
- An expert in a specific domain?
- A neutral analyzer?
- A creative assistant?
- A teacher?
- A code reviewer?
The role shapes the tone, knowledge level, and approach Claude takes.
Decision 2: How much context does Claude need?
Balance between:
- Too little: Claude may make incorrect assumptions
- Too much: Wastes tokens and may confuse the model
- Just right: Provides necessary context for accurate output
Decision 3: Should you use examples?
- Few-shot learning: Provide 1-3 examples to guide Claude
- Zero-shot: Trust Claude to understand from instructions alone
- When to use examples: Complex tasks, specific formats, edge cases
Decision 4: What format should the output be?
Specify:
- Plain text, JSON, XML, Markdown, etc.
- Structure (key-value pairs, lists, tables)
- Special formatting requirements
Decision 5: How should Claude handle uncertainty?
Tell Claude to:
- Ask for clarification
- Make reasonable assumptions and state them
- Decline to answer if uncertain
- Provide confidence levels
Decision 6: What constraints should you apply?
Consider:
- Length limits (tokens, words, sentences)
- Language requirements
- Tone/style requirements
- Factual constraints
Common Prompt Engineering Patterns
Pattern 1: The Expert Advisor
Define Claude as an expert and leverage that persona:
You are a senior software architect with 20 years of experience.
A developer is asking you for advice on...
Pattern 2: Structured Thinking
Ask Claude to break down its reasoning:
First, analyze the problem.
Then, consider the options.
Finally, provide your recommendation with reasoning.
Pattern 3: Few-Shot Learning
Provide examples of the desired behavior:
Here are examples of good summaries:
Example 1: [input] → [output]
Example 2: [input] → [output]
Now summarize this: [new input]
Pattern 4: Role + Format + Constraints
Combine multiple elements:
You are a technical writer. Convert this into a FAQ format.
Each answer should be 2-3 sentences maximum.
Use everyday language.
Content: [text]
Pattern 5: Self-Criticism
Ask Claude to evaluate its own work:
Generate a response. Then, critique it for accuracy, clarity, and completeness.
Provide the improved version.
Practical Exercise: Your First Engineered Prompt
Scenario
You need Claude to analyze customer feedback and categorize it as:
- Bug Report
- Feature Request
- Praise
- Complaint
- Other
Each piece of feedback should also receive a sentiment score (1-5).
Your Task
- Plan: Define success criteria
- Draft: Write your initial prompt
- Evaluate: Test with provided examples
- Refine: Make improvements based on results
- Document: Write down your key decisions