# Prompt Engineering — Techniques That Get Better AI Outputs

<!-- hint:slides topic="Prompt engineering: prompt anatomy, zero-shot vs few-shot, chain-of-thought, structured output, and system prompts" slides="5" -->

## What Makes a Good Prompt?

Prompts are instructions you give to an LLM. A good prompt is **specific**, **structured**, and **contextual** — it tells the model exactly what you want, in what format, with enough context to succeed.

## Anatomy of a Prompt

Every effective prompt can be broken into five components:

```mermaid
flowchart TB
    subgraph prompt["Prompt Anatomy"]
        R[Role]
        C[Context]
        T[Task]
        F[Format]
        X[Constraints]
    end
    R --> C --> T --> F --> X
    X --> Out[Better Output]
```

| Component | Purpose | Example |
|-----------|---------|---------|
| **Role** | Sets the AI's persona and expertise | "You are an expert Python developer." |
| **Context** | Background the model needs | "The codebase uses FastAPI and Pydantic." |
| **Task** | The specific request | "Add validation for the email field." |
| **Format** | Desired output structure | "Return a JSON object with keys: valid, errors." |
| **Constraints** | Limits and rules | "Keep under 100 words. No external APIs." |

## Zero-Shot vs Few-Shot Prompting

**Zero-shot** — No examples. The model infers from the instruction alone.

```
Translate "Hello" to Spanish.
```

**Few-shot** — Provide 1–5 examples to establish the pattern.

```
Translate to Spanish:
Hello → Hola
Goodbye → Adiós
Please → Por favor
Thank you → ?
```

Few-shot dramatically improves accuracy for structured, style-sensitive, or edge-case tasks. The model learns the pattern from examples.

## Chain-of-Thought Reasoning

Ask the model to "think step by step" or "show your reasoning." This improves performance on logic, math, and multi-step tasks.

```
What is 17 × 24?

Think through each step before giving the final answer.
```

CoT elicits intermediate reasoning, reducing errors and making output verifiable.

## System Prompts

Many APIs support a **system prompt** — instructions that apply across the conversation, separate from user messages. Use it for:

- Role and persona
- Output format preferences
- Guardrails and constraints
- Domain knowledge

## Structured Output

Request specific formats to make output machine-readable:

- **JSON** — `Return a JSON object with: name, email, score`
- **XML** — `Wrap the summary in <summary> tags`
- **Markdown** — `Format as a markdown table with columns A, B, C`

## Iterating on Prompts

1. **Start simple** — one clear task
2. **Add context** — if output is generic or wrong
3. **Add examples** — if the format or style drifts
4. **Add constraints** — if output is too long or off-topic
5. **Test edge cases** — ambiguous inputs, empty inputs

## Temperature and Creativity

- **Low (0–0.3)** — Deterministic, factual, consistent. Good for code, extraction, classification.
- **High (0.7–1.0)** — Creative, varied. Good for brainstorming, stories, varied phrasing.

## Common Mistakes

| Mistake | Fix |
|---------|-----|
| Vague instructions | Be explicit: "List 3 pros and 3 cons" |
| No examples | Add 1–2 few-shot examples for format/style |
| Too many constraints at once | Add constraints incrementally |
| Assuming the model knows your context | Provide relevant background |
| One giant prompt | Break into steps or sub-tasks |

## Prompting by Task Type

| Task | Strategy |
|------|----------|
| **Summarization** | Specify length, key points, audience |
| **Analysis** | Define criteria, structure (pros/cons, compare/contrast) |
| **Code** | Language, framework, patterns, input/output format |
| **Creative writing** | Tone, length, style, examples of desired voice |

---

## Key Takeaways

1. **Structure your prompts** — role, context, task, format, constraints
2. **Few-shot beats zero-shot** for structured or style-sensitive tasks
3. **Chain-of-thought** helps with reasoning and multi-step problems
4. **Iterate** — start simple, add context and examples as needed
5. **Avoid** vague instructions, no examples, and constraint overload
