# AI Agents Exercises

## Exercise 1: Write a Tool Schema

**Task:** Write a JSON schema for a tool that searches a vector database. Parameters: `query` (string), `top_k` (number, default 5), `filter` (optional object with `category`). Include a clear description the model can use to decide when to call it.

**Validation:**
- [ ] Has name, description, parameters
- [ ] Parameters match the spec (query, top_k, filter)
- [ ] Description explains when to use (e.g., "semantic search over indexed documents")

**Hints:**
1. Description: "Search the vector database for documents similar to the query."
2. `top_k`: integer, default 5
3. `filter`: optional, e.g., `{ "category": "docs" }`

---

## Exercise 2: Implement the Agent Loop (Pseudocode)

**Task:** Write pseudocode for the main agent loop: (1) Receive user message, (2) Call LLM with tools, (3) If LLM returns tool call, execute and append result to messages, (4) Repeat until LLM returns final answer. Include a max-iterations guard.

**Validation:**
- [ ] Loop: get LLM response → if tool call, execute → add result → repeat
- [ ] Max iterations to prevent infinite loops
- [ ] Stops when LLM returns text (no tool call)

**Hints:**
1. `while True` with `max_steps` counter
2. Check response type: `tool_calls` vs `content`
3. Append `{"role": "tool", "content": result}` and loop

---

## Exercise 3: Plan for Failure Modes

**Task:** List 5 failure modes for an agent that uses web search + summarization. For each, propose a mitigation (retry, fallback, user message, etc.).

**Validation:**
- [ ] Covers: no results, timeout, malformed response, rate limit, irrelevant results
- [ ] Each has a concrete mitigation

**Hints:**
1. No results → retry with different query or tell user
2. Timeout → retry once, then fail gracefully
3. Malformed → validate schema; ask model to fix or skip
4. Rate limit → backoff, queue, or inform user
5. Irrelevant → re-rank, filter, or prompt for refinement

---

## Exercise 4: Design Human-in-the-Loop

**Task:** Your agent can create calendar events. Design a human-in-the-loop flow: when does the agent pause? What does the user see? What can they do (approve, reject, edit)? Draw or describe the flow.

**Validation:**
- [ ] Agent pauses before creating (or similar destructive action)
- [ ] User sees proposed action (e.g., event details)
- [ ] User can approve, reject, or edit
- [ ] Describes what happens after each choice

**Hints:**
1. Agent proposes → "Create event: X at Y. Approve? [Y/N/Edit]"
2. Edit → agent updates proposal
3. Approve → agent executes
4. Reject → agent stops or suggests alternative

---

## Exercise 5: Compare Agent Architectures

**Task:** Compare a single-agent design vs. a multi-agent design for "Answer customer support questions using our docs." For each, list: (a) tools/agents, (b) pros, (c) cons, (d) when you'd choose it.

**Validation:**
- [ ] Single: one agent + search + docs tools; simpler, may be slower
- [ ] Multi: orchestrator + retrieval agent + answer agent; more modular, more complex
- [ ] Clear recommendation with justification

**Hints:**
1. Single: one agent does search + read + answer
2. Multi: retrieval agent finds docs, answer agent synthesizes
3. Choose single for simplicity; multi for scale or specialization
