Part 5: Prompt Engineering for Production Systems
Prompt Engineering Is Software Engineering
The Anatomy of a Production Prompt
βββββββββββββββββββββββββββββββββββ
β System Prompt β β Who the model is, rules, output format
βββββββββββββββββββββββββββββββββββ€
β Context β β Retrieved documents, user data, history
βββββββββββββββββββββββββββββββββββ€
β User Message β β The actual question or instruction
βββββββββββββββββββββββββββββββββββ# A complete prompt for my RAG service
SYSTEM_PROMPT = """You are a technical documentation assistant. You answer questions
based ONLY on the provided context. Follow these rules:
1. If the context contains the answer, provide it with specific details.
2. If the context does not contain the answer, say "I don't have information about this
in my knowledge base."
3. Never make up information that isn't in the context.
4. Reference the source document when possible.
5. Keep answers concise β aim for 2-4 paragraphs maximum.
Output format:
- Start with a direct answer to the question
- Follow with supporting details from the context
- End with source references if applicable"""
def build_rag_prompt(
question: str,
context_chunks: list[dict[str, str]],
) -> list[dict[str, str]]:
"""Build a complete prompt for RAG question-answering."""
# Format retrieved context
context_parts = []
for i, chunk in enumerate(context_chunks, 1):
context_parts.append(
f"[Source {i}: {chunk['title']}]\n{chunk['content']}"
)
context_text = "\n\n---\n\n".join(context_parts)
return [
{"role": "system", "content": SYSTEM_PROMPT},
{
"role": "user",
"content": f"Context:\n{context_text}\n\nQuestion: {question}",
},
]Why This Structure Matters
System Prompt Design
Pattern 1: Role + Rules + Format
Pattern 2: Explicit Boundaries
Prompt Templates with Pydantic
Why Pydantic for Prompts?
Structured Output β Getting JSON from LLMs
Approach 1: Prompt Instructions (Fragile)
Approach 2: JSON Mode (Better)
Approach 3: Pydantic Validation on Output (My Preferred Approach)
Defensive Prompting
Input Sanitization
Output Validation
Handling Edge Cases
Prompt Versioning
Key Takeaways
Last updated