How to Build an AI Agent
How to Build an AI Agent
Back to Skills

SubagentsSkills

s04 (151 LOC) → s05 (187 LOC)

LOC Delta

+36lines

New Tools

1

load_skill
New Classes

1

SkillLoader
New Functions

0

Subagents

Clean Context Per Subtask

151 LOC

5 tools: bash, read_file, write_file, edit_file, task

planning

Skills

Load on Demand

187 LOC

5 tools: bash, read_file, write_file, edit_file, load_skill

planning

Source Code Diff

s04 (s04_subagent.py) -> s05 (s05_skill_loading.py)
11#!/usr/bin/env python3
22"""
3-s04_subagent.py - Subagents
3+s05_skill_loading.py - Skills
44
5-Spawn a child agent with fresh messages=[]. The child works in its own
6-context, sharing the filesystem, then returns only a summary to the parent.
5+Two-layer skill injection that avoids bloating the system prompt:
76
8- Parent agent Subagent
9- +------------------+ +------------------+
10- | messages=[...] | | messages=[] | <-- fresh
11- | | dispatch | |
12- | tool: task | ---------->| while tool_use: |
13- | prompt="..." | | call tools |
14- | description="" | | append results |
15- | | summary | |
16- | result = "..." | <--------- | return last text |
17- +------------------+ +------------------+
18- |
19- Parent context stays clean.
20- Subagent context is discarded.
7+ Layer 1 (cheap): skill names in system prompt (~100 tokens/skill)
8+ Layer 2 (on demand): full skill body in tool_result
219
22-Key insight: "Process isolation gives context isolation for free."
10+ skills/
11+ pdf/
12+ SKILL.md <-- frontmatter (name, description) + body
13+ code-review/
14+ SKILL.md
15+
16+ System prompt:
17+ +--------------------------------------+
18+ | You are a coding agent. |
19+ | Skills available: |
20+ | - pdf: Process PDF files... | <-- Layer 1: metadata only
21+ | - code-review: Review code... |
22+ +--------------------------------------+
23+
24+ When model calls load_skill("pdf"):
25+ +--------------------------------------+
26+ | tool_result: |
27+ | <skill> |
28+ | Full PDF processing instructions | <-- Layer 2: full body
29+ | Step 1: ... |
30+ | Step 2: ... |
31+ | </skill> |
32+ +--------------------------------------+
33+
34+Key insight: "Don't put everything in the system prompt. Load on demand."
2335"""
2436
2537import os
38+import re
2639import subprocess
2740from pathlib import Path
2841
2942from anthropic import Anthropic
3043from dotenv import load_dotenv
3144
3245load_dotenv(override=True)
3346
3447if os.getenv("ANTHROPIC_BASE_URL"):
3548 os.environ.pop("ANTHROPIC_AUTH_TOKEN", None)
3649
3750WORKDIR = Path.cwd()
3851client = Anthropic(base_url=os.getenv("ANTHROPIC_BASE_URL"))
3952MODEL = os.environ["MODEL_ID"]
53+SKILLS_DIR = WORKDIR / "skills"
4054
41-SYSTEM = f"You are a coding agent at {WORKDIR}. Use the task tool to delegate exploration or subtasks."
42-SUBAGENT_SYSTEM = f"You are a coding subagent at {WORKDIR}. Complete the given task, then summarize your findings."
4355
56+# -- SkillLoader: scan skills/<name>/SKILL.md with YAML frontmatter --
57+class SkillLoader:
58+ def __init__(self, skills_dir: Path):
59+ self.skills_dir = skills_dir
60+ self.skills = {}
61+ self._load_all()
4462
45-# -- Tool implementations shared by parent and child --
63+ def _load_all(self):
64+ if not self.skills_dir.exists():
65+ return
66+ for f in sorted(self.skills_dir.rglob("SKILL.md")):
67+ text = f.read_text()
68+ meta, body = self._parse_frontmatter(text)
69+ name = meta.get("name", f.parent.name)
70+ self.skills[name] = {"meta": meta, "body": body, "path": str(f)}
71+
72+ def _parse_frontmatter(self, text: str) -> tuple:
73+ """Parse YAML frontmatter between --- delimiters."""
74+ match = re.match(r"^---\n(.*?)\n---\n(.*)", text, re.DOTALL)
75+ if not match:
76+ return {}, text
77+ meta = {}
78+ for line in match.group(1).strip().splitlines():
79+ if ":" in line:
80+ key, val = line.split(":", 1)
81+ meta[key.strip()] = val.strip()
82+ return meta, match.group(2).strip()
83+
84+ def get_descriptions(self) -> str:
85+ """Layer 1: short descriptions for the system prompt."""
86+ if not self.skills:
87+ return "(no skills available)"
88+ lines = []
89+ for name, skill in self.skills.items():
90+ desc = skill["meta"].get("description", "No description")
91+ tags = skill["meta"].get("tags", "")
92+ line = f" - {name}: {desc}"
93+ if tags:
94+ line += f" [{tags}]"
95+ lines.append(line)
96+ return "\n".join(lines)
97+
98+ def get_content(self, name: str) -> str:
99+ """Layer 2: full skill body returned in tool_result."""
100+ skill = self.skills.get(name)
101+ if not skill:
102+ return f"Error: Unknown skill '{name}'. Available: {', '.join(self.skills.keys())}"
103+ return f"<skill name=\"{name}\">\n{skill['body']}\n</skill>"
104+
105+
106+SKILL_LOADER = SkillLoader(SKILLS_DIR)
107+
108+# Layer 1: skill metadata injected into system prompt
109+SYSTEM = f"""You are a coding agent at {WORKDIR}.
110+Use load_skill to access specialized knowledge before tackling unfamiliar topics.
111+
112+Skills available:
113+{SKILL_LOADER.get_descriptions()}"""
114+
115+
116+# -- Tool implementations --
46117def safe_path(p: str) -> Path:
47118 path = (WORKDIR / p).resolve()
48119 if not path.is_relative_to(WORKDIR):
49120 raise ValueError(f"Path escapes workspace: {p}")
50121 return path
51122
52123def run_bash(command: str) -> str:
53124 dangerous = ["rm -rf /", "sudo", "shutdown", "reboot", "> /dev/"]
54125 if any(d in command for d in dangerous):
55126 return "Error: Dangerous command blocked"
56127 try:
57128 r = subprocess.run(command, shell=True, cwd=WORKDIR,
58129 capture_output=True, text=True, timeout=120)
59130 out = (r.stdout + r.stderr).strip()
60131 return out[:50000] if out else "(no output)"
61132 except subprocess.TimeoutExpired:
62133 return "Error: Timeout (120s)"
63134
64135def run_read(path: str, limit: int = None) -> str:
65136 try:
66137 lines = safe_path(path).read_text().splitlines()
67138 if limit and limit < len(lines):
68139 lines = lines[:limit] + [f"... ({len(lines) - limit} more)"]
69140 return "\n".join(lines)[:50000]
70141 except Exception as e:
71142 return f"Error: {e}"
72143
73144def run_write(path: str, content: str) -> str:
74145 try:
75146 fp = safe_path(path)
76147 fp.parent.mkdir(parents=True, exist_ok=True)
77148 fp.write_text(content)
78149 return f"Wrote {len(content)} bytes"
79150 except Exception as e:
80151 return f"Error: {e}"
81152
82153def run_edit(path: str, old_text: str, new_text: str) -> str:
83154 try:
84155 fp = safe_path(path)
85156 content = fp.read_text()
86157 if old_text not in content:
87158 return f"Error: Text not found in {path}"
88159 fp.write_text(content.replace(old_text, new_text, 1))
89160 return f"Edited {path}"
90161 except Exception as e:
91162 return f"Error: {e}"
92163
93164
94165TOOL_HANDLERS = {
95166 "bash": lambda **kw: run_bash(kw["command"]),
96167 "read_file": lambda **kw: run_read(kw["path"], kw.get("limit")),
97168 "write_file": lambda **kw: run_write(kw["path"], kw["content"]),
98169 "edit_file": lambda **kw: run_edit(kw["path"], kw["old_text"], kw["new_text"]),
170+ "load_skill": lambda **kw: SKILL_LOADER.get_content(kw["name"]),
99171}
100172
101-# Child gets all base tools except task (no recursive spawning)
102-CHILD_TOOLS = [
173+TOOLS = [
103174 {"name": "bash", "description": "Run a shell command.",
104175 "input_schema": {"type": "object", "properties": {"command": {"type": "string"}}, "required": ["command"]}},
105176 {"name": "read_file", "description": "Read file contents.",
106177 "input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "limit": {"type": "integer"}}, "required": ["path"]}},
107178 {"name": "write_file", "description": "Write content to file.",
108179 "input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "content": {"type": "string"}}, "required": ["path", "content"]}},
109180 {"name": "edit_file", "description": "Replace exact text in file.",
110181 "input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "old_text": {"type": "string"}, "new_text": {"type": "string"}}, "required": ["path", "old_text", "new_text"]}},
182+ {"name": "load_skill", "description": "Load specialized knowledge by name.",
183+ "input_schema": {"type": "object", "properties": {"name": {"type": "string", "description": "Skill name to load"}}, "required": ["name"]}},
111184]
112185
113186
114-# -- Subagent: fresh context, filtered tools, summary-only return --
115-def run_subagent(prompt: str) -> str:
116- sub_messages = [{"role": "user", "content": prompt}] # fresh context
117- for _ in range(30): # safety limit
118- response = client.messages.create(
119- model=MODEL, system=SUBAGENT_SYSTEM, messages=sub_messages,
120- tools=CHILD_TOOLS, max_tokens=8000,
121- )
122- sub_messages.append({"role": "assistant", "content": response.content})
123- if response.stop_reason != "tool_use":
124- break
125- results = []
126- for block in response.content:
127- if block.type == "tool_use":
128- handler = TOOL_HANDLERS.get(block.name)
129- output = handler(**block.input) if handler else f"Unknown tool: {block.name}"
130- results.append({"type": "tool_result", "tool_use_id": block.id, "content": str(output)[:50000]})
131- sub_messages.append({"role": "user", "content": results})
132- # Only the final text returns to the parent -- child context is discarded
133- return "".join(b.text for b in response.content if hasattr(b, "text")) or "(no summary)"
134-
135-
136-# -- Parent tools: base tools + task dispatcher --
137-PARENT_TOOLS = CHILD_TOOLS + [
138- {"name": "task", "description": "Spawn a subagent with fresh context. It shares the filesystem but not conversation history.",
139- "input_schema": {"type": "object", "properties": {"prompt": {"type": "string"}, "description": {"type": "string", "description": "Short description of the task"}}, "required": ["prompt"]}},
140-]
141-
142-
143187def agent_loop(messages: list):
144188 while True:
145189 response = client.messages.create(
146190 model=MODEL, system=SYSTEM, messages=messages,
147- tools=PARENT_TOOLS, max_tokens=8000,
191+ tools=TOOLS, max_tokens=8000,
148192 )
149193 messages.append({"role": "assistant", "content": response.content})
150194 if response.stop_reason != "tool_use":
151195 return
152196 results = []
153197 for block in response.content:
154198 if block.type == "tool_use":
155- if block.name == "task":
156- desc = block.input.get("description", "subtask")
157- print(f"> task ({desc}): {block.input['prompt'][:80]}")
158- output = run_subagent(block.input["prompt"])
159- else:
160- handler = TOOL_HANDLERS.get(block.name)
199+ handler = TOOL_HANDLERS.get(block.name)
200+ try:
161201 output = handler(**block.input) if handler else f"Unknown tool: {block.name}"
162- print(f" {str(output)[:200]}")
202+ except Exception as e:
203+ output = f"Error: {e}"
204+ print(f"> {block.name}: {str(output)[:200]}")
163205 results.append({"type": "tool_result", "tool_use_id": block.id, "content": str(output)})
164206 messages.append({"role": "user", "content": results})
165207
166208
167209if __name__ == "__main__":
168210 history = []
169211 while True:
170212 try:
171- query = input("\033[36ms04 >> \033[0m")
213+ query = input("\033[36ms05 >> \033[0m")
172214 except (EOFError, KeyboardInterrupt):
173215 break
174216 if query.strip().lower() in ("q", "exit", ""):
175217 break
176218 history.append({"role": "user", "content": query})
177219 agent_loop(history)
178220 response_content = history[-1]["content"]
179221 if isinstance(response_content, list):
180222 for block in response_content:
181223 if hasattr(block, "text"):
182224 print(block.text)
183225 print()