The Agent SDK Runs the Loop for You
Why This Matters
In module 4 you wrote the agent loop yourself, so you know every moving part. Now you can hand the loop to the Agent SDK and keep your attention for the decisions only you can make: which tools, which permissions, which brief, which output contract.
What the Agent SDK is
The Agent SDK gives you, as a library, the same agent loop, tools and context management that power Claude Code. You call one function, query(), and iterate over the messages it streams back.
npm install @anthropic-ai/claude-agent-sdk
import { query } from "@anthropic-ai/claude-agent-sdk";
for await (const message of query({
prompt: `Triage this failure and call submit_verdict when done.\n${buildTriageMessage(run, failure)}`,
options: {
systemPrompt: SYSTEM_PROMPT,
mcpServers: { testpilot: { command: "npx", args: ["tsx", "mcp/server.ts"] } },
allowedTools: ["mcp__testpilot__get_test_history", "mcp__testpilot__read_log", "mcp__testpilot__get_commit_diff"],
},
})) {
if ("result" in message) console.log(message.result);
}
Check the Agent SDK reference for the exact option names in your version; they evolve.
What you get
| You wrote in module 4 | The SDK handles |
|---|---|
| The send / run tools / send back loop | ✓ |
| Matching tool results to tool calls | ✓ |
| Connecting MCP servers | ✓ |
| Subagents | ✓ (next lessons) |
| Hooks around tool calls | ✓ (module 7) |
| Sessions you can resume | ✓ |
What stays yours
- The tools and what they may do (your MCP server)
- The permissions:
allowedToolspre-approves tools; it does not limit which tools exist. To limit them, usetools(the built-in tools;[]for none) anddisallowedTools, and setpermissionMode: "dontAsk"so that anything not approved is denied instead of asked. Tools that need no approval, such as reads in the working directory, run anyway - The brief: system prompt, definitions, rules
- The output contract: you still validate the verdict with
triageResultSchemabefore acting on it - The tests: scripted tests for your tools and gates, the golden set for the model’s judgement
A real run: the same team, two runtimes
solution/lesson-40-triage-team can run its specialists either way. npm run team uses the Messages API with the loop from module 4; npm run team -- --sdk uses the Agent SDK, with testpilot-mcp connected, no built-in tools (tools: []), only the role’s MCP tools in allowedTools, permissionMode: "dontAsk" and structured output:
| Own loop (Messages API) | Agent SDK | |
|---|---|---|
| Verdicts on run-1099 | 40 triaged, 1 bug proposal | the same |
| Time | 32 s | 63 s |
| Why the difference | one process, tools in-process | a Claude Code process and an MCP server per specialist |
| Code you maintain | the loop, tool plumbing, stop_reason handling | options and the output contract |
The first --sdk run failed for every specialist: Claude Code rejected the output schema because zod marks it with a draft 2020-12 $schema reference its validator cannot resolve. One line fixed it. The run did not crash; it reported 40 failures as not triaged, each with the reason (lesson 40). And the coordinator, its plan, the gates and the coverage check did not change at all between the two runtimes: they are code.
When to keep your own loop
- unusual control flow (for example strict alternation between two models)
- very strict audit requirements where you want every byte of every request in your own code
- a tiny single-purpose agent where a dependency is not worth it
For TestPilot’s team in this module, the SDK saves a lot of plumbing.
Exercise
Rebuild lesson 27’s single-agent triage with the Agent SDK, connected to testpilot-mcp, with only the read-only tools allowed. Run it on t-002, t-004 and t-005 and compare tool paths and verdicts with your own loop.
Compare with solution/lesson-40-triage-team, which covers lessons 34–40 (see docs/exercises.md, module 5).
