Skip to main content

Prompt Engineering

Prompt engineering is writing instructions that get AI models to do what you actually want. Sounds simple. It's not.

What is prompt engineering?

You're designing the input that guides the model's output. Good prompts reduce hallucinations by forcing explicit reasoning. They improve accuracy by giving context and examples. They control format through structure. They enable complex reasoning by breaking problems down strategically.

Your prompts directly affect what you get back. A well-crafted prompt makes the difference between mediocre and exceptional results.


Fundamentals

Prompt structure

Good prompts have four parts:

Identity or role defines what the AI is:

You are an expert code reviewer focusing on security and performance.

Instructions tell it what to do:

Review this code for:
1. Security vulnerabilities
2. Performance bottlenecks
3. Code quality issues

Context gives background:

This is a payment processing module handling sensitive customer data.

Examples show the pattern (few-shot learning):

Input: def add(a, b): return a + b
Output: ✓ Simple, clear function. Consider adding type hints.

Output format (optional) specifies structure:

Return your review as JSON:
{
"security_issues": [],
"performance_issues": [],
"suggestions": []
}

Zero-shot vs few-shot

ApproachDescriptionWhen to use
Zero-shotNo examples, just instructionSimple tasks where the model already knows the pattern
Few-shotInclude 2-5 examplesComplex patterns or specific output formats

Zero-shot example:

Translate to French: "Hello, how are you?"

Few-shot example:

Translate to French:
"Good morning" → "Bonjour"
"Thank you" → "Merci"
"Hello, how are you?" →
tip

For complex tasks that require structured thinking, see Reasoning Strategies.


Best practices

Be specific

❌ "Make this code better"

✓ "Refactor this code to improve performance, add error handling, and include type hints"

Give context

❌ "Fix this bug"

✓ "This authentication function fails when the token expires. Fix the bug and add proper error messages."

Use delimiters

XML tags, Markdown, or clear separators help:

## Task
Analyze the following code

<code>
def process_data(data):
return [x * 2 for x in data]
</code>

## Requirements
- Add error handling
- Support empty lists
- Add docstrings

Iterate

Start simple, test, add complexity:

  1. Basic instruction
  2. Add examples
  3. Include constraints
  4. Specify output format
  5. Add reasoning strategy

Cache effectively

Put reusable content first to leverage caching:

[REUSABLE: System instructions, guidelines, examples]
[DYNAMIC: Specific user query]

Common pitfalls

ProblemSolution
Vague instructionsBe explicit about requirements and constraints
No examplesProvide 2-3 examples for complex patterns
Missing contextInclude relevant background information
Ambiguous output formatSpecify exact format (JSON, Markdown, etc.)
Overloading single promptBreak complex tasks into sequential prompts

Resources