agentic-ai-engineering/site/.vitepress/dist/assets/modules_m2-architecture.md....

101 lines
42 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import{c as a,Q as e,j as t,m as i}from"./chunks/framework.BPKcPtvA.js";const k=JSON.parse('{"title":"Module 2: Agent Architecture","description":"","frontmatter":{},"headers":[],"relativePath":"modules/m2-architecture.md","filePath":"modules/m2-architecture.md","lastUpdated":1780492476000}'),n={name:"modules/m2-architecture.md"};function l(o,s,r,p,h,d){return e(),t("div",null,[...s[0]||(s[0]=[i(`<h1 id="module-2-agent-architecture" tabindex="-1">Module 2: Agent Architecture <a class="header-anchor" href="#module-2-agent-architecture" aria-label="Permalink to &quot;Module 2: Agent Architecture&quot;"></a></h1><h2 id="lesson-2-1-the-four-pillars" tabindex="-1">Lesson 2.1: The Four Pillars <a class="header-anchor" href="#lesson-2-1-the-four-pillars" aria-label="Permalink to &quot;Lesson 2.1: The Four Pillars&quot;"></a></h2><p>Every agent system is built on four pillars:</p><div class="language- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>┌─────────────────────────────────────────────────────────────┐</span></span>
<span class="line"><span>│ AGENT SYSTEM │</span></span>
<span class="line"><span>├──────────────┬──────────────┬──────────────┬────────────────┤</span></span>
<span class="line"><span>│ TOOLS │ LOOP │ CONTEXT │ MEMORY │</span></span>
<span class="line"><span>│ Capabilities │ Autonomy │ Awareness │ Persistence │</span></span>
<span class="line"><span>│ │ │ │ │</span></span>
<span class="line"><span>│ • Read/Write │ • Think→Act │ • System │ • Mental │</span></span>
<span class="line"><span>│ • Bash │ →Obs→Repeat │ prompt │ models │</span></span>
<span class="line"><span>│ • Search │ • Iteration │ • Messages │ • Expertise │</span></span>
<span class="line"><span>│ • API calls │ limits │ • Tool │ files │</span></span>
<span class="line"><span>│ • MCP │ • Terminate │ results │ • Scratch │</span></span>
<span class="line"><span>│ │ conditions │ • Window │ pads │</span></span>
<span class="line"><span>│ │ │ mgmt │ • Session logs │</span></span>
<span class="line"><span>└──────────────┴──────────────┴──────────────┴────────────────┘</span></span></code></pre></div><ul><li><strong>Tools</strong> define what the agent CAN do</li><li><strong>Loop</strong> defines WHEN the agent does it</li><li><strong>Context</strong> defines WHAT the agent knows right now</li><li><strong>Memory</strong> defines WHAT the agent remembers across sessions</li></ul><hr><h2 id="lesson-2-2-tool-design-patterns" tabindex="-1">Lesson 2.2: Tool Design Patterns <a class="header-anchor" href="#lesson-2-2-tool-design-patterns" aria-label="Permalink to &quot;Lesson 2.2: Tool Design Patterns&quot;"></a></h2><h3 id="tool-distribution-channels-from-beyond-mcp-research" tabindex="-1">Tool Distribution Channels (from Beyond MCP research) <a class="header-anchor" href="#tool-distribution-channels-from-beyond-mcp-research" aria-label="Permalink to &quot;Tool Distribution Channels (from Beyond MCP research)&quot;"></a></h3><table tabindex="0"><thead><tr><th>Channel</th><th>Context Cost</th><th>Portability</th><th>Auto-Discovery</th><th>Best For</th></tr></thead><tbody><tr><td><strong>MCP Server</strong></td><td>High (full context per call)</td><td>High</td><td>Yes (MCP protocol)</td><td>Multi-client, standardized tools</td></tr><tr><td><strong>CLI</strong></td><td>Medium</td><td>High</td><td>No</td><td>80% of new tools, direct control</td></tr><tr><td><strong>File System Scripts</strong></td><td>Low (progressive disclosure)</td><td>Medium</td><td>No</td><td>Context-sensitive, portable</td></tr><tr><td><strong>Skills</strong></td><td>Low</td><td>Medium</td><td>Yes (skill dir)</td><td>Agent-native, behavior rules</td></tr></tbody></table><h3 id="tool-design-rules" tabindex="-1">Tool Design Rules <a class="header-anchor" href="#tool-design-rules" aria-label="Permalink to &quot;Tool Design Rules&quot;"></a></h3><ol><li><strong>Single responsibility</strong>: One tool = one capability. Don&#39;t make a Swiss Army knife tool.</li><li><strong>Descriptive names</strong>: <code>search_web</code> not <code>sw</code>. <code>execute_sql_query</code> not <code>run</code>.</li><li><strong>Rich descriptions</strong>: Tell the LLM WHEN to use each tool and WHAT it returns.</li><li><strong>Parameter validation</strong>: Schema-enforce types, required fields, and constraints.</li><li><strong>Output limits</strong>: Cap returns (2KB for logs, 10 results for search) to avoid context overflow.</li></ol><h3 id="advanced-resource-mirror-pattern" tabindex="-1">Advanced: Resource Mirror Pattern <a class="header-anchor" href="#advanced-resource-mirror-pattern" aria-label="Permalink to &quot;Advanced: Resource Mirror Pattern&quot;"></a></h3><p>Some MCP clients don&#39;t support Resources. The fix: every Resource gets a mirror Tool that returns identical data:</p><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Resource: datasets://loaded → may not work in all clients</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># Mirror tool:</span></span>
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">@tool</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> list_loaded_datasets</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() -&gt; </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">str</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;&quot;&quot;List all currently loaded datasets. Returns names and row counts.&quot;&quot;&quot;</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> json.dumps(registry.list_datasets())</span></span></code></pre></div><hr><h2 id="lesson-2-3-agent-loop-variants" tabindex="-1">Lesson 2.3: Agent Loop Variants <a class="header-anchor" href="#lesson-2-3-agent-loop-variants" aria-label="Permalink to &quot;Lesson 2.3: Agent Loop Variants&quot;"></a></h2><h3 id="level-1-simple-prompt-→-execute" tabindex="-1">Level 1: Simple Prompt → Execute <a class="header-anchor" href="#level-1-simple-prompt-→-execute" aria-label="Permalink to &quot;Level 1: Simple Prompt → Execute&quot;"></a></h3><div class="language- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>LLM call → command string → execute → output</span></span></code></pre></div><p>No loop. Single API call generates a command, you run it. Good for: code generation, translation, summarization.</p><h3 id="level-2-tool-use-agent-loop" tabindex="-1">Level 2: Tool-Use Agent Loop <a class="header-anchor" href="#level-2-tool-use-agent-loop" aria-label="Permalink to &quot;Level 2: Tool-Use Agent Loop&quot;"></a></h3><div class="language- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>while not terminal_tool_called:</span></span>
<span class="line"><span> LLM(invoke with tools) → tool call → execute → feed result back</span></span></code></pre></div><p>Core pattern. 5+ tools, forced tool choice. Terminal tool (e.g., <code>run_final_query</code>) exits. Good for: data analysis, file editing, web research.</p><h3 id="level-3-task-completion-loop" tabindex="-1">Level 3: Task-Completion Loop <a class="header-anchor" href="#level-3-task-completion-loop" aria-label="Permalink to &quot;Level 3: Task-Completion Loop&quot;"></a></h3><div class="language- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>while not complete_task_tool_called:</span></span>
<span class="line"><span> LLM → tool call → execute → observe → continue or complete</span></span></code></pre></div><p>Adds explicit <code>complete_task</code> terminal action. Clear success/failure conditions. Good for: multi-step workflows with defined finish criteria.</p><h3 id="level-4-sub-agent-orchestration" tabindex="-1">Level 4: Sub-Agent Orchestration <a class="header-anchor" href="#level-4-sub-agent-orchestration" aria-label="Permalink to &quot;Level 4: Sub-Agent Orchestration&quot;"></a></h3><div class="language- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>main_agent → spawns sub_agents → collects results → synthesizes</span></span></code></pre></div><p>Main agent calls LLM sub-calls for parallel work (e.g., check 10 files for relevance in parallel batches). Good for: codebase analysis, parallel research, batch operations.</p><h3 id="level-5-full-multi-agent" tabindex="-1">Level 5: Full Multi-Agent <a class="header-anchor" href="#level-5-full-multi-agent" aria-label="Permalink to &quot;Level 5: Full Multi-Agent&quot;"></a></h3><div class="language- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>orchestrator → team_lead → worker_agents → results bubble up</span></span></code></pre></div><p>Depth-2+ delegation hierarchy. Each agent has domain, tools, memory. Good for: production systems, complex workflows.</p><hr><h2 id="lesson-2-3b-skills-system-deep-dive" tabindex="-1">Lesson 2.3b: Skills System Deep Dive <a class="header-anchor" href="#lesson-2-3b-skills-system-deep-dive" aria-label="Permalink to &quot;Lesson 2.3b: Skills System Deep Dive&quot;"></a></h2><p>Skills are the building blocks of agent behavior. A skill is a self-contained instruction file that an agent loads and follows.</p><h3 id="path-scoped-skills" tabindex="-1">Path-Scoped Skills <a class="header-anchor" href="#path-scoped-skills" aria-label="Permalink to &quot;Path-Scoped Skills&quot;"></a></h3><p>Skills can be scoped to specific directories. The agent only loads skills relevant to the files it&#39;s working on:</p><div class="language- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>repo/</span></span>
<span class="line"><span>├── .claude/skills/</span></span>
<span class="line"><span>│ ├── global/</span></span>
<span class="line"><span>│ │ ├── conversational-response.md</span></span>
<span class="line"><span>│ │ └── security-policy.md</span></span>
<span class="line"><span>│ ├── frontend/</span></span>
<span class="line"><span>│ │ ├── react-patterns.md</span></span>
<span class="line"><span>│ │ └── css-guidelines.md</span></span>
<span class="line"><span>│ └── backend/</span></span>
<span class="line"><span>│ ├── api-design.md</span></span>
<span class="line"><span>│ └── database-migrations.md</span></span></code></pre></div><h3 id="subdirectory-claude-md" tabindex="-1">Subdirectory CLAUDE.md <a class="header-anchor" href="#subdirectory-claude-md" aria-label="Permalink to &quot;Subdirectory CLAUDE.md&quot;"></a></h3><p>One CLAUDE.md doesn&#39;t scale for large projects. Modern agents walk the directory tree and load the closest rules file:</p><div class="language- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>repo/</span></span>
<span class="line"><span>├── CLAUDE.md # Root rules (all agents)</span></span>
<span class="line"><span>├── frontend/CLAUDE.md # Frontend overrides</span></span>
<span class="line"><span>├── backend/CLAUDE.md # Backend overrides</span></span>
<span class="line"><span>└── deploy/CLAUDE.md # Deployment rules</span></span></code></pre></div><h3 id="plugin-distribution-model" tabindex="-1">Plugin Distribution Model <a class="header-anchor" href="#plugin-distribution-model" aria-label="Permalink to &quot;Plugin Distribution Model&quot;"></a></h3><p>Skills and agent configs can be packaged as shareable plugins:</p><div class="language- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>my-agent-kit/</span></span>
<span class="line"><span>├── agent.yaml # Agent definition</span></span>
<span class="line"><span>├── skills/ # Skill files</span></span>
<span class="line"><span>├── tools/ # Custom tools</span></span>
<span class="line"><span>├── hooks/ # Lifecycle hooks</span></span>
<span class="line"><span>└── README.md # Usage instructions</span></span></code></pre></div><p>This is how ClaudeFAST distributes their 280 skills and 16 agents as commercial kits.</p><h3 id="lsp-mcp-server-pattern" tabindex="-1">LSP MCP Server Pattern <a class="header-anchor" href="#lsp-mcp-server-pattern" aria-label="Permalink to &quot;LSP MCP Server Pattern&quot;"></a></h3><p>For large codebases, grep is too slow. An LSP (Language Server Protocol) MCP server gives agents symbol-level search:</p><div class="language- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>Agent → MCP Client → LSP MCP Server → Language Server → Codebase</span></span></code></pre></div><p>Tools exposed: <code>find_definition</code>, <code>find_references</code>, <code>find_symbols</code>, <code>get_hover_info</code></p><hr><h2 id="lesson-2-3c-agent-readable-workspace-design" tabindex="-1">Lesson 2.3c: Agent-Readable Workspace Design <a class="header-anchor" href="#lesson-2-3c-agent-readable-workspace-design" aria-label="Permalink to &quot;Lesson 2.3c: Agent-Readable Workspace Design&quot;"></a></h2><p>An agent-readable workspace is organized so an agent can discover everything it needs without being told.</p><h3 id="the-discovery-pattern" tabindex="-1">The Discovery Pattern <a class="header-anchor" href="#the-discovery-pattern" aria-label="Permalink to &quot;The Discovery Pattern&quot;"></a></h3><div class="language- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>repo/</span></span>
<span class="line"><span>├── CLAUDE.md ← Agent reads this FIRST</span></span>
<span class="line"><span>├── init.sh ← Run this to set up environment</span></span>
<span class="line"><span>├── feature_list.json ← What features exist, what&#39;s done</span></span>
<span class="line"><span>├── Makefile ← Common commands (test, build, lint)</span></span>
<span class="line"><span>├── tests/ ← Expected outcomes (evidence)</span></span>
<span class="line"><span>└── docs/ ← Architecture decisions (ADRs)</span></span></code></pre></div><h3 id="initialization-as-its-own-phase" tabindex="-1">Initialization as Its Own Phase <a class="header-anchor" href="#initialization-as-its-own-phase" aria-label="Permalink to &quot;Initialization as Its Own Phase&quot;"></a></h3><p>The first thing an agent should do is NOT start coding. It should initialize:</p><ol><li><strong>Read</strong> all instruction files (CLAUDE.md, AGENTS.md, skills)</li><li><strong>Discover</strong> project structure (list files, read key configs)</li><li><strong>Verify</strong> environment (check tool versions, API keys, dependencies)</li><li><strong>Load</strong> state (mental models, session history, feature progress)</li><li><strong>Plan</strong> before coding</li></ol><h3 id="feature-lists-as-harness-primitives" tabindex="-1">Feature Lists as Harness Primitives <a class="header-anchor" href="#feature-lists-as-harness-primitives" aria-label="Permalink to &quot;Feature Lists as Harness Primitives&quot;"></a></h3><p>A <code>feature_list.json</code> gives agents a structured inventory of what to build and what evidence proves completion:</p><div class="language-json vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">json</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> &quot;features&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> &quot;id&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;auth-login&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> &quot;status&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;done&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> &quot;evidence&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;tests/test_auth.py::test_login&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;src/auth/login.tsx&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> },</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> &quot;id&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;auth-register&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> &quot;status&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;in_progress&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> &quot;evidence&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: []</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ]</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><h3 id="clean-state-between-sessions" tabindex="-1">Clean State Between Sessions <a class="header-anchor" href="#clean-state-between-sessions" aria-label="Permalink to &quot;Clean State Between Sessions&quot;"></a></h3><p>Every session must leave the workspace in a clean state:</p><ul><li>No half-finished files</li><li>No dangling processes</li><li>No uncommitted changes</li><li>Session logs archived</li><li>Temporary files cleaned</li></ul><p>The next agent (or the same agent on the next session) should find the workspace as if no one touched it.</p><hr><h2 id="lesson-2-4-context-window-management" tabindex="-1">Lesson 2.4: Context Window Management <a class="header-anchor" href="#lesson-2-4-context-window-management" aria-label="Permalink to &quot;Lesson 2.4: Context Window Management&quot;"></a></h2><h3 id="the-problem" tabindex="-1">The Problem <a class="header-anchor" href="#the-problem" aria-label="Permalink to &quot;The Problem&quot;"></a></h3><p>Context windows grow unbounded. Every tool result, every LLM response, every intermediate step gets appended. After 20 turns of file editing, your context contains thousands of lines of file contents and logs.</p><h3 id="solutions-from-worst-to-best" tabindex="-1">Solutions (from worst to best) <a class="header-anchor" href="#solutions-from-worst-to-best" aria-label="Permalink to &quot;Solutions (from worst to best)&quot;"></a></h3><table tabindex="0"><thead><tr><th>Approach</th><th>How</th><th>Trade-off</th></tr></thead><tbody><tr><td>Sliding window</td><td>Keep last N messages</td><td>Lose early context</td></tr><tr><td>Summarization</td><td>Compress old messages into summary</td><td>Information loss</td></tr><tr><td>Structured outputs</td><td>Agents output structured data, not free text</td><td>Requires schema design</td></tr><tr><td>Multi-agent isolation</td><td>Each agent has focused context window</td><td>Complexity, coordination cost</td></tr></tbody></table><h3 id="best-practice-hybrid" tabindex="-1">Best Practice: Hybrid <a class="header-anchor" href="#best-practice-hybrid" aria-label="Permalink to &quot;Best Practice: Hybrid&quot;"></a></h3><div class="language- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>[System prompt (always)] + [Recent N turns (full)] + [Summary of earlier turns] + [Current tool results]</span></span></code></pre></div><hr><h2 id="lesson-2-5-memory-patterns" tabindex="-1">Lesson 2.5: Memory Patterns <a class="header-anchor" href="#lesson-2-5-memory-patterns" aria-label="Permalink to &quot;Lesson 2.5: Memory Patterns&quot;"></a></h2><h3 id="mental-models-from-multi-team-system" tabindex="-1">Mental Models (from multi-team system) <a class="header-anchor" href="#mental-models-from-multi-team-system" aria-label="Permalink to &quot;Mental Models (from multi-team system)&quot;"></a></h3><p>Every agent maintains a personal expertise file:</p><div class="language-yaml vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">yaml</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># .pi/multi-team/expertise/backend-dev-mental-model.yaml</span></span>
<span class="line"><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">expertise</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">topic</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;API patterns used in this project&quot;</span></span>
<span class="line"><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> notes</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;We use tRPC for type-safe API calls. All endpoints follow /api/trpc/{router}.{procedure}&quot;</span></span>
<span class="line"><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> last_updated</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;2026-04-20&quot;</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">topic</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Database conventions&quot;</span></span>
<span class="line"><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> notes</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;SQLite with Drizzle ORM. Migrations in packages/db/src/migrations/&quot;</span></span>
<span class="line"><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> last_updated</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;2026-04-22&quot;</span></span></code></pre></div><p><strong>Rules</strong>:</p><ul><li>Agents own their mental models (they write them, not you)</li><li>Read-only expertise for critical domain knowledge (billing, deployment, security)</li><li>Self-improve commands validate expertise against actual codebase</li><li>Mental models compound across sessions</li></ul><h3 id="scratch-pads" tabindex="-1">Scratch Pads <a class="header-anchor" href="#scratch-pads" aria-label="Permalink to &quot;Scratch Pads&quot;"></a></h3><p>Ephemeral memory for a single session. Good for tracking:</p><ul><li>What&#39;s been tried and failed</li><li>Current working state</li><li>Decisions made this session</li></ul><hr><h2 id="lesson-2-6-the-reasoning-parameter" tabindex="-1">Lesson 2.6: The Reasoning Parameter <a class="header-anchor" href="#lesson-2-6-the-reasoning-parameter" aria-label="Permalink to &quot;Lesson 2.6: The Reasoning Parameter&quot;"></a></h2><p>Forcing the LLM to explain every tool call is the highest-ROI prompt engineering technique:</p><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> search_documentation</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(query: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">str</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, reasoning: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">str</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) -&gt; </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">str</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;&quot;&quot;</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> Search documentation.</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> </span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> Args:</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> query: The search terms</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> reasoning: WHY you are searching for this (required for audit)</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;&quot;&quot;</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> ...</span></span></code></pre></div><p><strong>Why it works</strong>:</p><ol><li>Forces the LLM to articulate intent before acting</li><li>Creates an audit trail of every decision</li><li>Catches hallucinated tool calls (if the reasoning is nonsense, the call is suspect)</li><li>Gives the LLM an extra &quot;thinking step&quot; without using chain-of-thought</li></ol><hr><h2 id="lesson-2-7-codebase-architectures-for-agents" tabindex="-1">Lesson 2.7: Codebase Architectures for Agents <a class="header-anchor" href="#lesson-2-7-codebase-architectures-for-agents" aria-label="Permalink to &quot;Lesson 2.7: Codebase Architectures for Agents&quot;"></a></h2><p>From single-file-agents research, 4 patterns for scaling agent codebases:</p><table tabindex="0"><thead><tr><th>Architecture</th><th>Structure</th><th>Best For</th></tr></thead><tbody><tr><td><strong>Atomic/Composable</strong></td><td>atom/ → molecule/ → organism/</td><td>Reusable tool primitives across many agents</td></tr><tr><td><strong>Layered</strong></td><td>api/ → services/ → models/ → data/</td><td>Clear separation of concerns within one agent</td></tr><tr><td><strong>Pipeline</strong></td><td>steps/ → pipeline_manager/ → shared/</td><td>Data transformation flows</td></tr><tr><td><strong>Vertical Slice</strong></td><td>features/{feature}/{api,service,model}/</td><td>Multiple independent agent capabilities</td></tr></tbody></table><h3 id="choosing-the-right-architecture" tabindex="-1">Choosing the Right Architecture <a class="header-anchor" href="#choosing-the-right-architecture" aria-label="Permalink to &quot;Choosing the Right Architecture&quot;"></a></h3><p>There&#39;s no single right answer. The choice depends on your agent&#39;s role:</p><table tabindex="0"><thead><tr><th>Agent Type</th><th>Recommended Arch</th><th>Why</th></tr></thead><tbody><tr><td>Single-purpose tool agent</td><td>Atomic</td><td>Simple, composable, testable</td></tr><tr><td>Multi-step workflow agent</td><td>Pipeline</td><td>Clear stage boundaries</td></tr><tr><td>Complex reasoning agent</td><td>Layered</td><td>Separation of concerns</td></tr><tr><td>Multi-capability platform</td><td>Vertical Slice</td><td>Independent feature teams</td></tr></tbody></table><p><strong>Rule of thumb</strong>: Start with Atomic (single file per tool). Only add architecture when the agent has 5+ tools or 3+ agents share tools.</p><hr><h2 id="lesson-2-7b-configuration-architecture" tabindex="-1">Lesson 2.7b: Configuration Architecture <a class="header-anchor" href="#lesson-2-7b-configuration-architecture" aria-label="Permalink to &quot;Lesson 2.7b: Configuration Architecture&quot;"></a></h2><p>Where does agent configuration live? Three patterns:</p><h3 id="pattern-a-flat-config-single-file" tabindex="-1">Pattern A: Flat Config (Single File) <a class="header-anchor" href="#pattern-a-flat-config-single-file" aria-label="Permalink to &quot;Pattern A: Flat Config (Single File)&quot;"></a></h3><div class="language-yaml vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">yaml</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># agent-config.yaml — everything in one place</span></span>
<span class="line"><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">agent</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span class="line"><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;code-reviewer&quot;</span></span>
<span class="line"><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> model</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">claude-sonnet-4</span></span>
<span class="line"><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> tools</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">read_file</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">grep_search</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">list_files</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span>
<span class="line"><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> max_turns</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">15</span></span>
<span class="line"><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> hooks</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">pre-tool/l3-blacklist</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">post-tool/logger</span></span></code></pre></div><p><strong>Best for</strong>: Single-agent projects, prototyping, small teams</p><h3 id="pattern-b-layered-config-directory-structure" tabindex="-1">Pattern B: Layered Config (Directory Structure) <a class="header-anchor" href="#pattern-b-layered-config-directory-structure" aria-label="Permalink to &quot;Pattern B: Layered Config (Directory Structure)&quot;"></a></h3><div class="language- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>agents/</span></span>
<span class="line"><span>├── base.yaml ← shared defaults (model, security)</span></span>
<span class="line"><span>├── reviewer.yaml ← extends base.yaml </span></span>
<span class="line"><span>├── builder.yaml ← extends base.yaml</span></span>
<span class="line"><span>└── orchestrator.yaml ← extends base.yaml</span></span></code></pre></div><p><strong>Best for</strong>: Multi-agent systems, team environments</p><h3 id="pattern-c-discoverable-config-agent-readable" tabindex="-1">Pattern C: Discoverable Config (Agent-Readable) <a class="header-anchor" href="#pattern-c-discoverable-config-agent-readable" aria-label="Permalink to &quot;Pattern C: Discoverable Config (Agent-Readable)&quot;"></a></h3><div class="language- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>repo/</span></span>
<span class="line"><span>├── AGENTS.md ← agent instructions</span></span>
<span class="line"><span>├── skills/ ← skill definitions</span></span>
<span class="line"><span>├── .mcp.json ← MCP server config</span></span>
<span class="line"><span>├── .claude/hooks/ ← lifecycle hooks</span></span>
<span class="line"><span>└── teams.yaml ← multi-team config</span></span></code></pre></div><p><strong>Best for</strong>: Production systems where agents need to self-configure</p><hr><h2 id="lesson-2-7c-error-handling-architecture" tabindex="-1">Lesson 2.7c: Error Handling Architecture <a class="header-anchor" href="#lesson-2-7c-error-handling-architecture" aria-label="Permalink to &quot;Lesson 2.7c: Error Handling Architecture&quot;"></a></h2><p>Every agent needs four error-handling layers:</p><ol><li><strong>Tool-level</strong>: Tool returns error string instead of crashing (all labs teach this)</li><li><strong>Loop-level</strong>: MAX_ITERATIONS prevents infinite loops (every lab has this)</li><li><strong>Agent-level</strong>: Retry with backoff on API failures</li><li><strong>System-level</strong>: Supervisor agent or human handoff for unrecoverable errors</li></ol><div class="language-python vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">python</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># System-level error handling pattern</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">MAX_RETRIES</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> =</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 3</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">for</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> attempt </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">in</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> range</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">MAX_RETRIES</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">):</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> try</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> response </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> client.messages.create(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">...</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> process_response(response)</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> except</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> APIError </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">as</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> e:</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> attempt </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">==</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> MAX_RETRIES</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> -</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;error&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;API unavailable after 3 retries&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;fallback&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;use cached result&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> time.sleep(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">2</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> **</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> attempt) </span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># exponential backoff</span></span></code></pre></div><hr><h2 id="lab-2-8-multi-tool-agent" tabindex="-1">Lab 2.8: Multi-Tool Agent <a class="header-anchor" href="#lab-2-8-multi-tool-agent" aria-label="Permalink to &quot;Lab 2.8: Multi-Tool Agent&quot;"></a></h2><p><strong>Objective</strong>: Add file operations + web search tools to the agent from Lab 1.</p><p><strong>Starter</strong>: <code>course/labs/L2-multi-tool/starter.py</code><br><strong>Solution</strong>: <code>course/labs/L2-multi-tool/solution.py</code></p><hr><h2 id="lab-2-9-context-aware-agent" tabindex="-1">Lab 2.9: Context-Aware Agent <a class="header-anchor" href="#lab-2-9-context-aware-agent" aria-label="Permalink to &quot;Lab 2.9: Context-Aware Agent&quot;"></a></h2><p><strong>Objective</strong>: Implement sliding window + summarization for long sessions.</p><p><strong>Starter</strong>: <code>course/labs/L2-context/starter.py</code><br><strong>Solution</strong>: <code>course/labs/L2-context/solution.py</code></p>`,120)])])}const g=a(n,[["render",l]]);export{k as __pageData,g as default};