Overview

Prompt engineering is the art of crafting inputs that get the best outputs from LLMs. Small changes in prompts can dramatically improve results.

Basic Techniques

Zero-Shot Prompting

Classify the sentiment of this review as positive, negative, or neutral:
"The product arrived late but works great."

Sentiment:

Few-Shot Prompting

Classify the sentiment:

Review: "Amazing quality, fast shipping!" → Positive
Review: "Broken on arrival, terrible." → Negative
Review: "It's okay, nothing special." → Neutral
Review: "The product arrived late but works great." →

Chain-of-Thought (CoT)

Q: A store has 23 apples. They sell 8 and receive 15 more. How many apples?

Let me think step by step:
1. Start with 23 apples
2. Sell 8: 23 - 8 = 15 apples
3. Receive 15: 15 + 15 = 30 apples

Answer: 30 apples

Role Prompting

You are an expert Python developer with 15 years of experience.
Review this code for bugs, performance issues, and best practices:

```python
def process(data):
    result = []
    for i in range(len(data)):
        result.append(data[i] * 2)
    return result

## Structured Output

Extract information from this text and return as JSON:

Text: “John Smith, CEO of TechCorp, announced a $50M funding round on March 15, 2024.”

Return format: { “person”: “”, “title”: “”, “company”: “”, “amount”: “”, “date”: "" }


## Self-Consistency

Ask the same question multiple times with temperature > 0, then take the majority answer. Improves accuracy on reasoning tasks.

## ReAct Pattern

Question: What is the population of the capital of France?

Thought: I need to find the capital of France first. Action: Search[capital of France] Observation: Paris is the capital of France.

Thought: Now I need to find Paris’s population. Action: Search[population of Paris] Observation: Paris has a population of about 2.1 million.

Answer: The population of Paris, the capital of France, is approximately 2.1 million.


## Best Practices

1. **Be specific**: "Write a 200-word summary" vs "Summarize this"
2. **Use delimiters**: Separate instructions from content with `---` or `###`
3. **Specify format**: JSON, markdown, bullet points
4. **Give examples**: Few-shot beats zero-shot for complex tasks
5. **Iterate**: Test and refine prompts systematically

## Key Resources

- [OpenAI Prompt Engineering Guide](https://platform.openai.com/docs/guides/prompt-engineering)
- [Anthropic Prompt Library](https://docs.anthropic.com/claude/prompt-library)