# AI Pair Programming Walkthrough — Learn by Doing

## Before We Begin

**Diagnostic Question:** What AI coding tools have you used (Cursor, Copilot, ChatGPT, etc.)? For which tasks did they help most — and for which did they fall short?

**Checkpoint:** You have a sense of the AI tools landscape. You're ready to think about when AI helps vs when human judgment is essential.

---

## Step 1: Identify the Right Tool

Not every task is best for AI.

<!-- hint:buttons type="multi" prompt="Which tasks suit AI assistance?" options="Repetitive code,Architecture decisions,Debugging,Documentation" -->

**Task:** For each scenario, decide: AI, Manual, or Both (AI drafts, you refine)?

1. Add a new REST endpoint that mirrors an existing one
2. Debug a race condition in your WebSocket handling
3. Write a README for your project
4. Choose between Redis vs Memcached for your caching layer

**Question:** What made you choose AI vs manual for each? What kind of context does the AI have (or lack) in each case?

**Checkpoint:** The user should recognize that repetitive/structured tasks (1, 3) suit AI; subtle debugging (2) and architecture (4) need human judgment. Both can mean AI drafts, human reviews.

---

## Step 2: Write a Specific Prompt

<!-- hint:list style="cards" -->

Vague prompts lead to generic output.

**Task:** You want to add input validation to a signup form. Your form has `email` and `password` fields. Write a prompt that gives the AI enough context to produce useful code.

Include: (a) what to validate, (b) any existing patterns (e.g., "we use `zod`"), (c) where the form lives (file/component).

**Question:** What would happen if you only said "add validation"? What extra information did you add and why?

**Checkpoint:** The user's prompt includes at least: fields to validate, validation rules (format, length), and where to put the code. They can explain why each piece matters.

---

## Step 3: Provide Examples

Show, don't just tell.

**Task:** Your codebase uses a custom `useApi` hook. Write a 2–3 sentence prompt that shows the pattern so the AI can add a new API call correctly.

Example hook usage: `const { data, loading, error } = useApi('/users', { method: 'GET' });`

**Question:** Why might "use our useApi hook" fail? What does the example give the AI that words alone don't?

**Checkpoint:** The user includes a concrete example of the hook in use. They understand that examples disambiguate style and reduce wrong guesses.

---

## Step 4: Iterate with Narrow Feedback

<!-- hint:card type="tip" title="Narrow feedback" -->

Practice giving feedback that helps the AI correct course.

**Task:** The AI produced a function that uses `var` and a `for` loop. You want `const` and `.map()`. Write the feedback you'd give — without rewriting the whole function yourself.

**Question:** What's the difference between "use modern JS" and "replace the for loop with .map() and use const instead of var"? Which gets better results?

**Checkpoint:** The user's feedback is specific (naming the constructs to change) and doesn't rewrite the code. They understand that narrow feedback is more actionable.

---

## Step 5: Review AI Output

Treat AI output as a draft to verify.

<!-- hint:code language="javascript" highlight="1,4" -->

**Task:** The AI suggested this code:

```javascript
async function fetchUser(id) {
  const res = await fetch(`/api/users/${id}`);
  return res.json();
}
```

List at least 3 things you'd check before merging: correctness, safety, edge cases.

**Question:** What could go wrong in production with this code? What would you add or change?

**Checkpoint:** The user identifies: no error handling, no check for non-OK status, possible JSON parse errors, no loading/empty states. They propose at least one concrete fix (e.g., check `res.ok`).

---

## Step 6: Use Skills and MCP

Leverage structured capabilities.

**Task:** Look up what skills or MCP tools are available in your setup (e.g., spec-writer, argus, code search). Pick one and describe a task where invoking it would help.

**Question:** When would you use a skill vs just asking in natural language? What does the skill give you that a freeform prompt doesn't?

**Checkpoint:** The user can name at least one skill or MCP tool and explain when it's useful (e.g., spec-writer after a feature change, argus for testing). They understand skills encode procedures.

---

## Step 7: Decide When to Take Over

Know when AI isn't the right tool.

**Task:** You've asked the AI to fix a bug three times. Each fix either doesn't work or introduces a new bug. What do you do next?

**Question:** At what point does iterating with the AI cost more than debugging yourself? What signals tell you to take over?

**Checkpoint:** The user proposes: gather more context (logs, repro steps), try a narrower prompt, or take over and fix manually. They recognize diminishing returns from repeated AI attempts.
