Prompt Engineering Fundamentals
Prompt engineering is the art and science of crafting inputs that guide LLMs to produce desired outputs. It's the most important skill for working with Generative AI.
Why Prompt Engineering Matters
The same model can give vastly different results depending on how you ask:
❌ Bad prompt: "Tell me about Python"
→ Vague, unfocused 500-word essay about Python (the language? the snake?)
✅ Good prompt: "Explain 3 key advantages of Python for data science,
with a code example for each. Target audience: developers
who know JavaScript but not Python."
→ Focused, relevant, actionable responseThe Anatomy of a Good Prompt
Every effective prompt has some combination of these elements:
1. Role / Context
Tell the model who it should be:
"You are a senior Python developer with 10 years of experience
in building REST APIs."2. Task / Instruction
State clearly what you want:
"Write a function that validates email addresses using regex."3. Specifics / Constraints
Add details that shape the output:
"Use Python 3.12 syntax. Include type hints. Handle edge cases
like empty strings and missing @ symbols."4. Output Format
Describe how you want the response structured:
"Return the result as a JSON object with keys: 'valid' (boolean)
and 'reason' (string explaining why it's invalid, or null if valid)."5. Examples (Optional)
Show what good output looks like:
"Example input: 'user@example.com' → {'valid': true, 'reason': null}
Example input: 'not-an-email' → {'valid': false, 'reason': 'Missing @ symbol'}"The CRAFT Framework
A simple mnemonic for writing better prompts:
| Letter | Meaning | Example |
|---|---|---|
| C | Context | "You are a marketing expert for SaaS products" |
| R | Role | "Act as a technical writer" |
| A | Action | "Write a blog post about..." |
| F | Format | "Use bullet points, keep it under 300 words" |
| T | Tone | "Professional but approachable" |
Basic Prompting Techniques
Direct Instruction
The simplest approach — just tell the model what to do:
"Summarize the following article in 3 bullet points:"Question-Based
Ask a specific question:
"What are the pros and cons of microservices vs monolithic architecture
for a startup with 5 developers?"Completion-Based
Start a sentence and let the model finish:
"The three main benefits of TypeScript over JavaScript are:"Step-by-Step
Ask the model to break down its reasoning:
"Explain how to set up a CI/CD pipeline for a Next.js app.
Walk me through each step."Formatting Prompts
Using Delimiters
Separate instructions from content with clear markers:
Summarize the text between the triple backticks:
\`\`\`
[Your long text here]
\`\`\`Common delimiters: triple backticks, XML tags, dashes, quotes.
Using XML Tags
Particularly effective with Claude:
<context>
You are reviewing a pull request for a Python web application.
</context>
<code>
def get_user(id):
return db.query(f"SELECT * FROM users WHERE id = {id}")
</code>
<task>
Review this code for security issues and suggest improvements.
</task>Structured Output Requests
"Analyze the following code and respond in this exact format:
## Issues Found
- [List each issue]
## Severity
- [Rate: Low / Medium / High / Critical]
## Suggested Fix
- [Provide corrected code]"Common Mistakes to Avoid
1. Being Too Vague
❌ "Make this better"
✅ "Improve the readability of this function by:
- Adding descriptive variable names
- Breaking long lines into shorter ones
- Adding a docstring explaining parameters and return value"2. Overloading with Instructions
❌ "Write a blog post about AI that's funny but serious, short but
comprehensive, technical but accessible, and includes 10 examples
but keep it under 200 words"
✅ "Write a 300-word blog post about AI for non-technical readers.
Use a conversational tone. Include 2-3 real-world examples."3. Not Specifying the Audience
❌ "Explain Kubernetes"
✅ "Explain Kubernetes to a frontend developer who has never used
containers. Use analogies to web development concepts they know."4. Ignoring the Output Format
❌ "Give me some API endpoints"
✅ "List 5 REST API endpoints for a todo app in this format:
METHOD /path - Description
Example: GET /todos - List all todos"Iterative Prompt Refinement
Prompt engineering is iterative. Start simple, then refine:
- Draft: Write your initial prompt
- Test: Run it and examine the output
- Identify gaps: What's missing or wrong?
- Refine: Add constraints, examples, or clarity
- Repeat: Continue until you get consistent, quality results
Iteration 1: "Write a function to sort users"
Iteration 2: "Write a Python function to sort a list of User objects
by last_name, then first_name"
Iteration 3: "Write a Python function to sort a list of User objects
by last_name (ascending), then first_name (ascending).
Handle None values by placing them last. Include type hints.
Users have: first_name: str, last_name: str, email: str"Practice Exercise
Try these prompts with any LLM and compare results:
- Write a prompt that generates a Python function to check if a string is a palindrome
- Write a prompt that explains recursion using a real-world analogy
- Write a prompt that creates a SQL query to find the top 5 customers by revenue
Focus on using the elements covered: role, task, specifics, format, and examples.
Mastering these fundamentals is your foundation. The next topic covers advanced techniques that build on these basics.