AIMachine learining

Prompt Engineering Patterns That Actually Work

Stop guessing with your prompts. Learn the proven patterns that consistently improve LLM output: persona prompting, chain-of-thought, few-shot learning, and structured outputs. With practical code examples.

2 min read

I've spent the last year watching developers struggle with prompt engineering. The frustrating part? Most of the "tips" out there are either too basic or too theoretical. Let me share what actually moves the needle.

These aren't tricks — they're battle-tested patterns that consistently improve output quality by 50-80%.

1. The Persona Pattern: Context is Everything

Don't just tell the model what to do. Tell it who it is. This single change dramatically improves response quality:

Python
# Bad: Generic instruction
prompt = "Review this code for bugs."

# Good: Persona with context
prompt = """
You are a senior software engineer at a FAANG company with 15 years of 
experience in Python. You're known for catching subtle bugs that others miss.

Review this code with the same rigor you'd apply to a production deployment 
at scale. Focus on:
- Edge cases and error handling
- Performance implications
- Security vulnerabilities
- Code maintainability

Code to review:
{code}
"""

2. Chain-of-Thought: Make the Model Show Its Work

For complex reasoning tasks, explicitly ask for step-by-step thinking. This isn't just about getting explanations — it actually improves accuracy:

Python
# Without CoT - often wrong on complex problems
prompt = "What's 17 * 24 + 156 / 12?"

# With CoT - significantly more accurate
prompt = """
Solve this step by step, showing your work:

What's 17 * 24 + 156 / 12?

Think through each operation:
1. First, identify the order of operations
2. Calculate each part separately
3. Combine the results
4. Verify your answer
"""

For code generation, this becomes even more powerful:

Python
prompt = """
Before writing any code, think through:
1. What are the inputs and expected outputs?
2. What edge cases need handling?
3. What's the most efficient algorithm?
4. What could go wrong?

Then implement the solution.

Task: Write a function to find the longest palindromic substring.
"""

3. Few-Shot Learning: Show, Don't Tell

Examples are worth a thousand words of instruction. The key is choosing examples that demonstrate the pattern you want:

Python
prompt = """
Convert natural language to SQL queries.

Example 1:
Input: "Show me all users who signed up last month"
Output: SELECT * FROM users WHERE created_at >= DATE_SUB(NOW(), INTERVAL 1 MONTH)

Example 2:
Input: "Count orders by status"
Output: SELECT status, COUNT(*) as count FROM orders GROUP BY status

Example 3:
Input: "Find the top 5 customers by total spend"
Output: SELECT customer_id, SUM(amount) as total FROM orders GROUP BY customer_id ORDER BY total DESC LIMIT 5

Now convert:
Input: "{user_query}"
Output:"""

4. Output Formatting: Structure Your Responses

Vague output instructions lead to inconsistent results. Be explicit about format:

Python
from pydantic import BaseModel
from langchain_openai import ChatOpenAI

class CodeReview(BaseModel):
    summary: str
    bugs: list[str]
    improvements: list[str]
    security_issues: list[str]
    score: int  # 1-10

llm = ChatOpenAI(model="gpt-4-turbo-preview")
structured_llm = llm.with_structured_output(CodeReview)

result = structured_llm.invoke("""
Review this Python code and provide structured feedback:

def get_user(id):
    query = f"SELECT * FROM users WHERE id = {id}"
    return db.execute(query)
""")

print(f"Score: {result.score}/10")
print(f"Security Issues: {result.security_issues}")

5. The Negative Prompt: What NOT to Do

Sometimes telling the model what to avoid is more effective than telling it what to do:

Python
prompt = """
Write a technical blog post about Kubernetes.

DO NOT:
- Use buzzwords without explanation
- Assume the reader knows Docker
- Skip over error handling in code examples
- Use placeholder comments like "// add your code here"
- Write more than 1500 words

DO:
- Start with a real-world problem Kubernetes solves
- Include complete, runnable code examples
- Explain each concept before using it
- End with actionable next steps
"""

6. Temperature and Top-P: The Creativity Dial

These parameters matter more than most people realize:

Python
from langchain_openai import ChatOpenAI

# For factual, deterministic tasks (code, math, data extraction)
llm_precise = ChatOpenAI(
    model="gpt-4-turbo-preview",
    temperature=0,  # Deterministic
    top_p=1
)

# For creative tasks (writing, brainstorming)
llm_creative = ChatOpenAI(
    model="gpt-4-turbo-preview",
    temperature=0.8,  # More variety
    top_p=0.9
)

# For balanced tasks (explanations, summaries)
llm_balanced = ChatOpenAI(
    model="gpt-4-turbo-preview",
    temperature=0.3,
    top_p=0.95
)
"Well-crafted prompts improve output quality by 50-80% and reduce token costs by 30-50%."

Putting It All Together

Here's a template that combines all these patterns:

Python
MASTER_PROMPT = """
# Role
You are {persona}.

# Context
{background_context}

# Task
{specific_task}

# Examples
{few_shot_examples}

# Constraints
DO NOT: {negative_constraints}
DO: {positive_constraints}

# Output Format
{output_format_specification}

# Input
{user_input}
"""

Prompt engineering isn't magic — it's systematic. Start with these patterns, measure your results, and iterate. The difference between a mediocre prompt and a great one is often just a few well-chosen words.

Was this helpful?

Loading reactions...

Share this article:

Written by

Amanuel Garomsa

Machine Learning Engineer & Full Stack Developer. Writing about AI, software development, and technology.

More Articles

View all