Prompt Engineering Patterns
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
| from pydantic import BaseModel
from openai import OpenAI
# Structured Output with Validation
class DocumentAnalysis(BaseModel):
summary: str
key_entities: list[str]
sentiment: str
confidence: float
citations: list[dict]
ANALYSIS_PROMPT = """
Analyze the following document and extract structured information.
<document>
{document_text}
</document>
Think through this step by step:
1. First, identify the main topic and purpose
2. Extract key entities (people, organizations, dates)
3. Determine overall sentiment
4. Note any citations or references
Respond with valid JSON matching this schema:
{schema}
Be precise and include confidence scores.
"""
def analyze_document(text: str) -> DocumentAnalysis:
response = client.chat.completions.create(
model="gpt-4-turbo",
messages=[{
"role": "user",
"content": ANALYSIS_PROMPT.format(
document_text=text,
schema=DocumentAnalysis.model_json_schema()
)
}],
response_format={"type": "json_object"}
)
return DocumentAnalysis.model_validate_json(response.choices[0].message.content)
|
Prompt Optimization Checklist
| Technique | Impact | When to Use |
|---|
| Be specific | Quality โ | Always |
| Show examples | Consistency โ | Complex formats |
| Chain of thought | Accuracy โ | Reasoning tasks |
| Output schema | Reliability โ | Data extraction |
| Temperature tuning | Control โ | Balance creativity/consistency |
| Token reduction | Cost โ | High-volume applications |
Frequently Asked Questions
What is prompt engineering?
Prompt engineering is the practice of crafting effective prompts to get desired outputs from LLMs. It includes: prompt design, few-shot examples, chain-of-thought techniques, output formatting, and iterative refinement for specific use cases.
How much does prompt engineering cost?
Prompt engineering typically costs $100-160 per hour. A prompt optimization project starts around $5,000-10,000, while thorough prompt development for production applications ranges from $15,000-40,000+.
Is prompt engineering a real skill?
Yes. Effective prompts can be the difference between a useful application and a unreliable one. Prompt engineering includes: understanding model capabilities, structured output techniques, handling edge cases, and balancing quality with cost.
What makes a good prompt?
Good prompts are: clear and specific, provide relevant context, include examples when helpful, specify output format, and handle edge cases. I also consider: token efficiency, robustness to variations, and testability.
Can you optimize my existing prompts?
Yes. I analyze existing prompts for: clarity, token efficiency, output consistency, and edge case handling. Common improvements: adding structured output, including negative examples, and reducing ambiguity. I’ve improved prompt performance 30-50% through optimization.
Experience:
Case Studies: LLM Email Assistant | Multi-LLM Orchestration | Enterprise RAG System
Related Technologies: OpenAI, Anthropic Claude, LangChain, RAG Systems