**Lecture 34: Multi-Agent & Biomedical Agents** **Total Time: 50 minutes** **Spectrum: Multi-Agent (N×)** **Code: subagent.py (commit 7f3abb7)** **Punchline: When 1 agent isn't enough, spawn N.** **[3 min] 0. Housekeeping** - Recap Lec 33: built complete single agent (5 files, ~1,200 lines) - Today: scale from 1 → N agents, then apply to biomedicine **[5 min] 1. WHAT: Multi-Agent = Agent That Spawns Agents** - Single agent: 1 conversation, 1 tool set, 1 context window - Multi-agent: parent spawns children with isolated conversations + specialized tools - On the spectrum: Multi-Agent (N×) — one step beyond multi-step **[10 min] 2. WHY: 3 Reasons N > 1** - **#1 Context fills up** — long tasks exhaust the 200K window. Sub-agent gets a fresh context for each subtask. - **#2 Specialization** — different tasks need different tools. Explore agent (read-only) vs general agent (all tools). Fewer tools = less confusion. - **#3 Parallelism** — independent subtasks run simultaneously. "Refactor module A" and "write tests for module B" in parallel. **[12 min] 3. HOW: subagent.py — 3 Steps** - **(a) Fork**: create isolated sub-agent with subset of tools + its own conversation ```python def fork_subagent(prompt, tools, model): state = AgentState() # fresh conversation system = build_system_prompt() # fresh context return run(prompt, state, config, system) ``` - **(b) Run**: execute agent loop in subprocess/thread, stream events - **(c) Collect**: gather result, return to parent as tool_result - **Agent types** (from Claude Code): | Type | Tools | Use Case | |------|-------|----------| | General | All | Default multi-step | | Explore | Read, Glob, Grep | Fast codebase search | | Plan | Read-only + output | Design before code | **[15 min] 4. APPLY: Biomedical Multi-Agent Workflows** - **The self-driving lab** (1 strong case study, not 6 scattered patterns): - Planner agent → generates hypothesis - Experiment agent → designs protocol, controls instruments - Analysis agent → processes data, runs statistics - Reasoning agent → interprets results, searches literature - Loop back to planner with findings - **Case study**: mitochondrial DNA mutations in Drosophila (agent_005.png) - Human scientist ↔ agents loop - Long-term memory: prior experiments inform next hypothesis - Literature search: PubMed API as a tool - **Agent taxonomy for biomedicine** (agent_003.png): - Perception (images, genomics, text) - Interaction (tool use, multi-agent, human-agent) - Reasoning (chain-of-thought, feedback) - Memory (long-term, short-term, learning) **[5 min] 5. Wrap-Up** - **3 takeaways**: context limits → fork, specialization → tool subsets, parallelism → concurrent agents - **Code**: subagent.py — fork, run, collect - **Next Lec 35**: Code Agent — what happens when you scale from 8 tools to 184? (claw-code)