Prompt Design Fundamentals
1. Overview of Prompt Engineering
Prompt engineering is a systematic approach to designing and optimizing input text to guide large language models (LLMs) toward desired outputs. Good prompt design can significantly improve model performance without modifying model parameters.
1.1 Why Prompt Engineering Matters
- Zero-cost optimization: Improve outputs by modifying inputs alone, no training or fine-tuning needed
- Rapid iteration: Second-level feedback for quick idea validation
- Universality: Applicable to all LLMs, skills are transferable
- First line of defense: Exhaust prompt optimization before considering fine-tuning
1.2 Basic Prompt Structure
[System Prompt] ← Role definition, behavioral constraints
[Context/Examples] ← Background information, examples
[User Input] ← Actual user request
[Output Format] ← Expected output format
2. Foundational Prompt Patterns
2.1 Zero-Shot Prompting
Provide only a task description without any examples:
Classify the following text as "positive", "negative", or "neutral":
"The food at this restaurant was great, but the service was very slow."
Applicable scenarios:
- Task description is clear and unambiguous
- The model has sufficient pre-training knowledge for the task
- Simple classification, translation, summarization, and other common tasks
Limitations:
- Complex tasks may yield unstable outputs
- Cannot convey nuanced judgment criteria through examples
2.2 Few-Shot Prompting
Provide a small number of examples to guide the model in understanding the task pattern:
Classify the following texts by sentiment.
Text: "What a beautiful day!" → Positive
Text: "This product is completely unusable." → Negative
Text: "The meeting has been moved to 3 PM." → Neutral
Text: "Although it's a bit expensive, the quality is really good." →
Best practices:
- Number of examples: 3-5 is typically sufficient
- Example diversity: Cover different categories and edge cases
- Example order: Place the most relevant examples last (recency effect)
- Format consistency: Keep all examples in the same format
Caveats:
- Avoid bias in examples (e.g., uneven class distribution)
- Example quality matters more than quantity
- Too many examples may exceed the context window
2.3 Chain-of-Thought (CoT) Prompting
Guide the model to show its reasoning process, arriving at the answer step by step:
Q: A store has 32 apples. If each box holds 8 apples, how many boxes are needed?
Let's think step by step.
A: Let me solve this step by step:
1. The store has 32 apples in total
2. Each box can hold 8 apples
3. Number of boxes needed = total apples ÷ apples per box
4. 32 ÷ 8 = 4
So 4 boxes are needed.
Trigger methods:
- Simple trigger: "Let's think step by step"
- Structured trigger: "Please analyze following these steps: 1. Understand the problem 2. Analyze conditions 3. Derive the answer"
- Few-shot CoT: Provide examples that include reasoning processes
Applicable scenarios:
- Mathematical reasoning
- Logical reasoning
- Multi-step problem analysis
- When the decision process needs to be explained
3. System Prompt Design
3.1 Role Definition
You are a senior Python developer specializing in code review and best practices.
Your responses should:
- Be concise and accurate
- Include code examples
- Point out potential issues and improvement suggestions
- Follow PEP 8 conventions
3.2 Behavioral Constraints
Rules:
1. Only answer programming-related questions
2. If uncertain, state so explicitly rather than guessing
3. Do not generate harmful or unsafe code
4. Respond in English
3.3 System Prompt Best Practices
- Define role clearly: Specify expert identity and domain of expertise
- Set boundaries: Clarify what to do and what not to do
- Specify format: Define output style and structure
- Prioritize: When rules conflict, clarify priority order
- Keep it concise: Avoid overly long system prompts
4. Template Engineering
4.1 Template Design Principles
# Python example: Using template engines
from string import Template
prompt_template = Template("""
You are a $role expert.
Background information:
$context
User question:
$question
Please respond in the following format:
1. Brief answer
2. Detailed explanation
3. Example (if applicable)
""")
prompt = prompt_template.substitute(
role="machine learning",
context="The user is learning deep learning fundamentals",
question="What is backpropagation?"
)
4.2 Variable Management
# Using Jinja2 templates
from jinja2 import Template
template = Template("""
Analyze the following {{ items | length }} data points:
{% for item in items %}
- {{ item.name }}: {{ item.value }}
{% endfor %}
Please provide:
{% if include_summary %}
1. Summary
{% endif %}
2. Key findings
3. Recommended actions
""")
4.3 Template Versioning
- Use Git to manage prompt templates
- Record the reason and effect of each modification
- Associate evaluation results with version numbers
- Support A/B testing of different versions
5. Output Format Control
5.1 JSON Mode
Please output the analysis results in JSON format:
{
"sentiment": "positive/negative/neutral",
"confidence": 0.0-1.0,
"keywords": ["keyword1", "keyword2"],
"summary": "One-sentence summary"
}
Note: Output only JSON, do not add any other text.
Tips:
- Provide a complete JSON schema example
- Specify field types and value ranges explicitly
- Use constraining instructions like "output only JSON"
- Leverage API JSON mode (e.g., OpenAI's
response_format)
5.2 Structured Output
Please output in the following format:
## Title
[One-line brief title]
## Key Findings
- Finding 1
- Finding 2
- Finding 3
## Detailed Analysis
[2-3 paragraphs of analysis]
## Recommendations
1. Recommendation 1
2. Recommendation 2
5.3 Table Output
Please compare the following frameworks in Markdown table format:
| Framework | Strengths | Weaknesses | Use Cases |
|-----------|-----------|------------|-----------|
6. Temperature and Sampling Parameters
6.1 Temperature
Controls the randomness of output:
| Temperature | Effect | Use Cases |
|---|---|---|
| 0.0 | Most deterministic, always selects most likely token | Classification, extraction, math |
| 0.3-0.5 | Low randomness, slight variation | Summarization, translation |
| 0.7-0.9 | Medium randomness | Creative writing, conversation |
| 1.0+ | High randomness | Brainstorming, poetry |
6.2 Top-p (Nucleus Sampling)
top_p = 0.9 # Sample from token pool with 90% cumulative probability
- top_p = 0.1: Consider only the most probable few tokens → more deterministic
- top_p = 0.9: Consider most tokens → more diverse
- Typically adjust either temperature or top_p, not both simultaneously
6.3 Other Parameters
- max_tokens: Controls maximum output length
- frequency_penalty: Reduces repetition (0-2)
- presence_penalty: Encourages topic diversity (0-2)
- stop sequences: Defines output termination conditions
7. Prompt Examples for Different Tasks
7.1 Text Classification
System: You are a text classification expert.
Classify the user's text into one of the following categories:
- Technical Issue
- Product Feedback
- Account Issue
- General Inquiry
Output only the category name, do not add any explanation.
User: I forgot my password. How do I reset it?
7.2 Information Extraction
Extract all mentioned entities from the following text, output in JSON format:
Text: "{text}"
Output format:
{
"people": ["list of names"],
"organizations": ["list of organizations"],
"locations": ["list of locations"],
"dates": ["list of dates"]
}
7.3 Code Generation
System: You are a senior Python developer. Generated code should:
- Include type annotations
- Include docstrings
- Handle edge cases
- Follow PEP 8
Write code for the following requirement:
{requirement}
Output format:
1. Code implementation
2. Usage example
3. Important notes
7.4 Summary Generation
Generate a summary of the following article:
Article:
{article}
Requirements:
- Length: 100-150 words
- Preserve key data and conclusions
- Use objective third person
- Do not add information not in the original text
8. Best Practices Summary
8.1 Design Principles
- Clarity: More specific instructions lead to more controllable outputs
- Step-by-step: Break complex tasks into multiple steps
- Give examples: Examples are more intuitive than descriptions
- Set constraints: Specify output format, length, and style explicitly
- Iterate: Prompt design is an iterative process
8.2 Common Pitfalls
- Too vague: "Write something about AI" → uncontrollable output
- Too verbose: Excessive instructions cause the model to ignore parts
- Contradictory instructions: Conflicts between different constraints
- Lack of format: Not specifying output format leads to inconsistency
- Ignoring edge cases: Not considering abnormal input scenarios
8.3 Debugging Tips
- Add/remove instructions incrementally, observe impact
- Use multiple test cases to verify stability
- Record each modification and its effects
- Compare outputs across different temperature settings
- Use evaluation frameworks to quantify prompt quality
References
- OpenAI Prompt Engineering Guide
- Anthropic Prompt Engineering Guide
- Advanced Prompt Techniques — More advanced prompt optimization methods
- Prompt Security — Prompt security and defense